Reputation: 77
I wanted to plot a graph in a PyQt5 window but it is completely black. I followed this tutorial and tried replacing my code with the static_canvas
the author wrote, but its still black. Complete code works fine, the problem arises when I only keep a single graph.
My code:
from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQT
from matplotlib.figure import Figure
import matplotlib.pyplot as pyp
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout_2.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.canvas = FigureCanvasQT(Figure())
self.horizontalLayout.addWidget(self.canvas)
self.ax = self.canvas.figure.subplots()
values = [i+1 for i in range(10)]
axe = [i for i in range(10)]
self.ax.plot(axe, values)
# pyp.show() doesn't do anything
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Regardless of what I try to plot, the result is the same black window. What is the issue?
Also, how do I go about linking a function that plots something to the FigureCanvasQT
? I want to embed a function from a different file to do and show the plot without having to write so much code in the window file.
Upvotes: 0
Views: 272
Reputation: 243945
In the tutorial that links FigureCanvasQT is not used but FigureCanvas. When analyzing the source code it is observed that FigureCanvasQT is FigureCanvas but when exporting(see here) it as _Backend more properties are added. So the solution is to change to:
from matplotlib.backends.backend_qt5agg import FigureCanvas
# ...
self.canvas = FigureCanvas(Figure())
# ...
Upvotes: 1