Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to load png file as figure

I saved figure as png file:

fig, ax = plt.subplots()
...
fig.savefig("test.png")

Now I want to load this png to figure a plot.

I have tried with:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   fig, ax = plt.subplots()
   plt.imread("test.png")
   plt.show()

And I’m getting empty figure
enter image description here

How can I load this png and show it as plot ?

>Solution :

To load the PNG file and display it as a plot, you need to use the imshow function. Here’s an updated code snippet:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Save figure as PNG file
fig, ax = plt.subplots()
# ...
fig.savefig("test.png")

# Load PNG file and display as plot
fig, ax = plt.subplots()
img = mpimg.imread("test.png")
ax.imshow(img)
plt.show()

Here, we’re using matplotlib.image module to load the PNG file as an image and then displaying it using imshow function on the plot. This should display the image you saved as "test.png" within your plot.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading