简体   繁体   中英

How to save a series of matplotlib plots as an image file?

I'm trying to save a series of matplotlib figures as one single image file with many slices. To put things in perspective, the following is the code I'm using:

for n in range(len(image.shape[0])): #this image here is a timelapse image
   plt.imshow(image[n, :, :], cmap='gray')
   ax = plt.gca()
     for acontour in contour_list:
         ax.add_patch(patches.Polygon(acontour[:, [1, 0]],linewidth=1,edgecolor='r',facecolor='none'))       
     plt.show()

I'm trying to overlay the corresponding contour on the original image for every slice and save all the images.

Thanks in advance.

Will this help:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2,len(image.shape[0])//2, figsize=(15, 6))
fig.subplots_adjust(hspace = .5, wspace=.001)
axs = axs.ravel()

for n in range(len(image.shape[0])):
    # Your plot code
    
plt.savefig('image.png')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM