Matplotlib - How to plot a high resolution graph?

前端 未结 6 1706
情深已故
情深已故 2020-12-07 13:16

I\'ve used matplotlib for plotting some experimental results (discussed it in here: Looping over files and plotting. However, saving the picture by clicking right to the ima

相关标签:
6条回答
  • 2020-12-07 13:27

    At the end of your for() loop, you can use the savefig() function instead of plt.show() and set the name, dpi and format of your figure.

    E.g. 1000 dpi and eps format are quite a good quality, and if you want to save every picture at folder ./ with names 'Sample1.eps', 'Sample2.eps', etc. you can just add the following code:

    for fname in glob("./*.txt"):
        # Your previous code goes here
        [...]
    
        plt.savefig("./{}.eps".format(fname), bbox_inches='tight', format='eps', dpi=1000)
    
    0 讨论(0)
  • 2020-12-07 13:29

    For future readers who found this question while trying to save high resolution images from matplotlib as I am, I have tried some of the answers above and elsewhere, and summed them up here.

    Best result: plt.savefig('filename.pdf')

    and then converting this pdf to a png on the command line so you can use it in powerpoint:

    pdftoppm -png -r 300 filename.pdf filename

    Less successful test #1: plt.savefig('filename.png', dpi=300)

    This does save the image at a bit higher than the normal resolution, but it isn't high enough for publication or some presentations. Using a dpi value of up to 2000 still produced blurry images when viewed close up.

    Less successful test #2: plt.savefig('filename.pdf')

    This cannot be opened in Microsoft Office Professional Plus 2016 (so no powerpoint), same with Google Slides.

    Less successful test #3: plt.savefig('filename.svg')

    This also cannot be opened in powerpoint or Google Slides, with the same issue as above.

    Less successful test #4: plt.savefig('filename.pdf')

    and then converting to png on the command line:

    convert -density 300 filename.pdf filename.png

    but this is still too blurry when viewed close up.

    Less successful test #5: plt.savefig('filename.pdf')

    and opening in GIMP, and exporting as a high quality png (increased the file size from ~100 KB to ~75 MB)

    Less successful test #6: plt.savefig('filename.pdf')

    and then converting to jpeg on the command line:

    pdfimages -j filename.pdf filename

    This did not produce any errors but did not produce an output on Ubuntu even after changing around several parameters.

    0 讨论(0)
  • 2020-12-07 13:33

    You can save your graph as svg for a lossless quality:

    import matplotlib.pylab as plt
    
    x = range(10)
    
    plt.figure()
    plt.plot(x,x)
    plt.savefig("graph.svg")
    
    0 讨论(0)
  • 2020-12-07 13:34

    For saving the graph:

    matplotlib.rcParams['savefig.dpi'] = 300

    For displaying the graph when you use plt.show():

    matplotlib.rcParams["figure.dpi"] = 100

    Just add them at the top

    0 讨论(0)
  • 2020-12-07 13:39

    You can use savefig() to export to an image file:

    plt.savefig('filename.png')
    

    In addition, you can specify the dpi argument to some scalar value, for example:

    plt.savefig('filename.png', dpi=300)
    
    0 讨论(0)
  • 2020-12-07 13:39

    use plt.figure(dpi=1200) before all your plt.plot... and at the end use plt.savefig(... see: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure and http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig

    0 讨论(0)
提交回复
热议问题