Search code examples
pythonmatplotlibremote-accessfiguresavefiledialog

Python matplotlib saving figures


I am not sure if I could describe this situation clearly, but let's say I have a function imported from a library:

from somelib import somefunc
somefunc(someinputs)

If I run this code on my local machine, a figure would pop up and pause the code until I manually close it. For some reason I cannot run it on my local machine so I had to run it remotely via ssh session. In this case the figure doesn't pop up anymore, I wanted to find a way to save this figure as png file and so I could download it from my remote machine to see it. I cannot modify the source files of this library. Anyone could give me some suggestions? Thanks you!


Solution

  • You can use multiple ways depending upon your image..

    from PIL import Image  
    import PIL  
    
    # creating a image object (main image)  
    im1 = Image.open(r"C:\Users\System-Pc\Desktop\flower1.jpg")  
    
    # save a image using extension 
    im1 = im1.save("new.jpg") 
    

    using matplotlib

    from matplotlib import pyplot as plt
    plt.savefig('new.png')