[PYTHON] Visualize graphs with Japanese labeled edges in NetworkX and PyGraphviz / Gephi

Introduction

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.

Installation

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.

Edge properties

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.

Graphviz output

Drawing result

testgraph.png

AGraph print

['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];
}

Visualize with Gephi

Load and configure

  1. Launch Gephi and open the GraphML file (test.graphml in the above example) from Open. (If you added the extension during installation, you can double-click the GraphML file.)
  2. Click the Data Studio button and verify that the Label is displayed correctly from the Nodes and Edges tabs.
  3. Make the following settings on the "Settings" tab of the preview. --Check Node Label> Show Label`` --Node label> Fontto Japanese font --Checkedge 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 font
  4. Press the "Update" button.

Drawing result

Export 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 ...

exportedGephi.png

reference

--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

Visualize graphs with Japanese labeled edges in NetworkX and PyGraphviz / Gephi
Labeled graphs on NetworkX
Visualize keywords in documents with TF-IDF and Word Cloud
Eliminate garbled Japanese characters in Python library matplotlib and NetworkX
Dynamically generate graphs with matplotlib and embed in PDF with reporlab
Use directional graphs with networkx
Output networkX graph with graphviz (PyGraphviz)