简体   繁体   中英

Removing y axis tick labels for certain subplots in Matplotlib,Python

I have a figure with three subplots, and the y-axis for all subplots uses the same tick labels (they're categorical). Here's the code:

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()

The plot looks like the following:

Plot

I want to remove the y axis labels for the last two plots because they essentially have the same labels as the first one. With that, I don't have to fight for space and the graph would look neater.

My approach would be to use axes.get_yaxis().set_visible(False) . So the following:

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()

Otherwise, try to fit it in your script and definitely use the axes.get_yaxis().set_visible(False) and target the two last plots. In my case, I defined them as ax2 and ax3 and "targeted" by name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM