原始代码:

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(squares, linewidth=3)
# 设置图表标题并给坐标轴加上标签
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)
# 设置刻度标志的大小
ax.tick_params(axis='both', labelsize=14)

plt.show()

画图结果:

matplotlib画图,中文乱码问题

 

 

可以看到我们的中文都是乱码。

乱码原因:matplotlib缺少中文配置导致的。

解决方案:在plt前边配置中文字体,具体如下:

# 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei']
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(squares, linewidth=3)
# 设置图表标题并给坐标轴加上标签
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)
# 设置刻度标志的大小
ax.tick_params(axis='both', labelsize=14)

plt.show()

matplotlib画图,中文乱码问题

 

相关文章:

  • 2022-12-23
  • 2022-02-28
  • 2021-10-30
  • 2021-09-04
  • 2022-12-23
  • 2021-08-19
  • 2021-10-04
  • 2022-12-23
猜你喜欢
  • 2021-05-24
  • 2022-01-06
  • 2021-09-27
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
相关资源
相似解决方案