请教各位大神:
使用matplotlib来绘图的时候,把
plt.style.use('seaborn')
放在
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
前面时可以正常显示中文,但放在这两代码后面则无法显示中文。而把plt.style.use('seaborn')换成plt.style.use('bmh'),则无论放在什么位置都能正常显示中文。而同样还有其他类似情况,比如:plt.style.use('classic')与plt.style.use('seaborn')结果一致,而plt.style.use('seaborn-talk')则与plt.style.use('bmh')结果一致。
能正常显示中文的代码如下:
import matplotlib
import matplotlib.pyplot as plt
#设置字体
plt.style.use('seaborn')
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots()
ax.scatter(2, 4, s=200)
# 设置图表标题并给坐标轴加上标签。
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("值", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)
# 设置刻度标记的大小。
ax.tick_params(axis='both', which='major', labelsize=14)
plt.show()
# 结果显示图片如下: