如何解決matplotlib模組中的中文亂碼
阿新 • • 發佈:2020-11-12
如何解決matplotlib模組中的中文亂碼
核心程式碼
## 設定屬性防止中文亂碼 mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False
例子:
1.沒有上面程式碼的
import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np ## 設定屬性防止中文亂碼 # mpl.rcParams['font.sans-serif'] = [u'SimHei'] # mpl.rcParams['axes.unicode_minus'] = False fig,ax = plt.subplots() x = np.linspace(0,20,1000) ax.plot(x,np.cos(x)) ax.axis('equal') ax.annotate('最大值',xy = (6.28,1),xytext = (10,4),arrowprops = dict(facecolor = 'black',shrink = 0.05)) ax.annotate('最小值',xy = (5*np.pi,-1),xytext=(2,-6),arrowprops = dict(arrowstyle = "->",connectionstyle ="angle3,angleA = 0,angleB=-90" )) plt.show()
2.新增上面程式碼
import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np ## 設定屬性防止中文亂碼 mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False fig,ax = plt.subplots() x = np.linspace(0,20,1000) ax.plot(x,np.cos(x)) ax.axis('equal') ax.annotate('最大值',xy = (6.28,1),xytext = (10,4),arrowprops = dict(facecolor = 'black',shrink = 0.05)) ax.annotate('最小值',xy = (5*np.pi,-1),xytext=(2,-6),arrowprops = dict(arrowstyle = "->",connectionstyle ="angle3,angleA = 0,angleB=-90" )) plt.show()