Can I prevent Spyder from displaying inline images temporarily?

纵饮孤独 提交于 2019-12-11 15:14:36

问题


In the Spyder IDE, I want to keep the inline console plotting (I don't want separate windows to spawn for each plot), but I want to programmatically disable plotting, i.e. in different cells.

In my workflow I need to plot a few simple graphs, and then generate figures and save them as video frames (many thousands). My frames are created by loading a jpg image, and then overlaying some annotation i.e.;

for jpg_path in path_list:
    img = mpl.image.imread(jpg_path)
    ax.imshow(img)
    ax.text(etc...)
    fig.savefig(etc...)

I want to keep the inline backend; %matplotlib inline.

But turn off plotting with something like plt.ioff().

But plt.ioff()only works with i.e. %matplotlib qt backend, not inline!

I've had several cases where I forget to change to %matplotlib qt (because it's not a python command and I have to enter it into the console seperately) and then plt.ioff() - resulting in 10000 images being posted in the console, freezing my machine.


回答1:


Ok I think I found an answer, thanks to this answer;

https://stackoverflow.com/a/46360516/789215

The key was the python command for the line magics get_ipython().run_line_magic('matplotlib', 'inline'). I created a context manager to wrap my video frame for-loop;

from IPython import get_ipython

class NoPlots:
    def __enter__(self):
        get_ipython().run_line_magic('matplotlib', 'qt')
        plt.ioff()
    def __exit__(self, type, value, traceback):
        get_ipython().run_line_magic('matplotlib', 'inline')
        plt.ion()

Or is there a better approach?



来源:https://stackoverflow.com/questions/52115896/can-i-prevent-spyder-from-displaying-inline-images-temporarily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!