Matplotlib(pyplot)savefig输出空白图像 [英] Matplotlib (pyplot) savefig outputs blank image

查看:1736
本文介绍了Matplotlib(pyplot)savefig输出空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib保存我的图表;但是,图像保存为空白。

I am trying to save plots I make using matplotlib; however, the images are saving blank.

这是我的代码:

plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)

plt.subplot(122)
y = copy.deepcopy(tumorStack)
y = np.ma.masked_where(y == 0, y)

plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')

if T0 is not None:
    plt.subplot(123)
    plt.imshow(T0, cmap=mpl.cm.bone)

    #plt.subplot(124)
    #Autozoom

#else:
    #plt.subplot(124)
    #Autozoom

plt.show()
plt.draw()
plt.savefig('tessstttyyy.png', dpi=100)

并且tessstttyyy.png为空(也尝试使用.jpg)

And tessstttyyy.png is blank (also tried with .jpg)

推荐答案

首先,当 T0不是无时会发生什么?我会测试一下,然后我会调整传递给 plt.subplot()的值;也许尝试值131,132和133,或取决于 T0 是否存在的值。

First, what happens when T0 is not None? I would test that, then I would adjust the values I pass to plt.subplot(); maybe try values 131, 132, and 133, or values that depend whether or not T0 exists.

其次,调用 plt.show()后,将创建一个新图。要处理这个问题,你可以

Second, after plt.show() is called, a new figure is created. To deal with this, you can


  1. 致电 plt.savefig('tessstttyyy.png',dpi = 100)在你打电话 plt.show()

保存之前在 show()之前的数字,通过调用 plt.gcf()获取获取当前数字,然后你可以随时在此对象上调用 savefig()

Save the figure before you show() by calling plt.gcf() for "get current figure", then you can call savefig() on this Figure object at any time.

例如:

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('tessstttyyy.png', dpi=100)

在你的代码中,'tesssttyyy.png'是空白的,因为它保存了新的数字,没有绘制任何内容。

In your code, 'tesssttyyy.png' is blank because it is saving the new figure, to which nothing has been plotted.

这篇关于Matplotlib(pyplot)savefig输出空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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