在Matplotlib,Python中删除某些子图的y轴刻度标签

时间:2020-08-27 15:41:16

标签: python pandas matplotlib seaborn subplot

我有一个带有三个子图的图形,所有子图的y轴都使用相同的刻度标签(它们是分类的)。这是代码:

on_bus = business_changes[business_changes['Business characteristics']=='Ontario']
qu_bus = business_changes[business_changes['Business characteristics']=='Quebec']
fig, ax = plt.subplots(nrows=1, ncols=3, sharex=True, sharey=True, figsize=(20,10))
ax1 = plt.subplot(1,3,1) 
sns.barplot(x = business_changes.iloc[0,1:], y= business_changes.columns[1:])
plt.title("Changes made by businesses - Canada")
plt.subplot(1,3,2)
sns.barplot(x = on_bus.iloc[0,1:], y = on_bus.columns[1:])
plt.title("Changes by businesses - Ontario")
plt.subplot(1,3,3)
sns.barplot(x = qu_bus.iloc[0,1:], y = qu_bus.columns[1:])
plt.title("Changes by businesses - Quebec")
plt.show()

情节如下:

Plot

我想删除最后两个图的y轴标签,因为它们本质上与第一个图具有相同的标签。这样,我不必为空间而战,图形看起来会更整洁。

1 个答案:

答案 0 :(得分:3)

我的方法是使用axes.get_yaxis().set_visible(False)。因此,以下内容:

f, axes = plt.subplots(1, 3)
ax1 = sns.barplot(x = business_changes.iloc[0,1:], y= business_changes.columns[1:], ax = [0])
plt.title("Changes made by businesses - Canada")
ax2 = sns.barplot(x = on_bus.iloc[0,1:], y = on_bus.columns[1:], ax = axes[1])
ax2.axes.get_yaxis().set_visible(False)
plt.title("Changes by businesses - Ontario")
ax3 = sns.barplot(x = qu_bus.iloc[0,1:], y = qu_bus.columns[1:], ax = axes[2])
plt.title("Changes by businesses - Quebec")
plt.show()

否则,请尝试使其适合您的脚本,并绝对使用axes.get_yaxis().set_visible(False)并以最后两个图为目标。就我而言,我将它们定义为ax2ax3并按名称“定位”。