0

I want to get a visual presentation of a rdf(.ttl) graph. I am using this code(python):

# print the ttl sources as a graph
def nt_to_graph(rel_path):
  g = Graph()
  g.parse(rel_path, format="ttl")
  g.bind("food", "http:<some_url>/food#")

  G = rdflib_to_networkx_multidigraph(g)

  # 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)
  nx.draw(G, with_labels=True)
  plt.show()

The nodes have the labels as follow: "http:some_url/food#".

I want the node label to be just "food".

How can i do this?

Thanks.

eytaniva
  • 11
  • 1

1 Answers1

1

Usually you would label an RDF class like this:

<some_class_uri>
  a rdfs:Class ;
  rdfs:label "Some Class" ;
  ...

And properties and Named Individuals similarly.

The prefix you're quoting here, food: http:/food#> isn't a label but a compact URI prefix mapping. Such a prefix will allow RDF serializers like rdflib's Turtle serializer to reduce

<http:<some_url>/food#Something> a rdfs:Class ;

to

food:Something a rdfs:Class ;

But this isn't going to do anything for labels.

I suggest you create labels in your RDF as actual properties of nodes (Classes, Properties or Named Individuals) and then extract them for plugging in to networkx as needed.

Nicholas Car
  • 1,164
  • 4
  • 7