0

I create a scatter plot, then plot it, save it to a file, load this file back and try to show the image. Why do I get an empty image?

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image

plt.scatter(x,y)
plt.show()

enter image description here

Then I save the image:

plt.savefig("./image.png")

Load it back:

img = mpimg.imread("./image.png")
img.shape
(288, 432, 4)

And when I plot it I get an a blank image, why?

plt.imshow(img)

enter image description here

dokondr
  • 3,389
  • 12
  • 38
  • 62
  • `plt.show()` erases the image. You can call `plt.savefig()` just earlier in the code. – JohanC Aug 12 '21 at 15:11
  • May I know why the need to save and re-upload it back? I assume you want to bypass something in order to do something on the step. Maybe a little explanation can help me to suggest a better alternative rather than save-reupload approach here – mpx Aug 12 '21 at 15:13
  • I also thought that `plt.show()` erases the image. But it is not the case. Moving it after `plt.savefig() ` does not help. – dokondr Aug 12 '21 at 15:16
  • I save it to the file to convert it to bitmap later with: `img.tobitmap()` – dokondr Aug 12 '21 at 15:26
  • Why do you think that `plt.show()` doesn't erase the image? Why do you think calling `plt.savefig()` before `plt.show()` wouldn't help? Did you test this rigorously? Did you erase the old file? Did you try with a new file name? – JohanC Aug 12 '21 at 15:33
  • I have tested this. `plt.scatter(x,y)` erases the image before `plt.show()` – dokondr Aug 12 '21 at 18:10

0 Answers0