How to make pylab.savefig() save image for 'maximized' window instead of default size

前端 未结 8 2112
一整个雨季
一整个雨季 2020-12-12 17:49

I am using pylab in matplotlib to create a plot and save the plot to an image file. However, when I save the image using pylab.savefig( image_name ), I find tha

相关标签:
8条回答
  • 2020-12-12 18:29

    I had this exact problem and this worked:

    plt.savefig(output_dir + '/xyz.png', bbox_inches='tight')
    

    Here is the documentation:

    [https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html][1]

    0 讨论(0)
  • 2020-12-12 18:31

    There are two major options in matplotlib (pylab) to control the image size:

    1. You can set the size of the resulting image in inches
    2. You can define the DPI (dots per inch) for output file (basically, it is a resolution)

    Normally, you would like to do both, because this way you will have full control over the resulting image size in pixels. For example, if you want to render exactly 800x600 image, you can use DPI=100, and set the size as 8 x 6 in inches:

    import matplotlib.pyplot as plt
    # plot whatever you need...
    # now, before saving to file:
    figure = plt.gcf() # get current figure
    figure.set_size_inches(8, 6)
    # when saving, specify the DPI
    plt.savefig("myplot.png", dpi = 100)
    

    One can use any DPI. In fact, you might want to play with various DPI and size values to get the result you like the most. Beware, however, that using very small DPI is not a good idea, because matplotlib may not find a good font to render legend and other text. For example, you cannot set the DPI=1, because there are no fonts with characters rendered with 1 pixel :)

    From other comments I understood that other issue you have is proper text rendering. For this, you can also change the font size. For example, you may use 6 pixels per character, instead of 12 pixels per character used by default (effectively, making all text twice smaller).

    import matplotlib
    #...
    matplotlib.rc('font', size=6)
    

    Finally, some references to the original documentation: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig, http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.gcf, http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.set_size_inches, http://matplotlib.sourceforge.net/users/customizing.html#dynamic-rc-settings

    P.S. Sorry, I didn't use pylab, but as far as I'm aware, all the code above will work same way in pylab - just replace plt in my code with the pylab (or whatever name you assigned when importing pylab). Same for matplotlib - use pylab instead.

    0 讨论(0)
  • 2020-12-12 18:32

    If I understand correctly what you want to do, you can create your figure and set the size of the window. Afterwards, you can save your graph with the matplotlib toolbox button. Here an example:

    from pylab import get_current_fig_manager,show,plt,imshow
    
    plt.Figure()
    thismanager = get_current_fig_manager()
    thismanager.window.wm_geometry("500x500+0+0") 
    #in this case 500 is the size (in pixel) of the figure window. In your case you want to maximise to the size of your screen or whatever
    
    imshow(your_data)
    show()
    
    0 讨论(0)
  • 2020-12-12 18:37

    Check this: How to maximize a plt.show() window using Python

    The command is different depending on which backend you use. I find that this is the best way to make sure the saved pictures have the same scaling as what I view on my screen.

    Since I use Canopy with the QT backend:

    pylab.get_current_fig_manager().window.showMaximized()
    

    I then call savefig() as required with an increased DPI per silvado's answer.

    0 讨论(0)
  • 2020-12-12 18:45

    I think you need to specify a different resolution when saving the figure to a file:

    fig = matplotlib.pyplot.figure()
    # generate your plot
    fig.savefig("myfig.png",dpi=600)
    

    Specifying a large dpi value should have a similar effect as maximizing the GUI window.

    0 讨论(0)
  • 2020-12-12 18:50

    You set the size on initialization:

    fig2 = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) # in inches!
    

    Edit:

    If the problem is with x-axis ticks - You can set them "manually":

    fig2.add_subplot(111).set_xticks(arange(1,3,0.5)) # You can actually compute the interval You need - and substitute here
    

    And so on with other aspects of Your plot. You can configure it all. Here's an example:

    from numpy import arange
    import matplotlib
    # import matplotlib as mpl
    import matplotlib.pyplot
    # import matplotlib.pyplot as plt
    
    x1 = [1,2,3]
    y1 = [4,5,6]
    x2 = [1,2,3]
    y2 = [5,5,5]
    
    # initialization
    fig2 = matplotlib.pyplot.figure(figsize=(8.0, 5.0)) # The size of the figure is specified as (width, height) in inches
    
    # lines:
    l1 = fig2.add_subplot(111).plot(x1,y1, label=r"Text $formula$", "r-", lw=2)
    l2 = fig2.add_subplot(111).plot(x2,y2, label=r"$legend2$" ,"g--", lw=3)
    fig2.add_subplot(111).legend((l1,l2), loc=0)
    
    # axes:
    fig2.add_subplot(111).grid(True)
    fig2.add_subplot(111).set_xticks(arange(1,3,0.5))
    fig2.add_subplot(111).axis(xmin=3, xmax=6) # there're also ymin, ymax
    fig2.add_subplot(111).axis([0,4,3,6]) # all!
    fig2.add_subplot(111).set_xlim([0,4])
    fig2.add_subplot(111).set_ylim([3,6])
    
    # labels:
    fig2.add_subplot(111).set_xlabel(r"x $2^2$", fontsize=15, color = "r")
    fig2.add_subplot(111).set_ylabel(r"y $2^2$")
    fig2.add_subplot(111).set_title(r"title $6^4$")
    fig2.add_subplot(111).text(2, 5.5, r"an equation: $E=mc^2$", fontsize=15, color = "y")
    fig2.add_subplot(111).text(3, 2, unicode('f\374r', 'latin-1'))
    
    # saving:
    fig2.savefig("fig2.png")
    

    So - what exactly do You want to be configured?

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