This is a very common problem, let’s say we draw a pie chart.
import matplotlib.pyplot as plt
data = [89.95.60]
labels = ['甲'.'乙'.'丙']
plt.pie(data, autopct='%.2f%%',labels=labels)
plt.legend()
plt.show()
Copy the code
The code looks normal, but none of the Chinese fonts’ a ‘, ‘b’, ‘C’ are displayed, and the fonts are represented by boxes.
In fact, just need to add a few lines of code, set the default font to solve the problem, in the same program to draw more pictures will not appear Chinese font display problem.
import matplotlib
# specify the default font
matplotlib.rcParams['font.sans-serif'] = [u'SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
Copy the code
The graph is as follows: