Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中可视化RDFLIB图_Python_Plot_Rdflib - Fatal编程技术网

在Python中可视化RDFLIB图

在Python中可视化RDFLIB图,python,plot,rdflib,Python,Plot,Rdflib,我不熟悉python中的RDFLIB。我在这里找到了创建图形的示例。可视化此代码创建的图形的最简单方法是什么 import rdflib # Now we create a graph, a representaiton of the ontology g = rdflib.Graph() # Now define the key words that we will use (the edge weights of the graph) has_border_with = rdflib.UR

我不熟悉python中的RDFLIB。我在这里找到了创建图形的示例。可视化此代码创建的图形的最简单方法是什么

import rdflib
# Now we create a graph, a representaiton of the ontology
g = rdflib.Graph()

# Now define the key words that we will use (the edge weights of the graph)
has_border_with = rdflib.URIRef('http://www.example.org/has_border_with')
located_in = rdflib.URIRef('http://www.example.org/located_in')

# define the things - base level objects that will be the nodes
# In this case first we have countries
germany = rdflib.URIRef('http://www.example.org/country1')
france = rdflib.URIRef('http://www.example.org/country2')
china = rdflib.URIRef('http://www.example.org/country3')
mongolia = rdflib.URIRef('http://www.example.org/country4')

# then we have continents
europa = rdflib.URIRef('http://www.example.org/part1')
asia = rdflib.URIRef('http://www.example.org/part2')

# Having defined the things and the edge weights, now assemble the graph
g.add((germany,has_border_with,france))
g.add((china,has_border_with,mongolia))
g.add((germany,located_in,europa))
g.add((france,located_in,europa))
g.add((china,located_in,asia))
g.add((mongolia,located_in,asia))

我看到rdflib包有一个工具组件,它有一个名为rdfs2dot的函数。如何使用此函数显示包含RDF图的绘图?

使用此问题中的提示:

我能够通过转换为Networkx图形并使用Networkx/Matplotlib绘图工具来绘制RDF图形

import rdflib
from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph
import networkx as nx
import matplotlib.pyplot as plt

url = 'https://www.w3.org/TeamSubmission/turtle/tests/test-30.ttl'

g = rdflib.Graph()
result = g.parse(url, format='turtle')

G = rdflib_to_networkx_multidigraph(result)

# Plot Networkx instance of RDF Graph
pos = nx.spring_layout(G, scale=2)
edge_labels = nx.get_edge_attributes(G, 'r')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
nx.draw(G, with_labels=True)

#if not in interactive mode for 
plt.show()

要可视化大型RDF图形,样式可能需要一些微调;-)

我可以按照您的建议,用rdf2dot为您的RDF图制作一张图片。尽管如此,我对这个库并不完全满意,因为它没有为文本创建节点。这是我的代码:

!pip install pydotplus
!pip install graphviz

import io
import pydotplus
from IPython.display import display, Image
from rdflib.tools.rdf2dot import rdf2dot

def visualize(g):
    stream = io.StringIO()
    rdf2dot(g, stream, opts = {display})
    dg = pydotplus.graph_from_dot_data(stream.getvalue())
    png = dg.create_png()
    display(Image(png))

visualize(g)
给出以下结果:

我看了一下,但找不到完整的解决方案。我发现rdfs2dot是一个命令行工具:您必须首先导出图形
g.serialize(“world.rdf”)
,然后将其转换为点语法:
rdfs2dot world.rdf>world.dot
,然后使用任何允许绘制点图的工具。例如xdot
xdot world.dot
。出于某种原因,rdfs2dot生成一个空图。根据Graph.serialize的文档,rdf应该是默认格式,因此这里或rdfs2dot中都存在错误。也许可以在github上查看打开的门票,或者发布新的一期。谢谢!这是有道理的。因此,您需要首先导出图形,然后在cmd中运行命令来创建图形点文件。有趣的是,您的答案比问题的答案要新几年。我想知道同时发生了什么变化或改进。从RDFLib GitHub的历史来看,似乎在您提问时,这个
RDFLib.extras.extral\u graph\u libs.RDFLib\u to\u networkx\u multi-digraph
已经可用。你的意思是你认为在这个时代应该有一个更好的解决方案吗?
result=g.parse(url,'ttl')
这一行对我不起作用。无法识别format参数,rdflib试图将其解析为rdf/xml。我必须编写“format”关键字,并且使用“turtle”而不是“ttl”使其工作
result=g.parse(url,format='turtle')
看起来很棒!您是否介意分享如何使用独立ttl或(如果可能)sparql端点复制此结果?我不确定您的意思。您可以按任何方式加载图形。一个小示例,包含从标准导入到RDF的RDF片段。如果您首先将ttl数据加载到一个字符串中,例如variabe ttlstring,那么您可以通过以下方式获取图形:从rdflib导入图形,URIRef,Literal g=graph()g.parse(data=ttlstring,format='turtle'),然后按照上述方式继续。抱歉,我无法在此处获取任何代码格式或新行。