Get Jupyter notebook to display matplotlib figures in real-time





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







4















I have a long running Python loop (used for machine learning), which periodically prints output and displays figures (using matplotlib). When run in Jupyter Notebook, all the text (stdout) is displayed in real-time, but the figures are all queued and not displayed until the entire loop is done.



I'd like to see the figures in real-time, on each iteration of the loop. During cell execution, not when the entire cell execution is done.



For example, if my code is:



for i in range(10):
print(i)
show_figure(FIG_i)
do_a_10_second_calculation()


I currently see:



0
1
2
...
9
FIG_0
FIG_1
...
FIG_9


What I'd like is:



0
FIG_0
1
FIG_1
2
FIG_2
...


Most importantly, I'd like to see the figures as they are calculated, as opposed to not seeing any figures on the screen until the entire loop is done.










share|improve this question





























    4















    I have a long running Python loop (used for machine learning), which periodically prints output and displays figures (using matplotlib). When run in Jupyter Notebook, all the text (stdout) is displayed in real-time, but the figures are all queued and not displayed until the entire loop is done.



    I'd like to see the figures in real-time, on each iteration of the loop. During cell execution, not when the entire cell execution is done.



    For example, if my code is:



    for i in range(10):
    print(i)
    show_figure(FIG_i)
    do_a_10_second_calculation()


    I currently see:



    0
    1
    2
    ...
    9
    FIG_0
    FIG_1
    ...
    FIG_9


    What I'd like is:



    0
    FIG_0
    1
    FIG_1
    2
    FIG_2
    ...


    Most importantly, I'd like to see the figures as they are calculated, as opposed to not seeing any figures on the screen until the entire loop is done.










    share|improve this question

























      4












      4








      4








      I have a long running Python loop (used for machine learning), which periodically prints output and displays figures (using matplotlib). When run in Jupyter Notebook, all the text (stdout) is displayed in real-time, but the figures are all queued and not displayed until the entire loop is done.



      I'd like to see the figures in real-time, on each iteration of the loop. During cell execution, not when the entire cell execution is done.



      For example, if my code is:



      for i in range(10):
      print(i)
      show_figure(FIG_i)
      do_a_10_second_calculation()


      I currently see:



      0
      1
      2
      ...
      9
      FIG_0
      FIG_1
      ...
      FIG_9


      What I'd like is:



      0
      FIG_0
      1
      FIG_1
      2
      FIG_2
      ...


      Most importantly, I'd like to see the figures as they are calculated, as opposed to not seeing any figures on the screen until the entire loop is done.










      share|improve this question














      I have a long running Python loop (used for machine learning), which periodically prints output and displays figures (using matplotlib). When run in Jupyter Notebook, all the text (stdout) is displayed in real-time, but the figures are all queued and not displayed until the entire loop is done.



      I'd like to see the figures in real-time, on each iteration of the loop. During cell execution, not when the entire cell execution is done.



      For example, if my code is:



      for i in range(10):
      print(i)
      show_figure(FIG_i)
      do_a_10_second_calculation()


      I currently see:



      0
      1
      2
      ...
      9
      FIG_0
      FIG_1
      ...
      FIG_9


      What I'd like is:



      0
      FIG_0
      1
      FIG_1
      2
      FIG_2
      ...


      Most importantly, I'd like to see the figures as they are calculated, as opposed to not seeing any figures on the screen until the entire loop is done.







      python matplotlib jupyter-notebook real-time






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 16:32









      SRobertJamesSRobertJames

      2,46773272




      2,46773272
























          2 Answers
          2






          active

          oldest

          votes


















          2














          I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,



          %matplotlib inline

          import matplotlib.pyplot as plt

          def do_a_1_second_calculation():
          plt.pause(1)

          def show_figure(i):
          plt.figure(i)
          plt.plot([1,i,3])
          plt.show()

          for i in range(10):
          print(i)
          show_figure(i)
          do_a_1_second_calculation()


          results in the desired outcome



          enter image description here






          share|improve this answer
























          • Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

            – SRobertJames
            Nov 23 '18 at 17:38



















          1














          The display function from IPython.display can be used to immediately flush a figure to cell output. Assuming that FIG_i in your code is an actual Matplotlib figure object, you can just replace show_figure(FIG_i) with display(FIG_i) and the figures will output in real time.



          Here's a complete example of display in action:



          from matplotlib import pyplot as plt
          import numpy as np
          from IPython.display import display
          from time import sleep

          for eps in range(0, 11, 5):
          data = np.random.randint(eps, eps+10, size=(2,10))

          fig = plt.figure()
          ax = fig.gca()

          ax.plot(*data)

          print('eps %f' % eps)
          display(fig)
          plt.close() # .close prevents the normal figure display at end of cell execution

          sleep(2)
          print('slept 2 sec')


          Here's a screenshot of the output:



          enter image description here






          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',
            autoActivateHeartbeat: false,
            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%2f53450189%2fget-jupyter-notebook-to-display-matplotlib-figures-in-real-time%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









            2














            I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,



            %matplotlib inline

            import matplotlib.pyplot as plt

            def do_a_1_second_calculation():
            plt.pause(1)

            def show_figure(i):
            plt.figure(i)
            plt.plot([1,i,3])
            plt.show()

            for i in range(10):
            print(i)
            show_figure(i)
            do_a_1_second_calculation()


            results in the desired outcome



            enter image description here






            share|improve this answer
























            • Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

              – SRobertJames
              Nov 23 '18 at 17:38
















            2














            I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,



            %matplotlib inline

            import matplotlib.pyplot as plt

            def do_a_1_second_calculation():
            plt.pause(1)

            def show_figure(i):
            plt.figure(i)
            plt.plot([1,i,3])
            plt.show()

            for i in range(10):
            print(i)
            show_figure(i)
            do_a_1_second_calculation()


            results in the desired outcome



            enter image description here






            share|improve this answer
























            • Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

              – SRobertJames
              Nov 23 '18 at 17:38














            2












            2








            2







            I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,



            %matplotlib inline

            import matplotlib.pyplot as plt

            def do_a_1_second_calculation():
            plt.pause(1)

            def show_figure(i):
            plt.figure(i)
            plt.plot([1,i,3])
            plt.show()

            for i in range(10):
            print(i)
            show_figure(i)
            do_a_1_second_calculation()


            results in the desired outcome



            enter image description here






            share|improve this answer













            I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,



            %matplotlib inline

            import matplotlib.pyplot as plt

            def do_a_1_second_calculation():
            plt.pause(1)

            def show_figure(i):
            plt.figure(i)
            plt.plot([1,i,3])
            plt.show()

            for i in range(10):
            print(i)
            show_figure(i)
            do_a_1_second_calculation()


            results in the desired outcome



            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 17:30









            ImportanceOfBeingErnestImportanceOfBeingErnest

            142k13170248




            142k13170248













            • Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

              – SRobertJames
              Nov 23 '18 at 17:38



















            • Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

              – SRobertJames
              Nov 23 '18 at 17:38

















            Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

            – SRobertJames
            Nov 23 '18 at 17:38





            Aha! I didn't realize you need to explicitly do plt.show(). I had omitted that, and all the figures still displayed at the end, when the cell completed execution. Adding that in the loop indeed solves the problem.

            – SRobertJames
            Nov 23 '18 at 17:38













            1














            The display function from IPython.display can be used to immediately flush a figure to cell output. Assuming that FIG_i in your code is an actual Matplotlib figure object, you can just replace show_figure(FIG_i) with display(FIG_i) and the figures will output in real time.



            Here's a complete example of display in action:



            from matplotlib import pyplot as plt
            import numpy as np
            from IPython.display import display
            from time import sleep

            for eps in range(0, 11, 5):
            data = np.random.randint(eps, eps+10, size=(2,10))

            fig = plt.figure()
            ax = fig.gca()

            ax.plot(*data)

            print('eps %f' % eps)
            display(fig)
            plt.close() # .close prevents the normal figure display at end of cell execution

            sleep(2)
            print('slept 2 sec')


            Here's a screenshot of the output:



            enter image description here






            share|improve this answer






























              1














              The display function from IPython.display can be used to immediately flush a figure to cell output. Assuming that FIG_i in your code is an actual Matplotlib figure object, you can just replace show_figure(FIG_i) with display(FIG_i) and the figures will output in real time.



              Here's a complete example of display in action:



              from matplotlib import pyplot as plt
              import numpy as np
              from IPython.display import display
              from time import sleep

              for eps in range(0, 11, 5):
              data = np.random.randint(eps, eps+10, size=(2,10))

              fig = plt.figure()
              ax = fig.gca()

              ax.plot(*data)

              print('eps %f' % eps)
              display(fig)
              plt.close() # .close prevents the normal figure display at end of cell execution

              sleep(2)
              print('slept 2 sec')


              Here's a screenshot of the output:



              enter image description here






              share|improve this answer




























                1












                1








                1







                The display function from IPython.display can be used to immediately flush a figure to cell output. Assuming that FIG_i in your code is an actual Matplotlib figure object, you can just replace show_figure(FIG_i) with display(FIG_i) and the figures will output in real time.



                Here's a complete example of display in action:



                from matplotlib import pyplot as plt
                import numpy as np
                from IPython.display import display
                from time import sleep

                for eps in range(0, 11, 5):
                data = np.random.randint(eps, eps+10, size=(2,10))

                fig = plt.figure()
                ax = fig.gca()

                ax.plot(*data)

                print('eps %f' % eps)
                display(fig)
                plt.close() # .close prevents the normal figure display at end of cell execution

                sleep(2)
                print('slept 2 sec')


                Here's a screenshot of the output:



                enter image description here






                share|improve this answer















                The display function from IPython.display can be used to immediately flush a figure to cell output. Assuming that FIG_i in your code is an actual Matplotlib figure object, you can just replace show_figure(FIG_i) with display(FIG_i) and the figures will output in real time.



                Here's a complete example of display in action:



                from matplotlib import pyplot as plt
                import numpy as np
                from IPython.display import display
                from time import sleep

                for eps in range(0, 11, 5):
                data = np.random.randint(eps, eps+10, size=(2,10))

                fig = plt.figure()
                ax = fig.gca()

                ax.plot(*data)

                print('eps %f' % eps)
                display(fig)
                plt.close() # .close prevents the normal figure display at end of cell execution

                sleep(2)
                print('slept 2 sec')


                Here's a screenshot of the output:



                enter image description here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 17:05

























                answered Nov 23 '18 at 16:46









                teltel

                7,52921433




                7,52921433






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450189%2fget-jupyter-notebook-to-display-matplotlib-figures-in-real-time%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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy