Last time, I made a Python environment using pyenv-virtualenv on Mac, but Japanese characters were garbled with matplotlib and NetworkX that I put in at that time. I was in a state of being.
Both are famous libraries, so I don't think they would fit in if you installed them normally, but they are garbled in your environment. I tried to find a solution.
Sample code
matplotlib-utf8.py
import matplotlib.pyplot as plt
plt.text(0.2, 0.2, "Japanese", fontsize=50)
plt.show()
When you run this code
python matplotlib-utf8.py
The Japanese part is garbled like this.
This was resolved by specifying Japanese fonts in the .matplotlib / matplotlibrc file.
~/.matplotlib/matplotlibrc
font.family :Hiragino Kaku Gothic Pro
backend : TkAgg
In NetworkX, you can display text on the node with draw_networkx_labels, but even if you specify the font in matplotlib, the characters remain garbled.
This was resolved by specifying a Japanese font such as Hiragino Kakugo in the font_family parameter of draw_networkx_labels.
networkx-label.py
import matplotlib.pyplot as plt
import networkx as nx
G=nx.Graph()
G.add_edge(0,1)
G.add_edge(1,2)
G.add_edge(2,0)
G.add_edge(0,3)
pos=nx.spring_layout(G)
nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5)
labels={0:"zero",1:"First",2:"Two",3:"three"}
nx.draw_networkx_labels(G,pos,labels,font_size=16,font_family='Hiragino Kaku Gothic Pro')
plt.show()
When executed, it could be displayed in Japanese on NetworkX.
python networkx-label.py
Recommended Posts