matplotlib – iPython notebook:如何防止在imshow()上输出图像
我和matplotlib一起使用了很多i
Python笔记本,虽然很多情况下我很高兴它会在我调用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():
# 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,y_max)) ax.imshow(im2,aspect=1.5) raster = ax.imshow(points,origin='lower') fig.colorbar(raster) ax.set_title('coordinates plot') fig.savefig("fancy-annotated-map.png",bbox_inches=0) pylab.ion() >这假设您仅在IPython中使用该函数,否则始终导入pylab.更好地包装尝试…除了它周围所以该功能可以在别处使用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |