Save plot to image file instead of displaying it using Matplotlib

前端 未结 20 1981
一生所求
一生所求 2020-11-22 06:07

I am writing a quick-and-dirty script to generate plots on the fly. I am using the code below (from Matplotlib documentation) as a starting point:

from pylab         


        
20条回答
  •  生来不讨喜
    2020-11-22 06:44

    You can save your image with any extension(png, jpg,etc.) and with the resolution you want. Here's a function to save your figure.

    import os
    
    def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
        path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension)
        print("Saving figure", fig_id)
        if tight_layout:
            plt.tight_layout()
        plt.savefig(path, format=fig_extension, dpi=resolution)
    

    'fig_id' is the name by which you want to save your figure. Hope it helps:)

提交回复
热议问题