[PYTHON] Draw a graph with networkx

= I made a graph using networkx of python, so make a note

make_graph.py



# -*- encoding:utf-8 -*-

import networkx
import pylab
from matplotlib import font_manager
from itertools import combinations
from random import randint

#A dict whose key is a node and the list of nodes that have an edge is a value.
vector = {}
persons = [u"Tanaka", u"Suzuki", u"Yamada", u"Kimura", u"Yoshioka"]
edge_labels = {}

for person in persons:
    # defaultdict(list)Do this to create a node instead
    vector[person] = []

for man_pair in combinations(persons, 2):
    man1, man2 = man_pair
    #Price the edges appropriately
    r = randint(1, 10)
    if r % 2:
        continue
    else:
        vector[man1].append(man2)
        edge_labels[(man1, man2)] = r

graph = networkx.Graph(vector)  #Undirected graph
# graph = network.DiGraph(vector)  #Directed graph(to_Can be converted to an undirected graph with undirected)
pylab.figure(figsize=(3, 4))  #Make it 3 inches wide and 4 inches long
pos = networkx.spring_layout(graph)  #Plot nicely
# pos = networkx.random_layout(graph)Anyway, you can plot at high speed



#Change font (font_change path as appropriate)
font_path = "/usr/share/fonts/japanese/TrueType/sazanami-gothic.ttf"
font_prop = font_manager.FontProperties(fname=font_path)
networkx.set_fontproperties(font_prop)

#Tweak the look
networkx.draw_networkx_nodes(graph, pos, node_size=100, node_color="w")
networkx.draw_networkx_edges(graph, pos, width=1)
networkx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels)
networkx.draw_networkx_labels(graph, pos, font_size=16, font_color="r")

pylab.xticks([])
pylab.yticks([])

pylab.show()
pylab.savefig("graph_networkx.png ")

Here, Japanese cannot be displayed unless the version of networkx is 1.5 or higher. Probably

ValueError: matplotlib display text must have all code points < 128 or use Unicode strings

I think you will get the error. Please apply the patch here to networkx / drawing / nx_pylab.py to display Japanese.

result

Like this

graph_networkx.png

*** Reference http://d.hatena.ne.jp/nishiohirokazu/20111121/1321849806 http://antibayesian.hateblo.jp/entry/20110828/1314491180

Recommended Posts

Draw a graph with NetworkX
Draw a graph with networkx
Draw a graph with Julia + PyQtGraph (2)
Draw a loose graph with matplotlib
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with pandas + XlsxWriter
Draw a graph with PySimple GUI
Draw a graph with PyQtGraph Part 1-Drawing
Draw a flat surface with a matplotlib 3d graph
Draw a graph with Japanese labels in Jupyter
How to draw a 2-axis graph with pyplot
Draw a graph with PyQtGraph Part 3-PlotWidget settings
[Python] Draw a directed graph with Dash Cytoscape
Draw a graph with PyQtGraph Part 4-PlotItem settings
Draw a graph with PyQtGraph Part 6-Displaying a legend
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
[Python] Draw a Qiita tag relationship diagram with NetworkX
[Python] How to draw a line graph with Matplotlib
Draw a graph with PyQtGraph Part 2--Detailed plot settings
Draw a beautiful circle with numpy
Let's make a graph with python! !!
Make a nice graph with plotly
Output networkX graph with graphviz (PyGraphviz)
Easily draw a map with matplotlib.basemap
Study math with Python: Draw a sympy (scipy) graph with matplotlib
[Visualization] I want to draw a beautiful graph with Plotly
[PyQt] Display a multi-axis graph with QtChart
How to draw a graph using Matplotlib
Draw a heart in Ruby with PyCall
Draw a Mandelbrot set with Brainf * ck
Count the maximum concatenated part of a random graph with NetworkX
Create a Connecting Nearest Neighbor with NetworkX
Simply draw a graph by specifying a file
Create a graph with borders removed with matplotlib
How to draw a bar graph that summarizes multiple series with matplotlib
Draw a graph that can be moved around with HoloViews and Bokeh
Draw a graph of a quadratic function in Python
How to draw a 3D graph before optimization
Draw a "breast curved surface" in a 3D graph (1)
A memo that made a graph animated with plotly
Try to draw a life curve with python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
I made a random number graph with Numpy
Draw multiple photos in a graph from multiple folders
NetworkX memo (directed graph)
A4 size with python-pptx
Band graph with matplotlib
NetworkX graph generator list
Decorate with a decorator
Draw graph in python
Draw a graph in Julia ... I tried a little analysis
I tried to draw a route map with Python
Visualize railway line data as a graph with Cytoscape 2
Forcibly draw something like a flowchart with Python, matplotlib
Note that there was a version problem with networkx
[Python] How to draw a scatter plot with Matplotlib
Draw Bezier curves with Go
Create a partial correlation matrix and draw an independent graph
Graph Excel data with matplotlib (1)
Learn librosa with a tutorial 1