Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. unable to update matplotlib figure in pyqt5
Forum Updated to NodeBB v4.3 + New Features

unable to update matplotlib figure in pyqt5

Scheduled Pinned Locked Moved Solved Qt for Python
19 Posts 2 Posters 2.5k Views 1 Watching
  • 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.
  • SGaistS SGaist

    Since your issue is solved, please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deem correct so other forum members may know a solution has been found :-)

    K Offline
    K Offline
    Kotaro
    wrote on last edited by
    #7

    @SGaist I’m sorry.
    I don't know where to fix the code.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #8

      Use self.forcemapWidget in onclick.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        Use self.forcemapWidget in onclick.

        K Offline
        K Offline
        Kotaro
        wrote on last edited by
        #9

        @SGaist

           def onclick(self,event):
                x,y=event.xdata,event.ydata
                forcemap=self.forcemapWidget (x)
        

        このようにしても更新できません。
        何回もすみません。

        SGaistS 1 Reply Last reply
        0
        • K Kotaro

          @SGaist

             def onclick(self,event):
                  x,y=event.xdata,event.ydata
                  forcemap=self.forcemapWidget (x)
          

          このようにしても更新できません。
          何回もすみません。

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @Kotaro you should add a method to the ForcemapWidget class that takes that x value and do the required update.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          0
          • SGaistS SGaist

            @Kotaro you should add a method to the ForcemapWidget class that takes that x value and do the required update.

            K Offline
            K Offline
            Kotaro
            wrote on last edited by
            #11

            @SGaist
            Isn't it def initUI in class ForcemapWidget?
            Is it wrong?
            maybe i don't understand the basics

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #12

              Sorry for being blunt but yes you are. Updating a widget by recreating it every time you want to change a value is wrong.

              initUI should be used once at construction time to put the various pieces in place. You should then have a separate function that take that x value and update the canvas content however you see fit. Such a method can also be called from the initUI method.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 2 Replies Last reply
              0
              • SGaistS SGaist

                Sorry for being blunt but yes you are. Updating a widget by recreating it every time you want to change a value is wrong.

                initUI should be used once at construction time to put the various pieces in place. You should then have a separate function that take that x value and update the canvas content however you see fit. Such a method can also be called from the initUI method.

                K Offline
                K Offline
                Kotaro
                wrote on last edited by
                #13

                @SGaist Thank you for your kindness
                I thought I could update it by recreating the widget each time I changed the value.
                How should I write the update function?
                Do you have any examples?

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Sorry for being blunt but yes you are. Updating a widget by recreating it every time you want to change a value is wrong.

                  initUI should be used once at construction time to put the various pieces in place. You should then have a separate function that take that x value and update the canvas content however you see fit. Such a method can also be called from the initUI method.

                  K Offline
                  K Offline
                  Kotaro
                  wrote on last edited by
                  #14

                  @SGaist
                  I changed it like this but it doesn't work.

                  class HeatmapWidget(QWidget):
                      def __init__(self):
                          super().__init__()
                          self.forcemapWidget=ForcemapWidget()
                          fig,ax=plt.subplots (figsize = (1,1))
                  
                          heatmap=np.random.rand (256,256)
                          im=ax.imshow (heatmap,cmap = "hot")
                          fig.colorbar (im)
                          ax.set_title ("Heatmap")
                          fig.canvas.mpl_connect ("button_press_event",self.onclick)
                  
                          canvas=FigureCanvas (fig)
                          layout=QVBoxLayout ()
                          layout.addWidget (canvas)
                          self.setLayout (layout)
                  
                      def onclick(self, event):
                          x, y = event.xdata, event.ydata
                          self.forcemapWidget.click(x)
                  
                  
                  class ForcemapWidget (QWidget):
                      def __init__(self):
                          super ().__init__ ()
                          self.figure=plt.figure ()
                          self.ax=self.figure.add_subplot (111)
                          heatmap=np.random.rand (256,256)
                          self.im=self.ax.imshow (heatmap,cmap = "hot")
                          canvas=FigureCanvas (self.figure)
                          layout=QVBoxLayout ()
                          layout.addWidget (canvas)
                          self.setLayout (layout)
                  
                      def click(self,x):
                          self.figure=plt.figure ()
                          self.ax=self.figure.add_subplot (111)
                          heatmap=np.random.rand (int (x),256)
                          self.im=self.ax.imshow (heatmap,cmap = "hot")
                          canvas=FigureCanvas (self.figure)
                          layout=QVBoxLayout ()
                          layout.addWidget (canvas)
                          self.setLayout (layout)
                          self.update()
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    You are rebuilding the whole widget and likely are getting warnings on the terminal.

                    Please take the time to learn how matplotlib works. What you have to do is update the plot and refresh the canvas.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    K 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      You are rebuilding the whole widget and likely are getting warnings on the terminal.

                      Please take the time to learn how matplotlib works. What you have to do is update the plot and refresh the canvas.

                      K Offline
                      K Offline
                      Kotaro
                      wrote on last edited by
                      #16

                      @SGaist I can understand by looking at the example that I am doing the mouse events in one class.
                      I'm confused because there is no example of mouse events between two classes
                      Do you have a simple example?

                      SGaistS 1 Reply Last reply
                      0
                      • K Kotaro

                        @SGaist I can understand by looking at the example that I am doing the mouse events in one class.
                        I'm confused because there is no example of mouse events between two classes
                        Do you have a simple example?

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        You handling the click event of your canvas, not multiple widgets. The fact that you called all methods click or onclick does not mean they are click event handler.

                        You really should use meaningful name for your methods and variables. That will make your code easier to follow and reason about.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        K 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          You handling the click event of your canvas, not multiple widgets. The fact that you called all methods click or onclick does not mean they are click event handler.

                          You really should use meaningful name for your methods and variables. That will make your code easier to follow and reason about.

                          K Offline
                          K Offline
                          Kotaro
                          wrote on last edited by Kotaro
                          #18

                          @SGaist
                          I made one that updates the color when clicked, but it doesn't.
                          Do you know why?

                          import numpy as np
                          import matplotlib.pyplot as plt
                          from PyQt5.QtWidgets import QVBoxLayout,QApplication,QWidget,QMainWindow,QMdiArea,QAction,QMdiSubWindow,QTextEdit, \
                             QComboBox,QLineEdit,QPushButton,QCheckBox,QFormLayout
                          import sys
                          from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                          
                          class MDIWindow (QMainWindow):
                             count=0
                             def __init__(self):
                                 super ().__init__ ()
                                 self.mdi=QMdiArea ()
                                 self.setCentralWidget (self.mdi)
                                 self.setWindowTitle ("MDI Application")
                                 sub2=QMdiSubWindow ()
                                 form_widget=ForcemapWidget ()
                                 sub2.setWidget (form_widget)
                                 sub2.setWindowTitle ("Sub Window 2")
                                 self.mdi.addSubWindow (sub2)
                                 sub2.show()
                                 sub3=QMdiSubWindow ()
                                 form_widget=HeatmapWidget ()
                                 sub3.setWidget (form_widget)
                                 sub3.setWindowTitle ("Sub Window 2")
                                 self.mdi.addSubWindow (sub3)
                                 sub3.show()
                          class HeatmapWidget(QWidget):
                             def __init__(self,parent=None):
                                 super().__init__()
                                 # self.forcemapWidget=ForcemapWidget()
                                 fig,ax=plt.subplots (figsize = (1,1))
                                 heatmap=np.random.rand (256,256)
                                 im=ax.imshow (heatmap,cmap = "hot")
                                 fig.colorbar (im)
                                 ax.set_title ("Heatmap")
                                 fig.canvas.mpl_connect ("button_press_event",self.onclick)
                                 canvas=FigureCanvas (fig)
                                 layout=QVBoxLayout ()
                                 layout.addWidget (canvas)
                                 self.setLayout (layout)
                             def onclick(self, event):
                                 self.force=ForcemapWidget()
                                 self.force.updates()
                          class ForcemapWidget(QWidget):
                             def __init__(self):
                                 super().__init__()
                                 self.figure = plt.figure(figsize=(5, 4))
                                 ax = self.figure.add_subplot(111)
                                 heatmap = np.random.rand(256, 256)
                                 self.im = ax.imshow(heatmap, cmap="Oranges")
                                 self.colorbar = self.figure.colorbar(self.im)
                                 canvas = FigureCanvas(self.figure)
                                 layout = QVBoxLayout()
                                 layout.addWidget(canvas)
                                 self.setLayout(layout)
                             def updates(self):
                                 self.im.set_cmap("Blues")
                                 self.figure.canvas.draw()
                          app = QApplication(sys.argv)
                          mdi = MDIWindow()
                          mdi.show()
                          app.exec_()
                          
                          K 1 Reply Last reply
                          0
                          • K Kotaro

                            @SGaist
                            I made one that updates the color when clicked, but it doesn't.
                            Do you know why?

                            import numpy as np
                            import matplotlib.pyplot as plt
                            from PyQt5.QtWidgets import QVBoxLayout,QApplication,QWidget,QMainWindow,QMdiArea,QAction,QMdiSubWindow,QTextEdit, \
                               QComboBox,QLineEdit,QPushButton,QCheckBox,QFormLayout
                            import sys
                            from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                            
                            class MDIWindow (QMainWindow):
                               count=0
                               def __init__(self):
                                   super ().__init__ ()
                                   self.mdi=QMdiArea ()
                                   self.setCentralWidget (self.mdi)
                                   self.setWindowTitle ("MDI Application")
                                   sub2=QMdiSubWindow ()
                                   form_widget=ForcemapWidget ()
                                   sub2.setWidget (form_widget)
                                   sub2.setWindowTitle ("Sub Window 2")
                                   self.mdi.addSubWindow (sub2)
                                   sub2.show()
                                   sub3=QMdiSubWindow ()
                                   form_widget=HeatmapWidget ()
                                   sub3.setWidget (form_widget)
                                   sub3.setWindowTitle ("Sub Window 2")
                                   self.mdi.addSubWindow (sub3)
                                   sub3.show()
                            class HeatmapWidget(QWidget):
                               def __init__(self,parent=None):
                                   super().__init__()
                                   # self.forcemapWidget=ForcemapWidget()
                                   fig,ax=plt.subplots (figsize = (1,1))
                                   heatmap=np.random.rand (256,256)
                                   im=ax.imshow (heatmap,cmap = "hot")
                                   fig.colorbar (im)
                                   ax.set_title ("Heatmap")
                                   fig.canvas.mpl_connect ("button_press_event",self.onclick)
                                   canvas=FigureCanvas (fig)
                                   layout=QVBoxLayout ()
                                   layout.addWidget (canvas)
                                   self.setLayout (layout)
                               def onclick(self, event):
                                   self.force=ForcemapWidget()
                                   self.force.updates()
                            class ForcemapWidget(QWidget):
                               def __init__(self):
                                   super().__init__()
                                   self.figure = plt.figure(figsize=(5, 4))
                                   ax = self.figure.add_subplot(111)
                                   heatmap = np.random.rand(256, 256)
                                   self.im = ax.imshow(heatmap, cmap="Oranges")
                                   self.colorbar = self.figure.colorbar(self.im)
                                   canvas = FigureCanvas(self.figure)
                                   layout = QVBoxLayout()
                                   layout.addWidget(canvas)
                                   self.setLayout(layout)
                               def updates(self):
                                   self.im.set_cmap("Blues")
                                   self.figure.canvas.draw()
                            app = QApplication(sys.argv)
                            mdi = MDIWindow()
                            mdi.show()
                            app.exec_()
                            
                            K Offline
                            K Offline
                            Kotaro
                            wrote on last edited by
                            #19

                            I understand this.
                            Here is the code as an example.

                            import numpy as np
                            import matplotlib.pyplot as plt
                            from PyQt5.QtWidgets import QVBoxLayout,QApplication,QWidget,QMainWindow,QMdiArea,QAction,QMdiSubWindow,QTextEdit, \
                               QComboBox,QLineEdit,QPushButton,QCheckBox,QFormLayout
                            import sys
                            from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                            
                            class MDIWindow (QMainWindow):
                               count=0
                               def __init__(self):
                                   super ().__init__ ()
                                   self.mdi=QMdiArea ()
                                   self.setCentralWidget (self.mdi)
                                   self.setWindowTitle ("MDI Application")
                                   sub2=QMdiSubWindow ()
                                   form_widget=ForcemapWidget ()
                                   sub2.setWidget (form_widget)
                                   sub2.setWindowTitle ("Sub Window 2")
                                   self.mdi.addSubWindow (sub2)
                                   sub2.show ()
                                   sub3 = QMdiSubWindow()
                                   form_widget = HeatmapWidget(forcemap_widget=form_widget)
                                   sub3.setWidget(form_widget)
                                   sub3.setWindowTitle("Sub Window 3")
                                   self.mdi.addSubWindow(sub3)
                                   sub3.show()
                            class HeatmapWidget(QWidget):
                               def __init__(self, forcemap_widget, parent=None):
                                   super().__init__()
                                   self.forcemap_widget = forcemap_widget  
                                   fig,ax=plt.subplots (figsize = (1,1))
                                   heatmap=np.random.rand (256,256)
                                   im=ax.imshow (heatmap,cmap = "hot")
                                   fig.colorbar (im)
                                   ax.set_title ("Heatmap")
                                   fig.canvas.mpl_connect ("button_press_event",self.onclick)
                                   self.canvas=FigureCanvas (fig)
                                   layout=QVBoxLayout ()
                                   layout.addWidget (self.canvas)
                                   self.setLayout (layout)
                               def onclick(self, event):
                                   x, y = event.xdata, event.ydata
                                   self.forcemap_widget.updates()  # 正
                            class ForcemapWidget(QWidget):
                               def __init__(self):
                                   super().__init__()
                                   self.figure = plt.figure(figsize=(5, 4))
                                   ax = self.figure.add_subplot(111)
                                   heatmap = np.random.rand(256, 256)
                                   self.im = ax.imshow(heatmap, cmap="Oranges")
                                   self.colorbar = self.figure.colorbar(self.im)
                                   self.canvas = FigureCanvas(self.figure)
                                   layout = QVBoxLayout()
                                   layout.addWidget(self.canvas)
                                   self.setLayout(layout)
                               def updates(self):
                                   self.im.set_cmap("Blues")
                                   self.figure.canvas.draw()
                                   # self.show()
                            app = QApplication(sys.argv)
                            mdi = MDIWindow()
                            mdi.show()
                            app.exec_()
                            
                            1 Reply Last reply
                            0
                            • K Kotaro has marked this topic as solved on

                            • Login

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