[英]Display y axis from 0 to 100 in Matplotlib plot
I wanted to display my data and wanted to set the y axis to show always from 0 to 100 or from 10 to 60.我想显示我的数据并想将 y 轴设置为始终显示从 0 到 100 或从 10 到 60。
I used "set_ylim()" and it worked with a small list but then I tried to put my data in that had datetime as x and a int as y and this didn't worked or the list is too long.我使用了“set_ylim()”,它适用于一个小列表,但后来我尝试将我的数据放入日期时间为 x 且 int 为 y 的数据中,但这不起作用或列表太长。
the data list looks like this: [ [ ], [ ], [ ] ] and the first list has datetime.datetime() the second is an int that mostly has 20,22 and 21 repeating and the third looks similiar to the second.数据列表如下所示: [ [ ], [ ], [ ] ] 并且第一个列表具有 datetime.datetime() 第二个是一个 int,其中大部分重复 20,22 和 21,第三个看起来类似于第二个。
using the small list with set_ylim: https://i.imgur.com/TinN6pT.png使用带有 set_ylim 的小列表: https://i.imgur.com/TinN6pT.png
with my data and set_ylim: https://i.imgur.com/uWPCYy6.png使用我的数据和 set_ylim: https://i.imgur.com/uWPCYy6.png
with my data working without set_ylim: https://i.imgur.com/wh4wPwp.png我的数据在没有 set_ylim 的情况下工作: https://i.imgur.com/wh4wPwp.png
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import matplotlib.dates as mdates
import numpy
import time
import datetime
#####read data####
data = ([datetime.datetime(2020, 5, 15, 7, 33, 35),
datetime.datetime(2020, 5, 15, 7, 33, 37),
datetime.datetime(2020, 5, 15, 7, 33, 39),
datetime.datetime(2020, 5, 15, 7, 33, 41),
datetime.datetime(2020, 5, 15, 7, 33, 43),
datetime.datetime(2020, 5, 15, 7, 33, 45)],
['20', '20', '22', '20', '20', '22'],
['30', '32', '31', '30', '32', '31'])
##################
#####plot data#####
fig = plt.figure()
#axis1#
ax1=fig.add_subplot(211)
#axis2#
ax2=fig.add_subplot(212)
def update(i):
#readData(data)
ax1.clear()
ax2.clear()
ax1.set_ylim((10,60))
ax2.set_ylim((0,100))
#ax1.plot([1,2], [20,30])
#ax2.plot([2,3],[2,3])
ax1.plot(data[0],data[1],color="red")
ax2.plot(data[0],data[2],color="blue")
#tweak style#
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
###################
a = anim.FuncAnimation(fig,update,repeat=False)
plt.show()
You are plotting strings - change them to ints您正在绘制字符串 - 将它们更改为整数
ax1.plot(data[0],[int(thing) for thing in data[1]],color="red")
ax2.plot(data[0],[int(thing) for thing in data[2]],color="blue")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.