在QWidget

时间:2015-07-26 22:49:40

标签: python matplotlib plot

我有一个QWidget,我用来托管一些matplotlib图。我有多个图(目前有2个图,但这会增加),但一次只显示一个图。我有一个超时事件,这样每5秒就可以删除当前的情节'从Widget中,下一个图表被添加到小部件中。这是我第一次使用matplotlib绘图,所以我不确定我的方法(下面)是否正确。

设置两个图:

#PLOT 1
Data = #inherited from another class
DataArray = #inherited from another class

DataPos = DataArray > 0
DataNeg = DataArray < 0

self.Figure1 = plt.figure('Figure1')
self.Figure1.patch.set_alpha(0)
self.Canvas1 = FigureCanvas(self.Figure1)
self.LayoutWidget.addWidget(self.Canvas1)

self.Ax1 = plt.subplot(1, 1, 1, axisbg='black')
self.Ax1.bar(np.arange(len(DataArray))[DataPos], DataArray[DataPos], width=1, color='blue')
self.Ax1.bar(np.arange(len(DataArray))[DataNeg], DataArray[DataNeg], width=1, color='red')
self.Ax1.tick_params(axis='y', colors='white')
plt.title('GRAPH TIME', color='w', fontsize=30, fontname='Sans Serif', fontweight='bold'
self.Figure1.tight_layout()
plt.close() #Not sure if/why I need this line?

#PLOT 2
Radius = [1, 2, 3, 4, 5, 6]
Area = [1, 4, 2, 4, 6, 8]
self.Figure2 = plt.figure('Figure2')
self.Figure2.patch.set_alpha(0)
self.Canvas2 = FigureCanvas(self.Figure2)

self.Ax2 = plt.subplot(1, 1, 1, axisbg='black')
self.Ax2.tick_params(axis='y', colors='white')
plt.title('GRAPH TITLE', colors='w', fontsize=30, fontname='Sans Serif', fontweight='bold')
self.Figure2.tight_layout()
plt.plot(Radius, Area, color='blue', linewidth=5)
plt.close() #Again, not sure if/why this is needed.

更改窗口小部件中的绘图的超时功能:

#if current plot = 1:
    self.LayoutWidget.removeWidget(self.Canvas1)
    self.LayoutWidget.addWidget(self.Canvas2)
    #This works as desired!

#elif current plot = 2:
    self.LayoutWidget.removeWidget(self.Canvas2)
    self.LayoutWidget.addWidget(self.Canvas1)
    #This doesn't work!

#Stop the timeout timer, then restart the timer, etc.

这是一种可接受的方法吗?

1 个答案:

答案 0 :(得分:0)

我在最后一个小时左右一直玩这个,并且已经实现了我想要的功能。我的原始代码中有一些东西需要更改。这是修改后的代码,可以按照需要运行。

设置两个图:

#PLOT 1
Data = #inherited from another class
DataArray = #inherited from another class

DataPos = DataArray > 0
DataNeg = DataArray < 0

self.Figure1 = plt.figure('Figure1')
self.Figure1.patch.set_alpha(0)
self.Canvas1 = FigureCanvas(self.Figure1)
self.LayoutWidget.addWidget(self.Canvas1)

self.Ax1 = plt.subplot(1, 1, 1, axisbg='black')
self.Ax1.bar(np.arange(len(DataArray))[DataPos], DataArray[DataPos], width=1, color='blue')
self.Ax1.bar(np.arange(len(DataArray))[DataNeg], DataArray[DataNeg], width=1, color='red')
self.Ax1.tick_params(axis='y', colors='white')
plt.title('GRAPH TIME', color='w', fontsize=30, fontname='Sans Serif', fontweight='bold'
self.Figure1.tight_layout()
self.Canvas1.show()

#PLOT 2
Radius = [1, 2, 3, 4, 5, 6]
Area = [1, 4, 2, 4, 6, 8]
self.Figure2 = plt.figure('Figure2')
self.Figure2.patch.set_alpha(0)
self.Canvas2 = FigureCanvas(self.Figure2)

self.Ax2 = plt.subplot(1, 1, 1, axisbg='black')
self.Ax2.tick_params(axis='y', colors='white')
plt.title('GRAPH TITLE', colors='w', fontsize=30, fontname='Sans Serif', fontweight='bold')
self.Figure2.tight_layout()
plt.plot(Radius, Area, color='blue', linewidth=5)

超时功能:

#if current plot = 1:
    self.Canvas1.close()
    self.LayoutWidget.removeWidget(self.Canvas1)
    self.LayoutWidget.addWidget(self.Canvas2)
    self.Canvas2.show()

#elif current plot = 2:
    self.Canvas2.close()
    self.LayoutWidget.removeWidget(self.Canvas2)
    self.LayoutWidget.addWidget(self.Canvas1)
    self.Canvas1.show()

#Stop the timeout timer, then restart the timer, etc.

简单!