seaborn jointplot margins not working with logarithmic axes

634 Views Asked by At

I'm trying to plot via:

g = sns.jointplot(x = etas, y = vs, marginal_kws=dict(bins=100), space = 0)
g.ax_joint.set_xscale('log')
g.ax_joint.set_yscale('log')
g.ax_joint.set_xlim(0.01)
g.ax_joint.set_ylim(0.01)
g.ax_joint.set_xlabel(r'$\eta$')
g.ax_joint.set_ylabel("V")

plt.savefig("simple_scatter_plot_Seanborn.png",figsize=(8,8), dpi=150)

Which leaves me with the following image:

plot

This is not what I want. Why are the histograms filled at the end? There are no data points there so I don't get it...

1

There are 1 best solutions below

0
On

You're setting a log scale on the matplotlib axes, but by the time you are doing that, seaborn has already computed the histogram. So the equal-width bins in linear space appear to have different widths; the lowest bin has a narrow range in terms of actual values, but that takes up a lot of space on the horizontal plot.

Tested in python 3.10, matplotlib 3.5.1, seaborn 0.11.2

Solution: pass log_scale=True to the histograms:

import seaborn as sns

# test dataset
planets = sns.load_dataset('planets')

g = sns.jointplot(data=planets, x="orbital_period", y="distance", marginal_kws=dict(log_scale=True))

enter image description here

  • without using marginal_kws=dict(log_scale=True)

enter image description here

  • Compared to setting the scale after the plot is created.
g = sns.jointplot(data=planets, x="orbital_period", y="distance")

g.ax_joint.set_xscale('log')
g.ax_joint.set_yscale('log')

enter image description here