matplotlib设置x、y轴标题

设置x轴标题

如下图红框处文字就是x轴标题

语法

matplotlib.pyplot.xlabel(xlabel, fontproperties,fontsize)
参数说明
  • xlabel:x轴显示的文字
  • fontproperties:设置字体,显示中文时,必须设置此项,否则乱码
  • fontsize:设置文字大小

例子

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

# 设置字体
my_font=fm.FontProperties(fname=r'C:\Windows\Fonts\msyh.ttc')

temps=[23,22,23,24,27,28,25,23]
hours=list(range(2,26,3))
plt.plot(hours,temps)

# 设置x轴标题
plt.xlabel('气 温',fontproperties=my_font,fontsize=18)

# 显示
plt.show()

设置y轴标题

语法

matplotlib.pyplot.ylabel(ylabel, fontproperties,fontsize)

使用同上


原文出处:/show_1EF3nPvQTbC3.html