我有一個matplotlib圖形,我想在PyQt4下的兩個獨立窗口中重複使用它。我已經嘗試將小部件添加到兩者的佈局中,但隨後小部件從第一個中消失。除了創建兩個相同的圖並保持同步外,是否有任何方法可以實現這一點?在多個位置嵌入matplotlib小部件
4
A
回答
0
雖然它不是一個完美的解決方案,但matplotlib有一個內置的方法來保持兩個獨立圖的同步極限,滴答等。
例如
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 4 * np.pi, 100)
y = np.cos(x)
figures = [plt.figure() for _ in range(3)]
ax1 = figures[0].add_subplot(111)
axes = [ax1] + [fig.add_subplot(111, sharex=ax1, sharey=ax1) for fig in figures[1:]]
for ax in axes:
ax.plot(x, y, 'go-')
ax1.set_xlabel('test')
plt.show()
注意,當您放大所有的3個地塊將保持同步,相位等
有可能,雖然這樣做的更好的方法。
2
的問題是,你不能在同一Qt物件添加兩個型動物的父母部件,因爲在添加小部件的過程中,Qt還使重新設置父級的過程,做什麼你看:
.. 。窗口部件從第一個窗口消失[窗口] ...
所以解決方案是使兩個畫布共享相同的數字。 下面是一個示例代碼,這將顯示兩個主窗口,每個窗口都有兩個畫布,並且四個圖表將同步:
import sys
from PyQt4 import QtGui
import numpy as np
import numpy.random as rd
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.main_widget = QtGui.QWidget(self)
vbl = QtGui.QVBoxLayout(self.main_widget)
self._fig = Figure()
self.ax = self._fig.add_subplot(111)
#note the same fig for two canvas
self.fc1 = FigureCanvas(self._fig) #canvas #1
self.fc2 = FigureCanvas(self._fig) #canvas #1
self.but = QtGui.QPushButton(self.main_widget)
self.but.setText("Update") #for testing the sync
vbl.addWidget(self.fc1)
vbl.addWidget(self.fc2)
vbl.addWidget(self.but)
self.setCentralWidget(self.main_widget)
@property
def fig(self):
return self._fig
@fig.setter
def fig(self, value):
self._fig = value
#keep the same fig in both canvas
self.fc1.figure = value
self.fc2.figure = value
def redraw_plot(self):
self.fc1.draw()
self.fc2.draw()
qApp = QtGui.QApplication(sys.argv)
aw1 = ApplicationWindow() #window #1
aw2 = ApplicationWindow() #window #2
aw1.fig = aw2.fig #THE SAME FIG FOR THE TWO WINDOWS!
def update_plot():
'''Just a random plot for test the sync!'''
#note that the update is only in the first window
ax = aw1.fig.gca()
ax.clear()
ax.plot(range(10),rd.random(10))
#calls to redraw the canvas
aw1.redraw_plot()
aw2.redraw_plot()
#just for testing the update
aw1.but.clicked.connect(update_plot)
aw2.but.clicked.connect(update_plot)
aw1.show()
aw2.show()
sys.exit(qApp.exec_())
相關問題
- 1. 在GUI嵌入圖中使用matplotlib光標小部件
- 2. 使用matplotlib自定義小部件在pyqt4中嵌入matplotlib工具欄
- 3. 在wxPython中嵌入多個matplotlib圖形
- 4. 將Matplotlib作爲自定義小部件嵌入到PyQt中
- 5. 使用matplotlib在另一個軸中嵌入多個插入軸
- 6. 如何在嵌入在PyQt4小部件中的Axes3D(matplotlib)中啓用循環?
- 7. 位置android小部件
- 8. 商店小部件位置
- 9. QtDesigner4中的matplotlib小部件
- 10. Qtdesigner中的Matplotlib小部件
- 11. 嵌入matplotlib圖並改變其大小
- 12. 在位置獲取SWT小部件
- 13. 位置一個小部件在另一個頁腳,在wordpress中?
- 14. LinkedIn如何知道或跟蹤我嵌入其小部件的位置?
- 15. GridLayout + ScrollArea小部件位置大小後的位置
- 16. 嵌入動畫matplotlib在tk
- 17. 在PyQt中嵌入Matplotlib
- 18. 在PyQt4中嵌入matplotlib
- 19. 多個小部件:在小部件上啓動配置活動點擊
- 20. 如何在python(x,y)中配置matplotlib小部件
- 21. 在PyQt中嵌入Matplotlib與多個陰謀
- 22. FriendBuy嵌入的小部件不工作
- 23. 動態嵌入式Twitter小部件
- 24. 如何將Twitter小部件嵌入Reactjs?
- 25. Theming Twitter數字可嵌入小部件
- 26. Twitter的嵌入時間軸小部件
- 27. 將設置添加到可嵌入的角度小部件
- 28. 馬克谷歌地圖上的位置,並嵌入部件
- 29. PyQt小部件在多個文件
- 30. 將外部JavaScript庫嵌入到GWT中的單個位置