sharex 和 sharey 不为子图创建共享轴标签
sharex and sharey not creating shared axis labels for subplots
我的图表没问题,我已经按照文档创建了共享的 x 和 y 标签,因为我想要一个更清晰的子图,但是传递给 subplots()
的参数无法正常工作。
代码:
fig, axs = plt.subplots(3, 2, sharex=True, sharey=True, figsize=(10,10))
plt.subplot(3, 1, 1)
plt.title('20 highest paid app markets, april 4/4-4/10')
dd_404_410.groupby('market').period_paid_apps.mean().sort_values(ascending=False).nlargest(10).plot(kind='bar', color='darkgrey')
plt.ylabel('apps')
plt.xticks(rotation=45)
plt.subplot(3, 1, 2)
plt.title('20 highest paid app markets, april 4/11-4/17')
dd_411_417.groupby('market').period_paid_apps.mean().sort_values(ascending=False).nlargest(10).plot(kind='bar', color='darkgrey')
plt.ylabel('apps')
plt.xticks(rotation=45)
plt.subplot(3, 1, 3)
plt.title('20 highest paid app markets, april 4/18-4/26')
plt.ylabel('apps')
dd_418_426.groupby('market').period_paid_apps.mean().sort_values(ascending=False).nlargest(10).plot(kind='bar', color='darkgrey')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
有谁知道需要修复什么,以便我在 x 轴上有一个 market
标签,在 y 轴上有一个 apps
标签?
您确实最初使用 plt.subplots()
创建了共享 x 和 y 的子图。但是你正在用连续的命令 plt.subplot()
覆盖轴(注意最后缺少 s)。
这可能是您应该采用的方法(未测试,因为我没有您的数据)
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, sharey=True, figsize=(10,10))
ax1.set_title('20 highest paid app markets, april 4/4-4/10')
ax1.set_ylabel('apps')
<YOUR DATAFRAME>.plot(kind='bar', color='darkgrey', ax=ax1)
ax2.set_title('20 highest paid app markets, april 4/11-4/17')
ax2.set_ylabel('apps')
<YOUR DATAFRAME>.plot(kind='bar', color='darkgrey', ax=ax2)
ax3.set_title('20 highest paid app markets, april 4/18-4/26')
ax3.set_ylabel('apps')
<YOUR DATAFRAME>.plot(kind='bar', color='darkgrey', ax=ax3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
我的图表没问题,我已经按照文档创建了共享的 x 和 y 标签,因为我想要一个更清晰的子图,但是传递给 subplots()
的参数无法正常工作。
代码:
fig, axs = plt.subplots(3, 2, sharex=True, sharey=True, figsize=(10,10))
plt.subplot(3, 1, 1)
plt.title('20 highest paid app markets, april 4/4-4/10')
dd_404_410.groupby('market').period_paid_apps.mean().sort_values(ascending=False).nlargest(10).plot(kind='bar', color='darkgrey')
plt.ylabel('apps')
plt.xticks(rotation=45)
plt.subplot(3, 1, 2)
plt.title('20 highest paid app markets, april 4/11-4/17')
dd_411_417.groupby('market').period_paid_apps.mean().sort_values(ascending=False).nlargest(10).plot(kind='bar', color='darkgrey')
plt.ylabel('apps')
plt.xticks(rotation=45)
plt.subplot(3, 1, 3)
plt.title('20 highest paid app markets, april 4/18-4/26')
plt.ylabel('apps')
dd_418_426.groupby('market').period_paid_apps.mean().sort_values(ascending=False).nlargest(10).plot(kind='bar', color='darkgrey')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
有谁知道需要修复什么,以便我在 x 轴上有一个 market
标签,在 y 轴上有一个 apps
标签?
您确实最初使用 plt.subplots()
创建了共享 x 和 y 的子图。但是你正在用连续的命令 plt.subplot()
覆盖轴(注意最后缺少 s)。
这可能是您应该采用的方法(未测试,因为我没有您的数据)
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, sharey=True, figsize=(10,10))
ax1.set_title('20 highest paid app markets, april 4/4-4/10')
ax1.set_ylabel('apps')
<YOUR DATAFRAME>.plot(kind='bar', color='darkgrey', ax=ax1)
ax2.set_title('20 highest paid app markets, april 4/11-4/17')
ax2.set_ylabel('apps')
<YOUR DATAFRAME>.plot(kind='bar', color='darkgrey', ax=ax2)
ax3.set_title('20 highest paid app markets, april 4/18-4/26')
ax3.set_ylabel('apps')
<YOUR DATAFRAME>.plot(kind='bar', color='darkgrey', ax=ax3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()