python - 如何在 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 轴?任何帮助表示赞赏,谢谢。
解决方案
您需要指定要绘制的轴,因此请替换以下行:
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
和,但您没有使用它们,而是在调用. 相反,您应该将要绘制的斧头作为参数传递给它。ax2
ax3
ax4
ECobs_frequency.plot
plot
ax
推荐阅读
- ios - 深层链接在 iPad 中有效,但在 iPhone 中无效
- sql-server - MSSQL 插入或更新(如果存在)
- swift - 如何在 Apple 的 PDFKit 中指定字母格式?
- r - 结合多个观察的向量化方法
- java - 如何为 `javax.validation.constraints` 注释添加自定义验证器?
- docker - 运行 `minikube start`,无法拉取 k8s.gcr 图像,但 `docker pull` 可以
- javascript - 模块名称是绑定中使用的阴影全局名称
- r - ggplotly 中的 Aes
- ios - 在 viewWillAppear 中设置约束
- javascript - VueJS,ReferenceError:谷歌未定义