Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create independent MplWidget in Qt Designer using Python?
QtWS25 Last Chance

How to create independent MplWidget in Qt Designer using Python?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Piyush
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • P Piyush

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      1
      • jsulmJ jsulm

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

        P Offline
        P Offline
        Piyush
        wrote on last edited by
        #3

        @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?

        jsulmJ 1 Reply Last reply
        0
        • P Piyush

          @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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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.

            P Offline
            P Offline
            Piyush
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved