plt.subplots 的问题,哪个应该是最好的选择?

Problems with plt.subplots, which should be the best option?

我是 python 和 Whosebug 的新手...我来自 ggplot2 R 背景,但我仍然被 python 所困扰。我不明白为什么我在使用 matplotlib 的图形之前有一个空图...我只有一个基本的 pandas 系列,我想在子图中绘制一些行,在其他行上绘制一些行(但是我的显示很糟糕,我不知道 why/how 修复它)。提前致谢!

df = organism_df.T
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(5,5))
ax1 = df.iloc[[0,2,3,-1]].plot(kind='bar')
ax1.get_legend().remove()
ax1.set_title('Number of phages/bacteria interacting vs actual DB')

ax2 = df.iloc[[1,4,5,6,7]].plot(kind='bar')
ax2.get_legend().remove()
ax2.set_title('Number of different taxonomies with interactions')

plt.tight_layout()

pandas 中的方法 plot 需要将轴作为参数给出,例如 df.plot(ax=ax1, kind='bar')。在您的示例中,首先创建图形(由 ax1 和 ax2 组成),然后 另一个图形由 plot 函数创建(同时覆盖原始 ax1 对象)等。