我可以阻止Spyder暂时显示内嵌图像吗? [英] Can I prevent Spyder from displaying inline images temporarily?

查看:1075
本文介绍了我可以阻止Spyder暂时显示内嵌图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spyder IDE中,我想保留内联控制台绘图(我不希望为每个绘图生成单独的窗口),但我想以编程方式禁用绘图,即在不同的单元格中。

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.

在我的工作流程中,我需要绘制几个简单的图形,然后生成图形并将它们保存为视频帧(数千个)。我的帧是通过加载jpg图像,然后覆盖一些注释来创建的,即

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...)

我想保留内联后端; %matplotlib inline

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

但请关闭使用 plt.ioff()之类的绘图

plt.ioff()仅适用于ie %matplotlib qt 后端,而不是内联

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

我有几个案例,我忘记更改为%matplotlib qt (因为它不是python命令我必须单独将它输入控制台)然后 plt.ioff() - 导致在控制台中发布10000张图像,冻结我的机器。

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.

推荐答案

好的,我想我找到了答案,多亏了这个答案;

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

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

关键是线魔法的python命令 get_ipython()。run_line_magic('matplotlib','inline')。我创建了一个上下文管理器来包装我的视频帧for-loop;

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?

这篇关于我可以阻止Spyder暂时显示内嵌图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆