Broken axis for seaborn scatterplot - Cannot minimize distance between plots

Viewed 333

Currently trying to create a sns.scatterplot. Due to outliers, I want to break the y-axis, possibly the x-axis as well, to better visualize the effect of outliers.

I've managed to break the y-axis, but the command: subplots_adjust does not seem to work.

Current code:

def scatter_plot(data, x, y, hue, anon): 
imgdata = BytesIO()

fig, (ax1, ax2)= plt.subplots(2, 1, figsize=fig_size, sharex=True)

cols = list()
for key in colors.keys():
    if re.search('Warm_.+|Cold_.+', key):
        cols.append(colors.get(key))

data.rename(columns={x: f'{x} (mio. DKK)', y: f'{y} (mio. DKK)'}, inplace=True)
print(colors)

ax1 = sns.scatterplot(x=f'{x} (mio. DKK)', y=f'{y} (mio. DKK)',  data=data, ax=ax1, hue='Forsikringsselskab', style='Årstal',
                palette=cols[0:len(colors)-1])

ax2 = sns.scatterplot(x=f'{x} (mio. DKK)', y=f'{y} (mio. DKK)',  data=data, ax=ax2, hue='Forsikringsselskab', style='Årstal',
                palette=cols[0:len(colors)-1])    

plt.legend(frameon=True, loc=(1.04, 0.5))


ax2.set_ylim(-90, 150)
ax1.set_ylim(250, 290)
ax1.get_legend().remove()

ax1.set_ylabel("")
ax2.set_ylabel("")
fig.text(0.002, 0.55, 'Investeringsafkast før forsikringsteknisk rente (mio. DKK)', va='center', rotation='vertical')

plt.axhline(0, linestyle=':')

plt.subplots_adjust(hspace=0.2)
plt.tight_layout()

fig.savefig(imgdata, format='png', dpi=300)

plt.close()

return imgdata

The current output of this code creates:

Output

My desire is to mimimize the distance. What am I missing?

Thank you in advance for your time!

0 Answers
Related