我创建了一个类,允许用户向MATPLOTLIB窗口添加多个图表。这些可以是折线图或条形图。它还具有这样一个特性:当一个图表已经添加到窗口中(从rowID中标识),而不是绘制新的绘图时,它将替换旧绘图中的数据。ie允许更新(动画)
这对于线条图来说很好,但是我在绘制几个条形图时会被破坏。课程看起来像:
import math
class TFrmPlot():
def __init__(self, point_lists, deleteCallback, plotType, rowID):
import matplotlib
matplotlib.interactive( True )
matplotlib.use( 'WXAgg' )
import matplotlib.pyplot as plt
self.plt = plt
self.fig = plt.figure()
self.fig.canvas.mpl_connect('close_event', self.on_close)
import matplotlib.axes as ax
self.ax = ax
self.deleteCallback = deleteCallback
self.chartArray = []
self.addChart(point_lists, plotType, rowID)
def close(self):
self.plt.close('all')
#self.fig.close()
def replaceChartDataIfChartExists(self, point_lists, rowID):
if rowID==0:
pass
for chart in self.chartArray:
for plot in chart.plots:
if plot.rowID == rowID:
plot.points = point_lists
if plot.plotType=="Point":
plot.plotItem.set_data(point_lists[0],point_lists[1])
chart.subPlot.draw_artist(plot.plotItem)
self.fig.canvas.blit(chart.subPlot.bbox)
else:
for rect, h in zip(plot.plotItem, point_lists[1]):
rect.set_height(h)
chart.subPlot.relim()
chart.subPlot.autoscale_view(True,True,True)
self.plt.draw()
return True
return False
def addChart(self, point_lists, plotType, rowID):
self.chartArray.append(TChart(rowID,plotType,point_lists))
self._drawAll()
def addPlot(self, point_lists, plotType, rowID):
chartNum = len(self.chartArray)
self.chartArray[chartNum-1].plots.append(TPlot(rowID,plotType,point_lists))
self._drawAll()
def on_close(self, event):
self.deleteCallback()
def _drawAll(self):
self.plt.clf()
numSubPlots = len(self.chartArray)
numCols = self._noCols(numSubPlots)
IndexConverter = TIndexConverter(numCols)
subPlot = None
for chartIndex in range(0,numSubPlots):
if numSubPlots==1:
subPlot = self.fig.add_subplot(1,1,1)
elif numSubPlots==2:
subPlot = self.fig.add_subplot(1,2,chartIndex+1)
else:
subPlot = self.fig.add_subplot(2,numCols,IndexConverter._getSubPlotIndex(chartIndex))
subPlot.relim()
subPlot.autoscale_view(True,True,True)
self.chartArray[chartIndex].subPlot = subPlot
self._drawSubs(self.chartArray[chartIndex])
self.plt.show()
def _drawSubs(self, chart):
for plot in chart.plots:
if plot.plotType=="Point":
chart.subPlot.plot(plot.points[0],plot.points[1])
plot.plotItem = chart.subPlot.lines[len(chart.subPlot.lines)-1]
else:
kwargs = {"alpha":0.5}
plot.plotItem = chart.subPlot.bar(plot.points[0],plot.points[1], width=self._calculateleastDiff(plot.points[0]), **kwargs)
def _noCols(self, numSubPlots):
return math.ceil(float(numSubPlots)/2.0)
def _calculateleastDiff(self, xValues):
xValues2 = sorted(xValues)
leastDiff = None
lastValue = None
for value in xValues2:
if lastValue is not None:
diff = value-lastValue
if leastDiff is None or diff < leastDiff:
leastDiff = diff
lastValue = value
return leastDiff
这有点长,所以总结一下:
addChart——基本上是添加一个新的子批次
addPlot——将新行或条添加到现有子批
replaceChartDataIfChartExists——如果ID已经存在,则刷新数据
我使用的虚拟数据只是连续地绘制一条正梯度和一条负梯度线。然而,我的绘图可能会进入一种状态,其中一个/部分或所有条形图都已损坏。它看起来像是x/y轴已经旋转了,个别的条不是从x轴开始的。这个问题是断断续续的,有时我会像预期的那样得到几个情节。一旦绘图被破坏,所有将来的更新都将保持损坏状态。在
根据要求,剩余代码:
^{pr2}$一些客户端代码:
def _updateData(self, state, data):
if self.plot is not None:
if not self.plot.replaceChartDataIfChartExists(data, state.comm.rowID):
if self.createNewChart == True:
self.plot.addChart(data, state.setting.plotType, state.comm.rowID)
else:
self.plot.addPlot(data, state.setting.plotType, state.comm.rowID)
这可能相关,也可能不相关,但您可以用以下内容替换
_calculateleastDiff
。在这段代码对于它所做的事情来说太复杂了。我怀疑您可以同时删除}类。我会保留一个数据列表列表(所以
TChart
和{[ [subplot1_data1,subplot1_data2],[subplot2_data1],[...]]
)、一个axes
对象的列表,以及一个跟踪所需绘图类型的内容列表。在另外,尽量不要使用matplotlib中已经使用的名称,这会使代码更难阅读。在
相关问题 更多 >
编程相关推荐