V
V
Victor Incognito2020-09-10 15:49:52
Python
Victor Incognito, 2020-09-10 15:49:52

How to put a plot built in matplotlib into an interface created in Qt Designer?

Good day, comrades.

How can Qt Designer mark plot(s) to be plotted with matplotlib ?

I am trying to do like this:

1. Created a design in Qt Designer
2. Placed the slider and LCD display I needed (as I wanted).
3. Added a widget from the containers section.
4. Saved the design
5. Converted the design from .ui to .py
6. Stuck. How to be further? The graph, for example, I have built.


Thank you for attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-09-10
@sanya84

Here
is an example for you:

import sys
import random
import matplotlib
matplotlib.use('Qt5Agg')

from PyQt5 import QtCore, QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class MplCanvas(FigureCanvas):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
        self.setCentralWidget(self.canvas)

        n_data = 50
        self.xdata = list(range(n_data))
        self.ydata = [random.randint(0, 10) for i in range(n_data)]
        self.update_plot()

        self.show()

        # Setup a timer to trigger the redraw by calling update_plot.
        self.timer = QtCore.QTimer()
        self.timer.setInterval(100)
        self.timer.timeout.connect(self.update_plot)
        self.timer.start()

    def update_plot(self):
        # Drop off the first y element, append a new one.
        self.ydata = self.ydata[1:] + [random.randint(0, 10)]
        self.canvas.axes.cla()  # Clear the canvas.
        self.canvas.axes.plot(self.xdata, self.ydata, 'r')
        # Trigger the canvas to update and redraw.
        self.canvas.draw()


app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question