Eliminate garbled Japanese characters in Python library matplotlib and NetworkX

Matplotlib and NetworkX are garbled in the environment created by pyenv-virtualenv

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.

Display Japanese with matplotlib

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

スクリーンショット 2016-02-08 18.24.33.png 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

スクリーンショット 2016-02-08 18.23.32.png

On the other hand, in NetworkX

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.

スクリーンショット 2016-02-08 18.13.08.png

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

スクリーンショット 2016-02-08 18.10.21.png

Postscript

Recommended Posts

Eliminate garbled Japanese characters in Python library matplotlib and NetworkX
Eliminate garbled Japanese characters in matplotlib graphs in Cloud Pak for Data Notebook
How to eliminate garbled characters in matplotlib output image
Eliminate garbled Japanese characters in JSON data acquired by API.
[Python] Read Japanese csv with pandas without garbled characters (and extract columns written in Japanese)
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
Windows Qt5.4 Python3.4 QProcess Japanese garbled characters
Create an image with characters in python (Japanese)
Fix garbled characters when handling Japanese in Requests
Japanese output in Python
I wrote python in Japanese
Seaborn, matplotlib garbled characters resolved in Windows10, Anaconda virtual environment
Overriding library functions in Python
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output
Stack and Queue in Python
Graph time series data in Python using pandas and matplotlib
Unittest and CI in Python
I understand Python in Japanese!
Get Japanese synonyms in Python
Count the number of Thai and Arabic characters well in Python
Visualize graphs with Japanese labeled edges in NetworkX and PyGraphviz / Gephi
Difference between list () and [] in Python
Difference between == and is in python
Installation of SciPy and matplotlib (Python)
[Python / matplotlib] Understand and use FuncAnimation
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Display characters like AA in python
Windows10: Install MeCab library in python
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Heatmap with Dendrogram in Python + matplotlib
Function synthesis and application in Python
Export and output files in Python
Make matplotlib Japanese compatible in 3 minutes
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
How to handle Japanese in Python
The first step in Python Matplotlib
Create and read messagepacks in Python
Fill the string with zeros in python and count some characters from the string
Unzip all Zips in Python folder (recursive) garbled characters (Qiita first post)
Detect Japanese characters from images using Google's Cloud Vision API in Python
Overlapping regular expressions in Python and Java
Display LaTeX notation formulas in Python, matplotlib
Japanese settings for matplotlib and Seaborn axes
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Displaying candlestick charts in Python (matplotlib edition)
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Comparison of Japanese conversion module in Python3