简体   繁体   中英

How to align subplot with specified size?

I have a lot of different plots, wich sizes must be strongly specified. One plot per one file. The problem is that when size too large I have some free spaces in subplot from the left, which I want to crop. I can't use subplots_adjust because plots with small sizes will be croped too. Also I can't use tight option when I call savefig . So, how can I align all my plots to the left side with fixed indent in all my files?

import sys, os
import matplotlib.pyplot as plt

path = sys.path[0]
sizes = [(12,3,), (4,3,)]
x =  range(20)


for i, size in enumerate(sizes):
    fig = plt.figure(figsize = size, dpi = 80, facecolor='white', edgecolor=None, linewidth=0.0, frameon=True, subplotpars=None)
    ax = fig.add_subplot(111)
    ax.plot(x)
    plt.ylabel ('Some label')

    fig.savefig(os.path.join(path, 'size_'+str(i)+'.png'), dpi=80, facecolor=fig.get_facecolor(), edgecolor=None, papertype=None, format='png' ,transparent=False)

大尺寸图像小尺寸图像

I think you can just use tight_layout() :

import sys, os
import matplotlib.pyplot as plt

path = sys.path[0]
sizes = [(12,3,), (4,3,)]
x =  range(20)


for i, size in enumerate(sizes):
    fig = plt.figure(figsize = size, dpi = 80, facecolor='white', edgecolor=None,linewidth=0.0, frameon=True, subplotpars=None)
    ax = fig.add_subplot(111)
    ax.plot(x)
    plt.ylabel ('Some label')

    plt.tight_layout()

    fig.savefig(os.path.join(path, 'size_'+str(i)+'.png'), dpi=80,facecolor=fig.get_facecolor(), edgecolor=None, papertype=None, format='png',transparent=False)

Also you can use something like

fig.subplots_adjust(right=0.9, left=0.1, top=0.9, bottom=0.1, wspace=0.2)

instead of plt.tight_layout() . If you want.


Please, tell me if that's not what you need.

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