如何在 X 轴刻度和标签之间添加更多间距 MATPLOTLIB

时间:2021-03-24 13:52:20

标签: matplotlib plot format axis-labels x-axis

我一直在网上寻求帮助,但我所看到的任何东西都无法帮助我在 xticks 之间添加空间而不会丢失 x 轴上的任何 xtick 标签。这是我的代码以及我试图将下图变为当前布局的内容。是否有自动调整有效? “plt.tight_layout”应该这样做,但似乎对我不起作用。感谢您的帮助。并且,plt.suptitle 不以月份(二月)为中心。

#CALC VARIABILITY AND PLOT BOXPLOTS for ea month
sitenames = df3.plant_name.unique().tolist()
months = ['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER']
from datetime import datetime
monthn = datetime.now().month
newList = list()
for i in range(monthn-1):
    newList.append(months[(i)%12])
print(newList)
for i, month in  enumerate(newList,1):
    #plt.figure()
    #fig, ax = plt.subplots()
    #ax = df3[df3['month']==i].boxplot(by='plant_name',column='Var',grid=False)
    datey = datetime.today().strftime("%Y")
    ax = df3[df3['month']==i].plot(kind='scatter',x='plant_name',y='Var',marker='.',s=5,grid=False)
    stdp[stdp['month']==i].plot(kind='scatter', x = 'plant_name', 
y='Var',color='k',marker='_',s=200,label = '1+Std', grid=False,ax=ax)
    stdn[stdn['month']==i].plot(kind='scatter', x = 'plant_name', 
y='Var',color='k',marker='_',s=200,label = '1-Std', grid=False,ax=ax)
    stdp2[stdp2['month']==i].plot(kind='scatter', x = 'plant_name', 
y='Var',color='k',marker='_',s=50,label = '2+Std',grid=False,ax=ax)
    stdn2[stdn2['month']==i].plot(kind='scatter', x = 'plant_name', 
y='Var',color='k',marker='_',s=50,label = '2-Std',grid=False,ax=ax)
    medn[medn['month']==i].plot(kind='scatter', x = 'plant_name', 
y='Var',color='g',marker='_',s=90,label = 'p50',grid=False,ax=ax)
    df3c[df3c['month']==i].plot(kind='scatter', x = 'plant_name', y='Var',color='r',label = 
datey,grid=False,ax=ax)
    # Save the default tick positions, so we can reset them...
    locs, labels = plt.xticks()
    plt.xticks(rotation=90, ha='center')
    plt.suptitle('1991-2020 ERA5 WIND PRODUCTION',y=1)
    plt.title(months[i-1])
    plt.xlabel('SITE')
    plt.ylabel('VARIABILITY')
    #plt.legend()
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),prop={'size': 8})
    plt.tick_params(axis='x', which='major', labelsize=10)
    plt.tight_layout(rect=[0, 0, 2, 1])
    plt.subplots_adjust(bottom=0.1)

没有“plot_tight_layout(rect[0, 0, 2, 1])”

enter image description here

并且,包括“plot_tight_layout(rect[0, 0, 2, 1])”:

enter image description here

0 个答案:

没有答案
相关问题