老师,我尝试在图中显示图的标题和三个轴的标签,但是都不成功,请问是为什么?该怎么做呢?以下是代码和输出的图片。
from matplotlib import pyplot as plt
# 设置中文显示
plt.rcParams['font.sans-serif'] = 'SimHei'
# 设置显示符号(负号)
plt.rcParams['axes.unicode_minus'] = False # 不加这句的话,无法显示负号,还会输出警告
# 1.准备数据
data1 = [26, 26, 24, 24, 23, 23, 23, 23, 22, 23, 25, 25, 26, 27, 28, 28, 31, 31, 30, 30, 28, 26, 26, 25] # 一天24小时的温度
data2 = [70, 70, 86, 86, 87, 88, 88, 90, 95, 91, 86, 80, 73, 66, 63, 60, 58, 56, 54, 53, 56, 60, 67, 35] # 一天24小时的湿度
x = ['22:00', '23:00', '0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00']
# 2.创建画布
fig, ax1 = plt.subplots(figsize = (20,8))
# 3.绘制图像
ax1.plot(x,data1,label='温度',color='r')
# 4.设置共享x轴
ax2 = ax1.twinx()
ax2.plot(x, data2,label='湿度',color='b')
# 5.设置显示图例
fig.legend()
fig.tight_layout()
# 6.设置图的标题,显示三个轴的标签(以下4个都不行)
ax2.set_title = '某一天24h温度湿度变化'
ax1.set_xlabel = '时间'
ax1.set_ylabel = '温度'
ax2.set_ylabel = '湿度'
