How to switch between diagrams with a button in Matplotlib











up vote
0
down vote

favorite












Is there an easy way, to switch between two or more diagrams with a button? I would like, for example be able to switch between these two diagrams with a button instead of showing them one after the other.



import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.bar(x_values, y_values)
plt.show()

x_values = [5, 4, 3, 2, 1]
y_values = [1, 4, 9, 16, 25]
plt.bar(x_values, y_values)
plt.show()


I know there are examples for this problem out there, but I simply can't get it to work...



Here is the link, where they show how to do it, but I don't understand how to do it...



Link: https://matplotlib.org/gallery/widgets/buttons.html










share|improve this question


























    up vote
    0
    down vote

    favorite












    Is there an easy way, to switch between two or more diagrams with a button? I would like, for example be able to switch between these two diagrams with a button instead of showing them one after the other.



    import matplotlib.pyplot as plt

    x_values = [1, 2, 3, 4, 5]
    y_values = [1, 4, 9, 16, 25]
    plt.bar(x_values, y_values)
    plt.show()

    x_values = [5, 4, 3, 2, 1]
    y_values = [1, 4, 9, 16, 25]
    plt.bar(x_values, y_values)
    plt.show()


    I know there are examples for this problem out there, but I simply can't get it to work...



    Here is the link, where they show how to do it, but I don't understand how to do it...



    Link: https://matplotlib.org/gallery/widgets/buttons.html










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Is there an easy way, to switch between two or more diagrams with a button? I would like, for example be able to switch between these two diagrams with a button instead of showing them one after the other.



      import matplotlib.pyplot as plt

      x_values = [1, 2, 3, 4, 5]
      y_values = [1, 4, 9, 16, 25]
      plt.bar(x_values, y_values)
      plt.show()

      x_values = [5, 4, 3, 2, 1]
      y_values = [1, 4, 9, 16, 25]
      plt.bar(x_values, y_values)
      plt.show()


      I know there are examples for this problem out there, but I simply can't get it to work...



      Here is the link, where they show how to do it, but I don't understand how to do it...



      Link: https://matplotlib.org/gallery/widgets/buttons.html










      share|improve this question













      Is there an easy way, to switch between two or more diagrams with a button? I would like, for example be able to switch between these two diagrams with a button instead of showing them one after the other.



      import matplotlib.pyplot as plt

      x_values = [1, 2, 3, 4, 5]
      y_values = [1, 4, 9, 16, 25]
      plt.bar(x_values, y_values)
      plt.show()

      x_values = [5, 4, 3, 2, 1]
      y_values = [1, 4, 9, 16, 25]
      plt.bar(x_values, y_values)
      plt.show()


      I know there are examples for this problem out there, but I simply can't get it to work...



      Here is the link, where they show how to do it, but I don't understand how to do it...



      Link: https://matplotlib.org/gallery/widgets/buttons.html







      python matplotlib button diagram






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 17:47









      Mysterious Challenger

      125




      125
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Here I modified code from the link you supplied so that it uses different sets of values depending on what values are currently plotted.



          import matplotlib.pyplot as plt
          from matplotlib.widgets import Button

          x1_values = [1, 2, 3, 4, 5]
          y1_values = [1, 4, 9, 16, 25]
          l, = plt.plot(x1_values, y1_values)


          class Index(object):
          def __init__(self):
          self.current = 1

          self.x1 = [5, 4, 3, 2, 1]
          self.y1 = [1, 4, 9, 16, 25]

          self.x2 = [1, 2, 3, 4, 5]
          self.y2 = [1, 4, 9, 16, 25]

          def plot(self, x):
          self.current += 1

          if self.current%2:
          self.values1()
          else:
          self.values2()

          def values1(self):
          l.set_xdata(self.x1)
          l.set_ydata(self.y1)
          plt.draw()

          def values2(self):
          l.set_xdata(self.x2)
          l.set_ydata(self.y2)
          plt.draw()

          callback = Index()
          axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
          bprev = Button(axnext, 'Switch')
          bprev.on_clicked(callback.plot)

          plt.show()





          share|improve this answer























          • Many thanks, much better to understand and to experiment with!
            – Mysterious Challenger
            Nov 10 at 18:11


















          up vote
          0
          down vote













          For those, who want to switch between bar- or plot diagrams (works for both types of diagram). I also think it is much cleaner to do it this way.



          import matplotlib.pyplot as plt
          from matplotlib.widgets import Button


          class Index(object):

          def start(self, event=None):
          ax.clear()
          x_values = [1, 2, 3, 4, 5]
          y_values = [3, 5, 2, 19, 1]
          ax.bar(x_values, y_values)
          plt.draw()

          def next(self, event):
          ax.clear()
          x_values = [1, 2, 3, 4, 5]
          y_values = [20, 15, 10, 5, 1]
          ax.bar(x_values, y_values)
          plt.draw()

          def prev(self, event):
          ax.clear()
          x_values = [1, 2, 3, 4, 5]
          y_values = [1, 5, 10, 15, 20]
          ax.bar(x_values, y_values)
          plt.draw()

          ax = plt.gca()
          callback = Index()
          callback.start()

          axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
          bprev = Button(axprev, 'Previous')
          bprev.on_clicked(callback.prev)
          axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
          bstart = Button(axstart, 'Start')
          bstart.on_clicked(callback.start)
          axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
          bnext = Button(axnext, 'Next')
          bnext.on_clicked(callback.next)

          plt.show()





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241759%2fhow-to-switch-between-diagrams-with-a-button-in-matplotlib%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Here I modified code from the link you supplied so that it uses different sets of values depending on what values are currently plotted.



            import matplotlib.pyplot as plt
            from matplotlib.widgets import Button

            x1_values = [1, 2, 3, 4, 5]
            y1_values = [1, 4, 9, 16, 25]
            l, = plt.plot(x1_values, y1_values)


            class Index(object):
            def __init__(self):
            self.current = 1

            self.x1 = [5, 4, 3, 2, 1]
            self.y1 = [1, 4, 9, 16, 25]

            self.x2 = [1, 2, 3, 4, 5]
            self.y2 = [1, 4, 9, 16, 25]

            def plot(self, x):
            self.current += 1

            if self.current%2:
            self.values1()
            else:
            self.values2()

            def values1(self):
            l.set_xdata(self.x1)
            l.set_ydata(self.y1)
            plt.draw()

            def values2(self):
            l.set_xdata(self.x2)
            l.set_ydata(self.y2)
            plt.draw()

            callback = Index()
            axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
            bprev = Button(axnext, 'Switch')
            bprev.on_clicked(callback.plot)

            plt.show()





            share|improve this answer























            • Many thanks, much better to understand and to experiment with!
              – Mysterious Challenger
              Nov 10 at 18:11















            up vote
            0
            down vote



            accepted










            Here I modified code from the link you supplied so that it uses different sets of values depending on what values are currently plotted.



            import matplotlib.pyplot as plt
            from matplotlib.widgets import Button

            x1_values = [1, 2, 3, 4, 5]
            y1_values = [1, 4, 9, 16, 25]
            l, = plt.plot(x1_values, y1_values)


            class Index(object):
            def __init__(self):
            self.current = 1

            self.x1 = [5, 4, 3, 2, 1]
            self.y1 = [1, 4, 9, 16, 25]

            self.x2 = [1, 2, 3, 4, 5]
            self.y2 = [1, 4, 9, 16, 25]

            def plot(self, x):
            self.current += 1

            if self.current%2:
            self.values1()
            else:
            self.values2()

            def values1(self):
            l.set_xdata(self.x1)
            l.set_ydata(self.y1)
            plt.draw()

            def values2(self):
            l.set_xdata(self.x2)
            l.set_ydata(self.y2)
            plt.draw()

            callback = Index()
            axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
            bprev = Button(axnext, 'Switch')
            bprev.on_clicked(callback.plot)

            plt.show()





            share|improve this answer























            • Many thanks, much better to understand and to experiment with!
              – Mysterious Challenger
              Nov 10 at 18:11













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Here I modified code from the link you supplied so that it uses different sets of values depending on what values are currently plotted.



            import matplotlib.pyplot as plt
            from matplotlib.widgets import Button

            x1_values = [1, 2, 3, 4, 5]
            y1_values = [1, 4, 9, 16, 25]
            l, = plt.plot(x1_values, y1_values)


            class Index(object):
            def __init__(self):
            self.current = 1

            self.x1 = [5, 4, 3, 2, 1]
            self.y1 = [1, 4, 9, 16, 25]

            self.x2 = [1, 2, 3, 4, 5]
            self.y2 = [1, 4, 9, 16, 25]

            def plot(self, x):
            self.current += 1

            if self.current%2:
            self.values1()
            else:
            self.values2()

            def values1(self):
            l.set_xdata(self.x1)
            l.set_ydata(self.y1)
            plt.draw()

            def values2(self):
            l.set_xdata(self.x2)
            l.set_ydata(self.y2)
            plt.draw()

            callback = Index()
            axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
            bprev = Button(axnext, 'Switch')
            bprev.on_clicked(callback.plot)

            plt.show()





            share|improve this answer














            Here I modified code from the link you supplied so that it uses different sets of values depending on what values are currently plotted.



            import matplotlib.pyplot as plt
            from matplotlib.widgets import Button

            x1_values = [1, 2, 3, 4, 5]
            y1_values = [1, 4, 9, 16, 25]
            l, = plt.plot(x1_values, y1_values)


            class Index(object):
            def __init__(self):
            self.current = 1

            self.x1 = [5, 4, 3, 2, 1]
            self.y1 = [1, 4, 9, 16, 25]

            self.x2 = [1, 2, 3, 4, 5]
            self.y2 = [1, 4, 9, 16, 25]

            def plot(self, x):
            self.current += 1

            if self.current%2:
            self.values1()
            else:
            self.values2()

            def values1(self):
            l.set_xdata(self.x1)
            l.set_ydata(self.y1)
            plt.draw()

            def values2(self):
            l.set_xdata(self.x2)
            l.set_ydata(self.y2)
            plt.draw()

            callback = Index()
            axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
            bprev = Button(axnext, 'Switch')
            bprev.on_clicked(callback.plot)

            plt.show()






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 19:06









            Mysterious Challenger

            125




            125










            answered Nov 10 at 18:02









            Filip Młynarski

            33917




            33917












            • Many thanks, much better to understand and to experiment with!
              – Mysterious Challenger
              Nov 10 at 18:11


















            • Many thanks, much better to understand and to experiment with!
              – Mysterious Challenger
              Nov 10 at 18:11
















            Many thanks, much better to understand and to experiment with!
            – Mysterious Challenger
            Nov 10 at 18:11




            Many thanks, much better to understand and to experiment with!
            – Mysterious Challenger
            Nov 10 at 18:11












            up vote
            0
            down vote













            For those, who want to switch between bar- or plot diagrams (works for both types of diagram). I also think it is much cleaner to do it this way.



            import matplotlib.pyplot as plt
            from matplotlib.widgets import Button


            class Index(object):

            def start(self, event=None):
            ax.clear()
            x_values = [1, 2, 3, 4, 5]
            y_values = [3, 5, 2, 19, 1]
            ax.bar(x_values, y_values)
            plt.draw()

            def next(self, event):
            ax.clear()
            x_values = [1, 2, 3, 4, 5]
            y_values = [20, 15, 10, 5, 1]
            ax.bar(x_values, y_values)
            plt.draw()

            def prev(self, event):
            ax.clear()
            x_values = [1, 2, 3, 4, 5]
            y_values = [1, 5, 10, 15, 20]
            ax.bar(x_values, y_values)
            plt.draw()

            ax = plt.gca()
            callback = Index()
            callback.start()

            axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
            bprev = Button(axprev, 'Previous')
            bprev.on_clicked(callback.prev)
            axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
            bstart = Button(axstart, 'Start')
            bstart.on_clicked(callback.start)
            axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
            bnext = Button(axnext, 'Next')
            bnext.on_clicked(callback.next)

            plt.show()





            share|improve this answer

























              up vote
              0
              down vote













              For those, who want to switch between bar- or plot diagrams (works for both types of diagram). I also think it is much cleaner to do it this way.



              import matplotlib.pyplot as plt
              from matplotlib.widgets import Button


              class Index(object):

              def start(self, event=None):
              ax.clear()
              x_values = [1, 2, 3, 4, 5]
              y_values = [3, 5, 2, 19, 1]
              ax.bar(x_values, y_values)
              plt.draw()

              def next(self, event):
              ax.clear()
              x_values = [1, 2, 3, 4, 5]
              y_values = [20, 15, 10, 5, 1]
              ax.bar(x_values, y_values)
              plt.draw()

              def prev(self, event):
              ax.clear()
              x_values = [1, 2, 3, 4, 5]
              y_values = [1, 5, 10, 15, 20]
              ax.bar(x_values, y_values)
              plt.draw()

              ax = plt.gca()
              callback = Index()
              callback.start()

              axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
              bprev = Button(axprev, 'Previous')
              bprev.on_clicked(callback.prev)
              axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
              bstart = Button(axstart, 'Start')
              bstart.on_clicked(callback.start)
              axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
              bnext = Button(axnext, 'Next')
              bnext.on_clicked(callback.next)

              plt.show()





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                For those, who want to switch between bar- or plot diagrams (works for both types of diagram). I also think it is much cleaner to do it this way.



                import matplotlib.pyplot as plt
                from matplotlib.widgets import Button


                class Index(object):

                def start(self, event=None):
                ax.clear()
                x_values = [1, 2, 3, 4, 5]
                y_values = [3, 5, 2, 19, 1]
                ax.bar(x_values, y_values)
                plt.draw()

                def next(self, event):
                ax.clear()
                x_values = [1, 2, 3, 4, 5]
                y_values = [20, 15, 10, 5, 1]
                ax.bar(x_values, y_values)
                plt.draw()

                def prev(self, event):
                ax.clear()
                x_values = [1, 2, 3, 4, 5]
                y_values = [1, 5, 10, 15, 20]
                ax.bar(x_values, y_values)
                plt.draw()

                ax = plt.gca()
                callback = Index()
                callback.start()

                axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
                bprev = Button(axprev, 'Previous')
                bprev.on_clicked(callback.prev)
                axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
                bstart = Button(axstart, 'Start')
                bstart.on_clicked(callback.start)
                axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
                bnext = Button(axnext, 'Next')
                bnext.on_clicked(callback.next)

                plt.show()





                share|improve this answer












                For those, who want to switch between bar- or plot diagrams (works for both types of diagram). I also think it is much cleaner to do it this way.



                import matplotlib.pyplot as plt
                from matplotlib.widgets import Button


                class Index(object):

                def start(self, event=None):
                ax.clear()
                x_values = [1, 2, 3, 4, 5]
                y_values = [3, 5, 2, 19, 1]
                ax.bar(x_values, y_values)
                plt.draw()

                def next(self, event):
                ax.clear()
                x_values = [1, 2, 3, 4, 5]
                y_values = [20, 15, 10, 5, 1]
                ax.bar(x_values, y_values)
                plt.draw()

                def prev(self, event):
                ax.clear()
                x_values = [1, 2, 3, 4, 5]
                y_values = [1, 5, 10, 15, 20]
                ax.bar(x_values, y_values)
                plt.draw()

                ax = plt.gca()
                callback = Index()
                callback.start()

                axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
                bprev = Button(axprev, 'Previous')
                bprev.on_clicked(callback.prev)
                axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
                bstart = Button(axstart, 'Start')
                bstart.on_clicked(callback.start)
                axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
                bnext = Button(axnext, 'Next')
                bnext.on_clicked(callback.next)

                plt.show()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 19 hours ago









                Mysterious Challenger

                125




                125






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241759%2fhow-to-switch-between-diagrams-with-a-button-in-matplotlib%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues