我能';在Matplotlib-Python中,不能同时使用多个活动绘图
这会引发轴不可下标的错误。 我有来自Arduino的恒定数据流。我应该试试多线程吗?有没有更简单、更有效的方法?我试着从不同的程序运行,效果非常好。 TIA我能';在Matplotlib-Python中,不能同时使用多个活动绘图,python,matplotlib,live-streaming,Python,Matplotlib,Live Streaming,这会引发轴不可下标的错误。 我有来自Arduino的恒定数据流。我应该试试多线程吗?有没有更简单、更有效的方法?我试着从不同的程序运行,效果非常好。 TIAplt.子地块(1,1)创建子地块的1×1网格,即只有一个地块。因此,返回的对象是轴本身,而不是数组中的轴。我怀疑您正在寻找plt.子图(1,2)或plt.子图(2,1),这取决于您希望轴的方向 而且,一旦修复了这个问题,您会发现代码中还有其他错误。您似乎将matplotlib.pyplot(plt)中的一些函数名与Axis对象上的方法混淆了
plt.子地块(1,1)
创建子地块的1×1网格,即只有一个地块。因此,返回的对象是轴本身,而不是数组中的轴。我怀疑您正在寻找plt.子图(1,2)
或plt.子图(2,1)
,这取决于您希望轴的方向
而且,一旦修复了这个问题,您会发现代码中还有其他错误。您似乎将matplotlib.pyplot
(plt
)中的一些函数名与Axis对象上的方法混淆了。例如,要设置y限制,可以使用plt.ylim(…)
或axes.set_ylim(…)
,但不能使用axes.ylim(…)
。我想当您试图设置ylim
、xlim
、标题
、xlabel
、和ylabel
时会遇到这个问题,如果我没记错的话,只要在每个属性的前面添加set.
,所有这些问题都可以解决
编辑:
另外,我注意到您在每个轴上设置了两次标题,第一次设置错误,第二次设置正确;你可能只想这么做一次。另外,还不确定
clf
和pause
调用(顺便说一下,这两个调用都必须是toplt
,而不是axes对象)发生了什么。调用这两个函数后,只需执行plt.show()
。谢谢!我使用暂停和clf进行交互式绘图
jsontime = [-9,-8,-7,-6,-5,-4,-3,-2,-1,0]
f, axarr = plt.subplots(1, 1)
ax1 = axarr[0, 0]
ax2 = axarr[0, 1]
def tempa_plot():
jsontempa = []
ay=[]
for k in range(100):
jsontempaval = random.randint(30, 80)
jsontempa.append(jsontempaval)
print(len(jsontempa))
for i in jsontempa:
ay.append(i)
if len(ay) > 10:
ay.pop(0)
ax1.ylim(30, 80) # Set y min and max values
ax1.xlim(-9, 1)
ax1.title('Live Streaming Sensor Data: Temperature')
ax1.grid(True)
ax1.ylabel('Temp C') # Set ylabels
ax1.xlabel('Time')
ax1.plot(jsontime,ay,label='Degrees C')
ax1.legend(loc='upper left') # plot the legend
ax1.pause(0.2)
ax1.clf()
ax1.set_title('Axis [0,0]')
def tempb_plot():
jsontempb = []
by=[]
for k in range(100):
jsontempaval = random.randint(50, 100)
jsontempb.append(jsontempaval)
print(len(jsontempb))
for i in jsontempb:
by.append(i)
if len(by) > 10:
by.pop(0)
print(by)
ax2.ylim(45, 105) # Set y min and max values
ax2.xlim(-9, 1)
ax2.title('Live Streaming Sensor Data: Temperature b')
ax2.grid(True)
ax2.ylabel('Temp C') # Set ylabels
ax2.xlabel('Time')
ax2.plot(jsontime,by,label='Degrees C')
ax2.legend(loc='upper left') # plot the legend
ax2.pause(0.2)
ax2.clf()
axarr[0, 1].set_title('Axis [0,0]')
tempa_plot()
tempb_plot()