如何用常规python脚本模拟jupyter笔记本单元来绘制多个图像并轻松查看它们?

如何用常规python脚本模拟jupyter笔记本单元来绘制多个图像并轻松查看它们?,python,matplotlib,wing-ide,Python,Matplotlib,Wing Ide,我倾向于在Jupyter笔记本和Wing IDE之间来回切换以调试代码。我喜欢Jupyter笔记本的地方在于我可以运行一个单元格并生成一系列图像,代码如下: import matplotlib.pyplot as plt for i in range(10): ...do stuff... plt.plot(image) plt.show() 所有图像在jupyter cell输出区域中一个接一个地显示出来,通过滚动该区域可以轻松查看 虽然我经常想进入调试器来编写新代码

我倾向于在Jupyter笔记本和Wing IDE之间来回切换以调试代码。我喜欢Jupyter笔记本的地方在于我可以运行一个单元格并生成一系列图像,代码如下:

import matplotlib.pyplot as plt
for i in range(10):
    ...do stuff...
    plt.plot(image)
    plt.show()
所有图像在jupyter cell输出区域中一个接一个地显示出来,通过滚动该区域可以轻松查看

虽然我经常想进入调试器来编写新代码或调试某些东西,但我还没有找到一种好的机制来执行类似的操作

对此有什么建议吗


一种选择是编写一个函数,将所有图像写入磁盘,然后使用照片查看应用程序查看这些图像,但我想知道是否还有其他方法。

可能需要使用wingdbstub,以便您可以在Jupyter的上下文中进行调试。这在中有详细描述--限制是您无法调试.ipynb文件中的代码,但您可以将大部分内容放入.py文件。

可能需要做的是使用wingdbstub,以便在Jupyter的上下文中进行调试。这在中有详细描述--限制是您无法调试.ipynb文件中的代码,但您可以将大部分内容放入.py文件中。

我找到了一种非常好的方法:

  • 使用非交互式后端以避免图像弹出
  • 用自己的函数my_plt_show()替换对plt.show()的所有调用,其中:

    • 调用plt.show()
    • 将每个fig保存到scratch目录,确保关闭fig
    • 在scratch目录中编写一个文件'all.html',以显示图像。这是要编写的all.html文件:
    
    



  • 在运行或调试之后,只需在浏览器中打开该all.html文件,它的行为几乎类似于jupyter单元格的输出

下面是我的示例代码:

I_AM_IN_JUPYTER = False
SCRATCH_IMAGE_DIR = 'C:\\Work\\ScratchImages'
SCRATCH_IMAGE_NUM = 0

if I_AM_IN_JUPYTER:
    get_ipython().magic('matplotlib inline')
else:
    # use non-interactive back-end to avoid images from popping up
    # See: http://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib-so-it-can-be
    from matplotlib import use
    use('Agg') 

# function to show a plot or write it to disk
def my_plt_show():
    global I_AM_IN_JUPYTER, SCRATCH_IMAGE_NUM, f_html
    plt.show()
    if I_AM_IN_JUPYTER == False:
        # at start
        if SCRATCH_IMAGE_NUM == 0:
            # clean out the scratch image dir
            files = glob.glob(SCRATCH_IMAGE_DIR+'\\*')
            for f in files:
                os.remove(f)  
            # open 'all.html' that displays all the images written
            f_html = open(SCRATCH_IMAGE_DIR+'\\all.html', 'w')
            f_html.write('<html>\n')

        # save all images to a scratch dir
        fname = 'img_{:04d}.jpg'.format(SCRATCH_IMAGE_NUM)
        plt.savefig(SCRATCH_IMAGE_DIR+'\\'+fname)
        fig = plt.gcf() # get reference to the current figure
        plt.close(fig)  # and close it
        f_html.write('<img src="'+fname+'" /> <br />\n') 
        f_html.flush() # flush it directly to disk, for debug purposes.        
        SCRATCH_IMAGE_NUM += 1    
I_AM_IN_JUPYTER=False
SCRATCH\u IMAGE\u DIR='C:\\Work\\ScratchImages'
划痕\u图像\u数量=0
如果我是朱庇特:
get_ipython().magic('matplotlib inline')
其他:
#使用非交互式后端以避免图像弹出
#见:http://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib-so-it-can-be
从matplotlib导入使用
使用('Agg')
#函数显示绘图或将其写入磁盘
定义我的节目():
全局I\u AM\u IN\u JUPYTER、SCRATCH\u IMAGE\u NUM、f\u html
plt.show()
如果I_AM_IN_JUPYTER==False:
#一开始
如果SCRATCH_IMAGE_NUM==0:
#清除划痕图像目录
files=glob.glob(SCRATCH\u IMAGE\u DIR+'\\\*')
对于文件中的f:
删除操作系统(f)
#打开显示所有已写入图像的“all.html”
f_html=open(SCRATCH_IMAGE_DIR+'\\all.html',w')
f_html.write('\n')
#将所有图像保存到临时目录
fname='img{:04d}.jpg'。格式(SCRATCH\u IMAGE\u NUM)
plt.savefig(SCRATCH\u IMAGE\u DIR+'\\'+fname)
fig=plt.gcf()#获取当前图形的参考
plt.关闭(图)#然后关闭它
f_html.write(“
\n”) f_html.flush()。 划痕图像数+=1
我想出了一个非常好的方法:

  • 使用非交互式后端以避免图像弹出
  • 用自己的函数my_plt_show()替换对plt.show()的所有调用,其中:

    • 调用plt.show()
    • 将每个fig保存到scratch目录,确保关闭fig
    • 在scratch目录中编写一个文件'all.html',以显示图像。这是要编写的all.html文件:
    
    



  • 在运行或调试之后,只需在浏览器中打开该all.html文件,它的行为几乎类似于jupyter单元格的输出

下面是我的示例代码:

I_AM_IN_JUPYTER = False
SCRATCH_IMAGE_DIR = 'C:\\Work\\ScratchImages'
SCRATCH_IMAGE_NUM = 0

if I_AM_IN_JUPYTER:
    get_ipython().magic('matplotlib inline')
else:
    # use non-interactive back-end to avoid images from popping up
    # See: http://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib-so-it-can-be
    from matplotlib import use
    use('Agg') 

# function to show a plot or write it to disk
def my_plt_show():
    global I_AM_IN_JUPYTER, SCRATCH_IMAGE_NUM, f_html
    plt.show()
    if I_AM_IN_JUPYTER == False:
        # at start
        if SCRATCH_IMAGE_NUM == 0:
            # clean out the scratch image dir
            files = glob.glob(SCRATCH_IMAGE_DIR+'\\*')
            for f in files:
                os.remove(f)  
            # open 'all.html' that displays all the images written
            f_html = open(SCRATCH_IMAGE_DIR+'\\all.html', 'w')
            f_html.write('<html>\n')

        # save all images to a scratch dir
        fname = 'img_{:04d}.jpg'.format(SCRATCH_IMAGE_NUM)
        plt.savefig(SCRATCH_IMAGE_DIR+'\\'+fname)
        fig = plt.gcf() # get reference to the current figure
        plt.close(fig)  # and close it
        f_html.write('<img src="'+fname+'" /> <br />\n') 
        f_html.flush() # flush it directly to disk, for debug purposes.        
        SCRATCH_IMAGE_NUM += 1    
I_AM_IN_JUPYTER=False
SCRATCH\u IMAGE\u DIR='C:\\Work\\ScratchImages'
划痕\u图像\u数量=0
如果我是朱庇特:
get_ipython().magic('matplotlib inline')
其他:
#使用非交互式后端以避免图像弹出
#见:http://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib-so-it-can-be
从matplotlib导入使用
使用('Agg')
#函数显示绘图或将其写入磁盘
定义我的节目():
全局I\u AM\u IN\u JUPYTER、SCRATCH\u IMAGE\u NUM、f\u html
plt.show()
如果I_AM_IN_JUPYTER==False:
#一开始
如果SCRATCH_IMAGE_NUM==0:
#清除划痕图像目录
files=glob.glob(SCRATCH\u IMAGE\u DIR+'\\\*')
对于文件中的f:
删除操作系统(f)
#打开显示所有已写入图像的“all.html”
f_html=open(SCRATCH_IMAGE_DIR+'\\all.html',w')
f_html.write('\n')
#将所有图像保存到临时目录
fname='img{:04d}.jpg'。格式(SCRATCH\u IMAGE\u NUM)
plt.savefig(SCRATCH\u IMAGE\u DIR+'\\'+fname)
fig=plt.gcf()#获取当前图形的参考
plt.关闭(图)#然后关闭它
f_html.write(“
\n”) f_html.flush()。 划痕图像数+=1
不是您所要求的,但spyder在jupyter小部件中提供了类似单元格的执行模型和内联图。不是您所要求的,但spyder在jupyter小部件中提供了类似单元格的执行模型和内联图。