Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matplotlib iPython笔记本:如何防止在imshow()上输出图像?_Matplotlib_Jupyter Notebook_Ipython - Fatal编程技术网

Matplotlib iPython笔记本:如何防止在imshow()上输出图像?

Matplotlib iPython笔记本:如何防止在imshow()上输出图像?,matplotlib,jupyter-notebook,ipython,Matplotlib,Jupyter Notebook,Ipython,我经常将iPython notebook与matplotlib结合使用,虽然在很多情况下,当我调用imshow()时,它会自动显示图像,但有时我想阻止这种行为 具体来说,我在一个非常大的数组上循环,并在matplotlib中为每个应该保存到磁盘上的元素生成一个图形。作为图形创建的一部分,我必须调用imshow()将现有图像(在我的例子中是地图的屏幕截图)绘制到轴上,以便稍后在轴上绘制其他材质。无论何时我调用imshow作为过程的一部分,最终的图形都会在iPython笔记本中内联显示,我如何防止这

我经常将iPython notebook与matplotlib结合使用,虽然在很多情况下,当我调用imshow()时,它会自动显示图像,但有时我想阻止这种行为

具体来说,我在一个非常大的数组上循环,并在matplotlib中为每个应该保存到磁盘上的元素生成一个图形。作为图形创建的一部分,我必须调用imshow()将现有图像(在我的例子中是地图的屏幕截图)绘制到轴上,以便稍后在轴上绘制其他材质。无论何时我调用imshow作为过程的一部分,最终的图形都会在iPython笔记本中内联显示,我如何防止这种情况发生

我的代码如下所示:

import matplotlib as plt
fig = plt.pyplot.figure(figsize=(20,20))
im2 = plt.pyplot.imread('/some/dir/fancy-map.png')

# Magic to calculate points, x_min etc.

fig.clf()
ax = fig.gca(xlim=(x_min, x_max), ylim=(y_min, y_max))
ax.imshow(im2, extent=(4, 4.5746, 42.5448, 43.3791), aspect=1.5)
raster = ax.imshow(points, vmin = 0, vmax=maxval, extent=(x_min, x_max, y_min, y_max), aspect=1.5, origin='lower')
fig.colorbar(raster)
ax.set_title('coordinates plot')

fig.savefig("fancy-annotated-map.png", bbox_inches=0)

尝试将其移动到函数中,并在函数开始时执行
pylab.ioff()
,最后返回到
pylab.ion()

  • 这假设您仅在IPython中使用该函数,或者始终导入
    pylab
    。最好将
    try
    包裹在它周围,这样该函数就可以在其他地方使用了

  • 查看一下(函数调用
    %%
    %%
    ,如
    %cpaste
    )。一个很好的方法是创建您自己的魔术,比如
    %imnoshow
    或其他东西,只包装调用
    imshow
    的部分,这样您就可以在线处理
    imshow
    输出,而不必查看输出。因为你的问题不涉及一般的绘图界面,而是具体的绘图界面,所以我不会在这里尝试实现它,但希望上面的链接足以让你在需要时实现一些东西

  • 另一种方法是按照您自己的说明进行设置,包括拥有一些特定的
    .py
    文件,其中包含您自己的导入和帮助器类定义等。然后将您自己的特殊绘图函数放置在那里,以便在启动时加载它们,并在IPython的全局范围内可用。我强烈推荐这一版本有几个原因:(a)实际上,您可以为自己的助手函数编写单元测试,如果您愿意,甚至可以在每次启动IPython时轻松地进行测试!(b) 这使您能够更轻松地进行版本控制并封装经常需要的助手函数的逻辑。(c) 您可以享受函数“就在那里”的好处,而无需将其变为神奇函数或稍后导入


  • 太好了,谢谢!现在直接使用了
    pylab.ioff()
    ,但肯定会考虑用它来实现iPython魔术功能
    # Obvs add arguments so you can pass in your data and plot choices.
    def make_img():
        pylab.ioff()
    
        import matplotlib as plt
        fig = plt.pyplot.figure(figsize=(20,20))
        im2 = plt.pyplot.imread('/some/dir/fancy-map.png')
    
        # Magic to calculate points, x_min etc.
    
        fig.clf()
        ax = fig.gca(xlim=(x_min, x_max), ylim=(y_min, y_max))
        ax.imshow(im2, extent=(4, 4.5746, 42.5448, 43.3791), aspect=1.5)
        raster = ax.imshow(points, 
                           vmin = 0, 
                           vmax=maxval, 
                           extent=(x_min, x_max, y_min, y_max), 
                           aspect=1.5, 
                           origin='lower')
        fig.colorbar(raster)
        ax.set_title('coordinates plot')
        fig.savefig("fancy-annotated-map.png", bbox_inches=0)
    
        pylab.ion()