首页 > 解决方案 > 如何在 2x2 子图中正确绘制四个条形图

问题描述

我正在尝试获取四个条形图并将它们放在一个 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 轴?任何帮助表示赞赏,谢谢。

标签: pythonpandasmatplotlibplotdata-visualization

解决方案


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

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')

请注意,在您的代码中,您在第一行定义了 ,ax1和,但您没有使用它们,而是在调用. 相反,您应该将要绘制的斧头作为参数传递给它。ax2ax3ax4ECobs_frequency.plotplotax


推荐阅读