【问题标题】:How to correctly plot four bar graphs in a 2x2 subplot [duplicate]如何在 2x2 子图中正确绘制四个条形图 [重复]
【发布时间】: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 = ax1ax = ax2 等添加到您的plot 呼叫中。
  • 对于您的设置,answer 的第 2 部分可能是最好的。

标签: python pandas matplotlib plot data-visualization


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2018-05-20
    • 2020-01-18
    相关资源
    最近更新 更多