0

我有一个图是箱线图和散点图(红色数据点)的叠加。一切都很好,除了红色数据点没有在 x 轴上与箱线图 x 轴对齐。我认为这是因为在同一轴上使用了两种不同的绘图方法?我注意到“散点图”的第二种方法图有效地将箱线图在 x 轴上向右移动。绘制没有散点图的箱线图不会移动 x 轴值,如下所示。我在下面使用的方法应该有效,但事实并非如此。这是我的代码:

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[(monthn-1+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')
    df3c[df3c['month']==i].plot(kind='scatter', x='plant_name', y='Var',ax=ax,color='r',label='CY')
    plt.xticks(rotation=90, ha='right')
    plt.suptitle('1991-2020 ERA5 WIND PRODUCTION',y=1)
    plt.title(months[i-1])
    plt.xlabel('SITE')
    plt.ylabel('VARIABILITY')
    plt.legend()
    plt.show()

这是“二月”的图表,显示了未对齐的 x 轴:

在此处输入图像描述

以下是 df3、df3c 的部分行:

df3.head(3)
Out[223]: 
  plant_name  month  year     power_kwh  power_kwh_rhs   Var
0  BII NEE STIPA      1  1991  11905.826075   14673.281223 -18.9
1  BII NEE STIPA      1  1992  14273.927688   14673.281223  -2.7
2  BII NEE STIPA      1  1993  12559.828360   14673.281223 -14.4

df3c.head(3)
Out[224]: 
  plant_name  month    year     power_kwh  power_kwh_rhs   Var
0  BII NEE STIPA      1  2021.0  14863.643952   14673.281223   1.3
1  BII NEE STIPA      2  2021.0   9663.393155   12388.328084 -22.0
2  DOS ARBOLITOS      1  2021.0  36819.502285   36882.205762  -0.2

我发现了一个类似的问题,但看不到如何将此解决方案插入到我的代码中:Shift matplotlib axes to match eachother

4

0 回答 0