我下面有一些代码,通过将3组随机数加到一个图中(模拟从温度传感器收集的真实世界数据)。 我试图在同一块地上做两个比例尺。在
这里,y2List
是负数,这是我要为其创建第二个轴的数据集。我用这里的其他问题弄明白了怎么做。在
问题是,当添加每个数据点时,第二个y轴记号会再次显示,因此第二个y轴上的数字非常多。我可以通过在第二个y轴上设置一个限制来绕过这个问题,这将产生如下图像:
第二个y轴比其他轴稍暗,这是因为python在绘制每个点之后,在现有的y轴上绘制相同的数字(我可以看出,因为每个点绘制后,数字会变暗)
我的问题。。。有没有办法让第二个y轴只画第二个比例尺一次?这显然只是为了让情节更具美感,但每一点都有帮助!在
我的代码如下:
plt.ion() # enable interactivity
def makeFig():
ax.plot(xList, yList, color='blue', label='something1' if x == 0 else '')
ax.plot(xList, y1List, color='red', label='something2' if x == 0 else '')
ax2 = ax.twinx()
ax2.plot(xList, y2List, color='orange', label='something else' if x == 0 else '')
ax2.set_ylim(-20,0)
xList=list()
yList=list()
y1List=list()
y2List=list()
x=0
while x<11:
fig1=plt.figure(1)
ax = fig1.add_subplot(111)
x_1 = datetime.datetime.now()
date_formatter = DateFormatter('%H:%M:%S')
y=np.random.random()
y1=np.random.random() *3
y2=np.random.random() *(-13)
xList.append(x_1)
yList.append(y)
y1List.append(y1)
y2List.append(y2)
makeFig()
plt.gcf().autofmt_xdate()
ax = plt.gca()
ax.xaxis.set_major_formatter(date_formatter)
max_xticks = 10
xloc = plt.MaxNLocator(max_xticks)
ax.xaxis.set_major_locator(xloc)
plt.get_current_fig_manager().window.wm_geometry("940x700+5+0")
plt.draw()
plt.legend(loc=2, bbox_to_anchor=(1, 0.5), prop={'size':10})
x+=1
plt.pause(0.5)
您应该将图形和双轴的创建移动到循环之外。他们只需要做一次。在
具体来说,将}移出循环。在
fig1=plt.figure(1)
、ax = fig1.add_subplot(111)
和{相关问题 更多 >
编程相关推荐