How To Solve Mac System Python Matplotlib Library Chinese Gibberish Problem

Google matplotlib library drawing generate Chinese messy code problem, the most popular answer is the following lines of code.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']

Obviously, this is because there is no SimHei font library on the MAC, so most tutorials tell you how to download SimHei fonts and how to put them into the MAC font library and how to configure the matplotlib font library, but most of these tutorials are a few years old, some have expired, some are too complicated.

So… Why bother? Just look under the MAC for any font libraries that support Chinese. Then I actually found Arial Unicode MS, which is available for beta testing. Below is the source code.

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']

If you do not believe you can use the following complete drawing code to make a try.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']

plt.figure()
names = ['5', '10', '15', '20', '25']
x = range(len(names))
y = [0.855, 0.84, 0.835, 0.815, 0.81]
y1=[0.86,0.85,0.853,0.849,0.83]
plt.plot(x, y, marker='o', mec='r', mfc='w',label=u'y=x^2The Graph')
plt.plot(x, y1, marker='*', ms=10,label=u'y=x^3The Graph')
plt.legend() # Let the legend take effect
plt.xticks(x, names, rotation=45)
plt.margins(0)
plt.subplots_adjust(bottom=0.15)
plt.xlabel(u"time(s)neighbor") #X axis labels
plt.ylabel("RMSE") #Y axis labels
plt.title("Fix chinese messy code problem") #Title
plt.show()