我想显示我的数据,并希望将 y 轴设置为始终显示从 0 到 100 或从 10 到 60。
我使用了"set_ylim((",它适用于一个小列表,但后来我尝试将我的数据放入日期时间为 x 和 int 为 y 的数据中,但这不起作用或列表太长。
数据列表如下所示: [ [ ] , [ ] , [ ] ] 第一个列表有 datetime.datetime(( 第二个是 int,主要有 20,22 和 21 重复,第三个看起来与第二个相似。
将小列表与set_ylim一起使用:https://i.stack.imgur.com/i7MYL.png
使用我的数据和set_ylim:https://i.stack.imgur.com/2dGcL.png
使用我的数据无需set_ylim:https://i.stack.imgur.com/lRD7F.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()
您正在绘制字符串 - 将它们更改为整数
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")