在 matplotlib 中丢弃 multi-bar 图表中的 x-axis 标签
discard x-axis label in multi-bar chart in matplotlib
我使用 matplotlib 和 Pandas 生成了如下所示的图表,除了两个问题:
我无法摆脱顶部三个图的 x 轴标签(年份)(我尝试过的内容在附加代码中被注释掉了),而且我无法将 y 轴标签居中。
这看起来微不足道,但我无法让它发挥作用。
有什么建议么?
谢谢
fig, axes = plt.subplots(nrows=4, ncols=1, sharex=True, figsize=(12, 12))
for i, crop in enumerate(['Cabbage','Green beans','Potato', 'Wheat']):
mean_irr_soil[crop].plot(kind='bar', ax=axes[i], title=crop, grid=False)
axes[i].set_ylim([0, 700]) # set limit for all y axes
axes[i].tick_params(axis='x', top='off') # remove top ticks
axes[i].tick_params(axis='y', right='off') # remove right-hand ticks
axes[i].tick_params(axis='x', labelbottom='off')
if i is not 0:
axes[i].legend_.remove() # all but top graph
# if i is not 3:
# axes[i].get_xaxis().set_visible(False) # remove axis all together
# axes[i].tick_params(axis='x', labelbottom='off')
# axes[i].set_xticklabels([]) # remove x-axis labels
axes[0].legend(bbox_to_anchor=(1.1, 1.4))
axes[2].set_ylabel('Mean irrigation requirements in mm')
您需要 xlabel
属性:ax.set_xlabel("")
.
我使用 matplotlib 和 Pandas 生成了如下所示的图表,除了两个问题: 我无法摆脱顶部三个图的 x 轴标签(年份)(我尝试过的内容在附加代码中被注释掉了),而且我无法将 y 轴标签居中。 这看起来微不足道,但我无法让它发挥作用。 有什么建议么? 谢谢
fig, axes = plt.subplots(nrows=4, ncols=1, sharex=True, figsize=(12, 12))
for i, crop in enumerate(['Cabbage','Green beans','Potato', 'Wheat']):
mean_irr_soil[crop].plot(kind='bar', ax=axes[i], title=crop, grid=False)
axes[i].set_ylim([0, 700]) # set limit for all y axes
axes[i].tick_params(axis='x', top='off') # remove top ticks
axes[i].tick_params(axis='y', right='off') # remove right-hand ticks
axes[i].tick_params(axis='x', labelbottom='off')
if i is not 0:
axes[i].legend_.remove() # all but top graph
# if i is not 3:
# axes[i].get_xaxis().set_visible(False) # remove axis all together
# axes[i].tick_params(axis='x', labelbottom='off')
# axes[i].set_xticklabels([]) # remove x-axis labels
axes[0].legend(bbox_to_anchor=(1.1, 1.4))
axes[2].set_ylabel('Mean irrigation requirements in mm')
您需要 xlabel
属性:ax.set_xlabel("")
.