ユーザーがMATPLOTLIBウィンドウに複数のチャートを追加できるようにするクラスを作成しました。これらは、折れ線グラフまたは棒グラフのいずれかです。また、チャートが(rowIDから識別されるように)ウィンドウにすでに追加されている場合、新しいプロットを描画するのではなく、古いプロットのデータを置き換えるという機能もあります。つまり、更新(アニメーション)が可能です
これは折れ線グラフではうまく機能しますが、いくつかの棒グラフをプロットすると破損します。クラスは次のようになります。
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がすでに存在する場合、データを更新します
私が使用しているダミーデータは、正の勾配と負の勾配線を連続してプロットするだけです。ただし、私のプロットは、バープロットの1つまたは一部またはすべてが破損した状態になる可能性があります。個々のバーがx軸から始まっていない状態で、x/y軸が回転しているように見えます。問題は断続的です。時々私は期待通りにいくつかのプロットを取得します。プロットが破損すると、それ以降のすべての更新は破損したままになります。
要求に応じて、残りのコード:
class TIndexConverter():
def __init__(self, numCols):
self.evenCounter = 0
self.oddCounter = numCols
def _getSubPlotIndex(self, arrayIndex):
if arrayIndex%2==0:
self.evenCounter += 1
return self.evenCounter
else:
self.oddCounter += 1
return self.oddCounter
class TChart():
def __init__(self, rowID, plotType, point_lists):
self.subPlot = None
self.plots = [TPlot(rowID, plotType, point_lists)]
class TPlot():
def __init__(self, rowID, plotType, point_lists):
self.plotItem = None
self.plotType = plotType
self.rowID = rowID
self.points = point_lists
いくつかのクライアントコード:
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)