这是多线程 python 脚本 (threading.thread) 的一部分,通过队列传输数据,当尝试绘制在 matplotlib 中使用 pyplot 收集的数据时出现错误。
整个脚本在以前的版本中工作;唯一的变化是我分别使用 pickle.dump 和 pickle.load 存储和加载数据数组。在这个调试版本中,没有启用加载。
这是失败的相关代码,由包含线程分解:
在主循环(线程)中:
elif kbcheck == 'p':
print "I KNOW YOU TYPED P" # Debug to see if plotting is triggered.
PLOTFLAGQ.put(1)
在数据处理线程中:
self.PLOTQL = [self.RAL, self.THD_avgL, self.VacL,
self.IacL, self.PacL, self.VdcL,
self.IdcL, self.PdcL, self.tempL]
if not self.PLOTFLAGQ.empty():
self.plotflag = self.PLOTFLAGQ.get()
self.PLOTQ.put(self.PLOTQL)
在绘图线程中:
if not self.PLOTQ.empty():
(self.RAL, self.THD_avgL, self.VacL, self.IacL,
self.PacL, self.VdcL, self.IdcL, self.PdcL,
self.TempL) = self.PLOTQ.get()
self.XaxisL = []
for i in range(len(self.VacL)):
self.XaxisL.append(i+1)
self.fig = pyplot.figure()
self.gs = gridspec.GridSpec(6,1, height_ratios = [1,2,2,2,2,2])
self.sT = pyplot.subplot(self.gs[0])
self.sT.yaxis.set_major_locator(pyplot.MaxNLocator(5))
self.sV = pyplot.subplot(self.gs[1])
self.sV.yaxis.set_major_locator(pyplot.MaxNLocator(10))
self.Va = self.sV.plot(self.XaxisL,self.VacL,
'-b',label = 'Voltage (AC)')
这是调试信息:
17
Here is the info in the Plot Thread
the length of VacL is: 17
[250.119, 250.156, 250.19, 250.193, 250.206, 250.158, 250.107, 250.103, 250.159, 250.156, 250.146, 250.093, 250.084, 250.095, 250.134, 250.0
35, 249.994]
the length of the x axis is: 17
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
上面的输出显示了采样的计数 (17),然后是 Vac 列表数组及其长度,然后是 x 轴数组(源自其他值的长度)。
这是错误:
Exception in thread Thread-5:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner
self.run()
File "C:\Projects\PythonScripts\Matthew_Threading_2_v0.3.3db.py", line 273, in run
self.Va = self.sV.plot(self.XaxisL,self.VacL,"-b",label = "Voltage AC")
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 3848, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 323, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 300, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 240, in _xy_from_xy
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
我已经重写了整个程序两次,并且只重写了保存线程(似乎破坏绘图的部分)两次并且无法弄清楚是什么导致了这个错误。
如果有人可以帮助我,将不胜感激。谢谢!