Matplotlib (Pyplot) Savefig Outputs Blank Image

Matplotlib (pyplot) savefig outputs blank image

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.

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

  1. Call plt.savefig('tessstttyyy.png', dpi=100) before you call plt.show()

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

For example:

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

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

Matplotlib savefig exporting as blank image in colab

Ola @devman3211 o/

I'm a Google Colab user too :)

  • Have you check if the plot respect your condition?

  • If yes, try to change not sharpened_image is None for sharpened_image is not None.

You have to respect a certain order of priority, update your code considering the following:

#first generate your plot
fig1 = plt.imshow(#whatever you want to plot)

#then save it
plt.savefig('test.png', dpi=1000)

#finally .show() or .draw() it in this order
plt.show()
plt.draw()

Proof of work:
Sample Image

Result (for fun):
Sample Image

Matplotlib .savefig results in empty image

You must save it writing plt.savefig('path.pdf') BUT before plt.show(). I think it will work. Tell me.

pyplot saving blank image and plt.show() not working

Tried replicating your code in colab, It indeed works fine when run in a single cell.

The reason for getting blank output while segregating the code into different cells is:
The plot is generated as soon as the cell comprising plt.axis is run.
After that, all operations are done on the axis, whose axis? Is not specified so a new plotting window is created (this gives a blank plot).

I would advise you to run the plotting statements in a single cell, as colab shows output as soon as something related to matplotlib is run thereby making the axis of previous plot inaccessible. And all subsequent operations are run on a fresh figure.

plt.savefig saves a blank image

Well if you want to fix your problem input this in your code. I don't know exactly why it happens but it is fixed by this plt.show() should come before plt.savefig()

Explanation: plt.show() clears the whole thing, so anything afterwards will happen on a new empty figure.

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig(y_train, y_dev, y_test, y, plot_data_stripplot_distplot, dpi=100)

plt.savefig produces blank figure in Python

Try using the Figure object directly.

fig=plt.figure()
plt.plot(x,y)
fig.savefig('line_plot.png', dpi=100)

If you are interested in a PNG use '.png' instead of '.jpg'. Depending on the GUI toolkit you may need to add fig.show() before the last line.



Related Topics



Leave a reply



Submit