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:
fig, ax = plt.subplots()
plt.imread("test.png")
plt.show()
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.
