Python Matplotlib hangs when asked to plot a second chart (after closing first chart window)

后端 未结 6 1082
不知归路
不知归路 2021-02-14 18:09

Weird behaviour, I\'m sure it\'s me screwing up, but I\'d like to get to the bottom of what\'s happening:

I am running the following code to create a very simple graph

相关标签:
6条回答
  • 2021-02-14 18:27

    did you try:

    plt.close()
    

    to make sure you closed the plot object?

    0 讨论(0)
  • 2021-02-14 18:29

    Apparently, this is caused by a bug in the tkinter backend. See, e.g., https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/313834 . It's being worked on... If you can regress to a slightly older tkinter library, that should be a workaround for the time-being (I ran into this same thing a couple of weeks ago, and that was my only hope).

    0 讨论(0)
  • 2021-02-14 18:31

    Three months late to the party, but I found a suggestion in the matlibplot documentation to use draw() rather than show(); the former apparently just does a render of the current plot, while the latter starts up all the interactive tools, which is where the problems seem to start.

    It's not terribly prominently placed in the documentation, but here's the link: http://matplotlib.sourceforge.net/faq/howto_faq.html#use-show

    For what it's worth, I've tried pylab.show() and had exactly the same issue you did, while pylab.draw() seems to work fine if I just want to see the output.

    0 讨论(0)
  • 2021-02-14 18:36

    As posted somewhere above:

    Use plt.draw() for all your plots except the last one.

    For your last plot, use plt.show()

    It's weird, but if you don't use plt.show() in the last one and try plt.draw() instead, you don't see any plots.

    Good luck with this!

    0 讨论(0)
  • 2021-02-14 18:43

    Have you tried to use ipython instead of the standard python interpreter?

    You can install ipython with the following command:

    easy_install ipython
    

    and then, ipython has a specific mode to be ran with pylab, called -pylab:

    ipython -pylab
    
    In[1]: ...
    

    I think that most of the people use this solution to plot graphs with python, it is a command line similar to the one of R/Matlab, completition, etc... and it runs a separated thread for every plot so it shouldn't have the problem you have described.

    0 讨论(0)
  • 2021-02-14 18:44

    I had this problem when using TkAgg as the backend. After using plt.close('all') my computer froze.

    The solution was to switch to a different backend. I now use Qt4Agg instead.

    If you have Qt4Agg installed it is possible to switch backends by typing:

    plt.switch_backend('Qt4Agg')
    

    before plotting data

    0 讨论(0)
提交回复
热议问题