How to create independent MplWidget in Qt Designer using Python?



  • I need to create multiple chart under a tabWidget using independent MplWidgets.

    Currently, I am creating multiple charts in one single MplWidget. But, in this case whenever I need to move location of MplWidget, I also have to adjust the chart sizes as there are multiple charts in on widget. I am doing it in the following way (multiple charts in one MplWidget):

    from PyQt5 import QtWidgets
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
    import matplotlib.dates as mdates
    import matplotlib
    
    # Ensure using PyQt5 backend
    matplotlib.use('QT5Agg')
    
    # Matplotlib canvas class to create figure
    class MplCanvas(Canvas):
        def __init__(self):
            self.fig = Figure()
            
            self.ax1 = self.fig.add_subplot(211)
            self.ax2 = self.fig.add_subplot(212)
            self.ax3 = self.ax2.twinx()
             
            self.ax1.set_position([0.05, 0.8, 0.65, 0.17])
            self.ax2.set_position([0.05, 0.6, 0.65, 0.17])
            self.ax3.set_position([0.05, 0.61, 0.65, 0.17])
                    
            Canvas.__init__(self, self.fig)
            Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
            Canvas.updateGeometry(self)
    
    # Matplotlib widget
    class MplWidget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            QtWidgets.QWidget.__init__(self, parent)   # Inherit from QWidget
            self.canvas = MplCanvas()                  # Create canvas object
            self.vbl = QtWidgets.QVBoxLayout()         # Set box for plotting
            self.vbl.addWidget(self.canvas)
            self.setLayout(self.vbl)
    

    So can you suggest me how can I create multiple independent MplWidgets with only one chart in on MplWidget?


  • Moderators

    @Piyush I don't understand the problem. Just create several instances of MplWidget each showing one chart.



  • @jsulm I tried creating several instances of MplWidget with each showing one chart. However, all the instances started showing all the charts in them instead of one unique chart per instance.

    Could you share some example in python?


  • Moderators

    @Piyush What examples?
    I only saw parts of your code, so I don't know why you're showing several charts in each MplWidget instance.



  • @jsulm Examples as in different charts in different instances. Anyway I will try that again and post it here if it doesn't work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.