如何修复 matplotlib y 轴数字不一致

How to fix matplotlib y axis numbers not consistant

我试图获得一个易于阅读的图表,但是当我用 matplotlib.pyplot 绘制它时,y 轴的读数与 x 轴的读数不一致(y 轴从 26 到 25 到 24 到 37,等等)我做错了什么!

temp = ['26', '26', '25', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24', '24']

hum = ['37', '38', '38', '39', '39', '40', '40', '40', '40', '40', '40', '40', '40', '41', '40', '39', '39', '39', '39', '39', '39', '39', '39']

time = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115]

plt.plot(time, temperature, color="green")
plt.plot(time, humidity, color="orange")
plt.xlabel("time (minutes)")
plt.ylabel("Temp/Humidity (C,%)")
plt.title("Temp and Humidity over time")
plt.show()

我的结果绘制正确,但在保持不变方面绘制不佳

你的数据读取为string你需要自己转换成int(或float)

temp = list(map(int, temp)) # change data from string to int
hum = list(map(int, hum))
plt.plot(time, temp, color="green")
plt.plot(time, hum, color="orange")
plt.axis([None, None, 0, 45])  # if you to set Y limits