使用 matplotlib 在一个图形上绘制四个箱线图

Four boxplots on one figure with matplotlib

所以我有多个数据文件,所有这些文件我都使用数据帧以便轻松处理信息。所有文件都是 NetCDF 文件。我正在尝试在同一个图形上绘制 4 个箱形图,以便可以轻松地比较它们(我正在查看同一时间选择的平均值在不同时间达到峰值)。它们将具有不同的 X 和 Y 值,因为这是在稍微不同的时间间隔获取的观测数据,并且不同的变量编号对应于不同的时间相关变量。

我一直在尝试使用子图来使用 matplotlib 来实现这一点,但它只是吐出三个空图。我怎样才能做到这一点?

代码如下:

plt.figure(1)
plt.subplot(2, 1, 1)
ds1 = xr.open_dataset(lfile1)
one_day1 = ds1[variable1].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df1 = one_day1.to_pandas().to_frame(name=variable1)
df1.index=df1.index-pd.DateOffset(hours=7)
df1.boxplot(column=variable1, by=df1.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Temperature [degF]')
plt.title('Daily Cycle')
plt.suptitle('')

plt.subplot(2, 1, 2)
ds2 = xr.open_dataset(lfile2)
one_day2 = ds2[variable2].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df2 = one_day2.to_pandas().to_frame(name=variable2)
df2.index=df2.index-pd.DateOffset(hours=7)
df2.boxplot(column=variable2, by=df2.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Average Wind Speed')
plt.title('Daily Cycle')
plt.suptitle('')

plt.subplot(2, 2, 1)
ds3 = xr.open_dataset(lfile3)
one_day3 = ds3[variable3].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df3 = one_day3.to_pandas().to_frame(name=variable3)
df3.index=df1.index-pd.DateOffset(hours=7)
df3.boxplot(column=variable3, by=df3.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Wind  Direction')
plt.title('Daily Cycle')
plt.suptitle('')

plt.subplot(2, 2, 2)
ds4 = xr.open_dataset(lfile4)
one_day4 = ds4[variable4].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df4 = one_day4.to_pandas().to_frame(name=variable4)
df4.index=df4.index-pd.DateOffset(hours=7)
df4.boxplot(column=variable4, by=df4.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Solar Radiation [W/m^2]')
plt.title('Daily Cycle')
plt.suptitle('')

plt.show()

到底是什么没有发生?

好的,现在,我将其编辑为如下所示:

plt.figure()
ax1 = plt.subplot(2, 2, 1)
df1.boxplot(column=variable1, by=df1.index.time, whis=[10, 90], sym='')
ax1.set_title('Daily Cycle')

ax2 = plt.subplot(2, 2, 2)
df2.boxplot(column=variable2, by=df2.index.time, whis=[10, 90], sym='')
ax2.set_title('Daily Cycle')

ax3 = plt.subplot(2, 2, 3)
df3.boxplot(column=variable3, by=df3.index.time, whis=[10, 90], sym='')
ax3.set_title('Daily Cycle')

ax4 = plt.subplot(2, 2, 4)
df4.boxplot(column=variable4, by=df4.index.time, whis=[10, 90], sym='')
ax4.set_title('Daily Cycle')

plt.show()

现在,我得到五个 windows,五位数。一个占据整个 window 的图形具有四个子图中应有的所有数据,而其他四个图形在我想要的位置上各有一个子图,但为空。

您只获得了三个图表,因为您提供了 subplot 错误的参数并覆盖了您之前创建的坐标轴。 来自文档:

subplot(nrows, ncols, plot_number)

nrows 是图中子图的行数,ncols 是图中的列数。

这是一个使用 Pandas DataFrame 箱线图方法的工作示例:

df1 = pd.DataFrame({'a':[1,2,3],'b':[6,8,2],'c':[9,1,7]})
df2 = pd.DataFrame({'a':[15,23,32],'b':[6,80,2],'c':[9,10,7]})
df3 = pd.DataFrame({'a':[0.2,0.5,0.5],'b':[18,5,2],'c':[9,7,7]})
df4 = pd.DataFrame({'a':[51,32,20],'b':[4,3,20],'c':[7,2,1]})

fig = plt.figure()
ax1 =fig.add_subplot(2,2,1)
df1.boxplot(ax = ax1)
ax2 =fig.add_subplot(2,2,2)
df2.boxplot(ax = ax2)
ax3 =fig.add_subplot(2,2,3)
df3.boxplot(ax = ax3)
ax4 =fig.add_subplot(2,2,4)
df4.boxplot(ax = ax4)
plt.show()

我不确定为什么你的地块都是空的。我会检查您正在绘制的 DataFrame 的内容。