Matplotlib ion() function fails to be interactive

前端 未结 2 442
时光取名叫无心
时光取名叫无心 2020-11-29 03:20

I have problem with interactive feature of Matplotlib. I ran the following program and received a freezing empty graph window.

import matplotlib.pyplot as pl         


        
相关标签:
2条回答
  • 2020-11-29 03:45

    I am having the exact same problem. In ipython there is the magic %matplotlib, which solved the problem for me. At least now I can type plt.figure() (assuming that import matplotlib.pyplot as plt has been called) and get a fully interactive responsive figure.

    However, I would still be interested to know what this magic imports exactly to be able to understand the problem.

    0 讨论(0)
  • 2020-11-29 03:46

    I bumped into this link found here, which answers my problem.

    It seems that after turning on interactive mode through plt.ion(), pyplot needs to be paused temporarily for it to update/redraw itself through plt.pause(0.0001). Here is what I did and it works!

    >>> import matplotlib.pyplot as plt
    >>> import numpy as np
    >>> plt.ion()
    >>> x = np.arange(0, 4*np.pi, 0.1)
    >>> y = [np.sin(i) for i in x]
    >>> plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)
    >>> plt.pause(0.0001)         
    >>> plt.plot(x, [i**2 for i in y], 'g-', linewidth=1.5, markersize=4)
    >>> plt.pause(0.0001)
    >>> plt.plot(x, [i**2*i+0.25 for i in y], 'r-', linewidth=1.5, markersize=4) 
    >>> plt.pause(0.0001)
    

    If you tried that in your IDLE console, notice that up to this point everything got displayed except that the graph window freezes and cannot exit. To unfreeze it type the following last statement

    >>> plt.show(block=True)
    

    Now the window can be closed.

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