Search code examples
pythonmatplotlibseabornsubplot

Setting different axis limits for subplots in seaborn


I cannot figure out the way of setting diferent Y axis limits to different subplots. It takes as Y limit for all the subplots the last statement 'ax6.set_ylim(0,68)' .Any help would be very welcome.

fig, (ax1, ax2, ax3,ax4,ax5,ax6) = plt.subplots(nrows=6,ncols=1, sharey=True,figsize=(14,14))


sns.regplot(x=df['Income'], y=df['Murder'], ax=ax1,scatter_kws={'alpha': 0.9, 's': 2.0, 'rasterized': 
False, 'zorder': 1})
ax1.set_ylim(0,10)


sns.regplot(x=df['Income'], y=df['Life Exp'], ax=ax2,scatter_kws={'alpha': 0.9, 's': 2.0, ' 
rasterized': False, 'zorder': 1})
ax2.set_ylim(0,100)


sns.regplot(x=df['Income'], y=df['Illiteracy'], ax=ax3,scatter_kws={'alpha': 0.9, 's': 2.0, 
'rasterized': False, 'zorder': 1})
ax3.set_ylim(0,5)


sns.regplot(x=df['Income'], y=df['Frost'], ax=ax4)
ax4.set_ylim(0,200)

sns.regplot(x=df['Income'], y=df['Population'], ax=ax5)
ax5.set_ylim(0,21198)


sns.regplot(x=df['Income'], y=df['HS Grad'], ax=ax6)
ax6.set_ylim(0,68)

Solution

  • You have set sharey=True when you created your axes (plt.subplots(..., sharey=True, ...)), which by definition means that all subplots have the same range (the one from ax6 since it is the last you are setting).

    Just pass sharey=False if you do not want this behavior.