如何在 2x2 子图中正确绘制四个条形图

How to correctly plot four bar graphs in a 2x2 subplot

我正在尝试获取四个条形图并将它们放在一个 2x2 子图中。但是,当我尝试创建子图时,它首先绘制四个空子图,然后将四个条形图放在它下面。

这是我一直在使用的代码。

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, figsize = (20,15), sharex=True, sharey=True)
fig.suptitle('% Frequency Distribution of Daily Total Precipitation', fontsize = 45)

ax1 = ECobs_frequency.plot(x ='Precip Interval', y = 'Herbert Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Herbert Island Station')
ax2 = ECobs_frequency.plot(x = 'Precip Interval', y = 'Sartine Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Sartine Island Station')
ax3 = ECobs_frequency.plot(x = 'Precip Interval', y = 'Solander Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Solander Island Station')
ax4 = ECobs_frequency.plot(x = 'Precip Interval', y = 'Quatsino % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Quatsino Station')

ax1.legend(loc = 'upper left', fontsize = 28)
ax2.legend(loc = 'upper left', fontsize = 28)
ax3.legend(loc = 'upper left', fontsize = 28)
ax4.legend(loc = 'upper left', fontsize = 28)

ax1.tick_params(axis = 'both', which = 'both', labelsize=25)
ax2.tick_params(axis = 'both', which = 'both', labelsize=25)
ax3.tick_params(axis = 'both', which = 'both', labelsize=25)
ax4.tick_params(axis = 'both', which = 'both', labelsize=25)
ax3.tick_params(axis = 'x', which = 'both', rotation = 35)
ax4.tick_params(axis = 'x', which = 'both', rotation = 35)

ax1.grid(linewidth = 0.5)
ax2.grid(linewidth = 0.5)
ax3.grid(linewidth = 0.5)
ax4.grid(linewidth = 0.5)
ax1.set_axisbelow(True)
ax2.set_axisbelow(True)
ax3.set_axisbelow(True)
ax4.set_axisbelow(True)



fig.text(0.5, 0.04, 'Daily Total Precipitation (mm/d)', ha='center', fontsize = 48)
fig.text(0.04, 0.5, 'Percentage Frequency (%)', va='center', rotation='vertical', fontsize = 48)
for ax in fig.get_axes():
    ax.label_outer()

这是我从 运行 这段代码得到的输出。

我怎样才能使这四个条形图实际上位于子图中并共享相同的 x 轴和 y 轴?感谢任何帮助,谢谢。

您需要指定要绘制的轴,因此请替换这些行:

ax1 = ECobs_frequency.plot(x ='Precip Interval', y = 'Herbert Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Herbert Island Station')
ax2 = ECobs_frequency.plot(x = 'Precip Interval', y = 'Sartine Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Sartine Island Station')
ax3 = ECobs_frequency.plot(x = 'Precip Interval', y = 'Solander Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Solander Island Station')
ax4 = ECobs_frequency.plot(x = 'Precip Interval', y = 'Quatsino % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Quatsino Station')

这些:

ECobs_frequency.plot(ax = ax1, x ='Precip Interval', y = 'Herbert Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Herbert Island Station')
ECobs_frequency.plot(ax = ax2, x = 'Precip Interval', y = 'Sartine Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Sartine Island Station')
ECobs_frequency.plot(ax = ax3, x = 'Precip Interval', y = 'Solander Island % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Solander Island Station')
ECobs_frequency.plot(ax = ax4, x = 'Precip Interval', y = 'Quatsino % Frequency', kind="bar", width = 1, edgecolor = 'black', label = 'Quatsino Station')

请注意,在您的代码中,您在第一行定义了 ax1ax2ax3ax4,但您没有使用它们,而且您当你调用 ECobs_frequency.plot 时覆盖它们。相反,您应该将要绘制的斧头作为 ax 参数传递给 plot