Jak osadzić matplotlib w pyqt - dla manekinów
Obecnie próbuję osadzić wykres, który chcę narysować w interfejsie użytkownika pyqt4, który zaprojektowałem. Ponieważ jestem prawie zupełnie nowy w programowaniu - nie rozumiem, jak ludzie zrobili osadzenie w przykładach, które znalazłem - ten (na dole) i tamten.
Byłoby super, gdyby ktoś mógł opublikować wyjaśnienie krok po kroku lub przynajmniej bardzo mały, bardzo prosty kod tworzący np. wykres i przycisk w jednym GUI pyqt4.
3 answers
To wcale nie jest takie skomplikowane. Odpowiednie widżety Qt są w matplotlib.backends.backend_qt4agg
. FigureCanvasQTAgg
i NavigationToolbar2QT
są zazwyczaj tym, czego potrzebujesz. Są to zwykłe widżety Qt. Traktujesz je jak każdy inny widget. Poniżej bardzo prosty przykład z Figure
, Navigation
i jeden przycisk, który rysuje losowe DANE. Dodałem komentarze wyjaśniające różne rzeczy.
import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import random
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = Figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QtGui.QPushButton('Plot')
self.button.clicked.connect(self.plot)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
def plot(self):
''' plot some random stuff '''
# random data
data = [random.random() for i in range(10)]
# create an axis
ax = self.figure.add_subplot(111)
# discards the old graph
ax.clear()
# plot data
ax.plot(data, '*-')
# refresh canvas
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
Edit :
Zaktualizowano w celu uwzględnienia komentarzy i zmian w API.
-
NavigationToolbar2QTAgg
zmieniony przezNavigationToolbar2QT
- Importuj bezpośrednio
Figure
zamiastpyplot
- Zastąp przestarzałe
ax.hold(False)
przezax.clear()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-03 20:11:33
Poniżej znajduje się adaptacja poprzedniego kodu do użycia pod PyQt5 i Matplotlib 2.0 . Istnieje wiele małych zmian: struktura podmoduł PyQt, inny podmoduł z matplotlib, przestarzała metoda została zastąpiona...
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import random
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QPushButton('Plot')
self.button.clicked.connect(self.plot)
# set the layout
layout = QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
def plot(self):
''' plot some random stuff '''
# random data
data = [random.random() for i in range(10)]
# instead of ax.hold(False)
self.figure.clear()
# create an axis
ax = self.figure.add_subplot(111)
# discards the old graph
# ax.hold(False) # deprecated, see above
# plot data
ax.plot(data, '*-')
# refresh canvas
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-22 15:23:09
Dla tych, którzy szukają dynamicznego rozwiązania do osadzania Matplotlib w PyQt5 (nawet wykreślić dane za pomocą przeciągania i upuszczania). W PyQt5 musisz użyć super na głównej klasie okna, aby zaakceptować dropy. Funkcja dropevent może być użyta do uzyskania nazwy pliku, a reszta jest prosta:
def dropEvent(self,e):
"""
This function will enable the drop file directly on to the
main window. The file location will be stored in the self.filename
"""
if e.mimeData().hasUrls:
e.setDropAction(QtCore.Qt.CopyAction)
e.accept()
for url in e.mimeData().urls():
if op_sys == 'Darwin':
fname = str(NSURL.URLWithString_(str(url.toString())).filePathURL().path())
else:
fname = str(url.toLocalFile())
self.filename = fname
print("GOT ADDRESS:",self.filename)
self.readData()
else:
e.ignore() # just like above functions
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-10-02 01:23:02