NetworkX can also use matplotlib, but it seems that drawing a graph with PyGraphviz or Gephi allows for various finer settings.
Here, after creating a graph with Japanese labeled edges in NetworkX on Python, AGraph class object (dot format) Here is an example of converting to /agraph.html) and outputting the drawing result as a file with pygraphviz, and at the same time outputting a GraphML file. The output GraphML file can be input to Gephi for visualization.
Incidentally, I will summarize the installation method.
NetworkX
> pip install networkx
PyGraphviz
If you try to insert it with pip as it is, [an error occurs] such as fatal error:'graphviz / cgraph.h' file not found
(https://pod.hatenablog.com/entry/2015/03/07/163911) So, install it according to this page.
Graphviz is installed separately and the path is passed. The point is that there is PyGraphviz packaged for conda. The point is to specify that channel and do the following:
> conda install -c alubbock pygraphviz
Gephi
Install from https://gephi.org/.
--If you want to open GraphML by double-clicking, select the check box in the extension specification. -Java JRE is also required.
When adding an edge on the NetworkX side, add label
as shown below.
import pygraphviz as pgv
import networkx as nx
G = nx.Graph()
#If it is a lowercase label, it will be recognized as a label on Graphviz side.
#Both uppercase and lowercase letters are OK on the Gephi side
G.add_edges_from([('Node 1', 'Node 2', {'label': 'Edge', 'weight': 0.2})])
print(list(G.nodes))
#It is also possible to add attributes
G.node['Node 1']['style'] = 'solid,filled'
G.node['Node 1']['fillcolor'] = '#ccccff'
G.node['Node 1']['shape'] = 'egg'
G.node['Node 2']['color'] = '#ff9999'
G.node['Node 2']['fontcolor'] = 'red'
G.edges['Node 1', 'Node 2']['style'] = 'dotted'
G.edges['Node 1', 'Node 2']['fontsize'] = 10
G.edges['Node 1', 'Node 2']['fontcolor'] = '#00cc66'
nx.write_graphml(G, "test.graphml") #Output GraphML file for Gephi
#Convert to AGraph for GraphViz and draw
ag = nx.nx_agraph.to_agraph(G)
ag.node_attr.update(fontname="MS Gothic") #When using MS Gothic on Windows
ag.edge_attr.update(fontname="MS Gothic")
print(ag) #Can be confirmed in dot language
ag.draw('test.pdf', prog='fdp') #Try to specify fdp for the layout
If you add other attributes of dot language such as color
to nodes and edges, it will be reflected in Graphviz.
['Node 1', 'Node 2']
strict graph "" {
node [fontname="MS Gothic"];
edge [fontname="MS Gothic"];
Node 1[fillcolor="#ccccff",
shape=egg,
style="solid,filled"];
Node 2[color="#ff9999",
fontcolor=red];
Node 1--Node 2[fontcolor="#00cc66",
fontsize=10,
label=Edge,
style=dotted,
weight=0.2];
}
test.graphml
in the above example) from Open. (If you added the extension during installation, you can double-click the GraphML file.)Node Label> Show Label`` --
Node label> Fontto Japanese font --Check
edge label> shorten label (There are two" shorten label ", but the first one. This is probably a mistake of" show label ".) --
Edge label> Font` to Japanese fontExport to png. If it is left as it is, the edge is missing. As for the edge attribute, the Label has become dotted, so I removed all the edge attributes and output the GraphML file again. (Isn't it reproducible?)
For the time being, Japanese was displayed ...
--If you want to add other attributes in dot language, it seems good to refer to Qiita article here. --For the cooperation between NetworkX and PyGraphViz, refer to here. -Graphvis layout sample
Recommended Posts