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']

names = ['5', '10', '15', '20', '25']
x = range(len(names))
y = [0.855, 0.84, 0.835, 0.815, 0.81]
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.xlabel(u"time(s)neighbor") #X axis labels
plt.ylabel("RMSE") #Y axis labels
plt.title("Fix chinese messy code problem") #Title
5 1 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest Most Voted
Inline Feedbacks
View all comments

thx, it works

Would love your thoughts, please comment.x