【发布时间】:2021-11-05 12:31:49
【问题描述】:
我正在尝试获取四个条形图并将它们放在一个 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 轴?任何帮助表示赞赏,谢谢。
【问题讨论】:
-
将
ax = ax1、ax = ax2等添加到您的plot呼叫中。 -
对于您的设置,answer 的第 2 部分可能是最好的。
标签: python pandas matplotlib plot data-visualization