0

I need to show the yticks in the marginal distribution of a Seaborn.jointgrid plot, but it doesn't seem simple.

Here is an example of a code taken from the Seaborn documentation:

import seaborn as sns; sns.set(style="ticks", color_codes=True)
import bumpy as np
tips = sns.load_dataset("tips")

g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g = g.plot_joint(sns.scatterplot, color="m")
_ = g.ax_marg_x.hist(tips["total_bill"], color="b", alpha=.6,
                      bins=np.arange(0, 60, 5))
_ = g.ax_marg_y.hist(tips["tip"], color="r", alpha=.6,
                      orientation="horizontal",
                      bins=np.arange(0, 12, 1))

I would like to add the yaxis and its values to the marginal plots. I can plot the yaxis using:

sns.despine(ax=g.ax_marg_x)
sns.despine(ax=g.ax_marg_y)

But it doesn't contain any values or ticks. I have tried the solution proposed here but it just doesn't do anything.

Here is the plot given by the code

image

William Miller
  • 9,839
  • 3
  • 25
  • 46
Grog
  • 3
  • 2
  • As of seaborn 0.12.1, you can also call `sns.JointGrid(marginal_ticks=True)` which does the same as the 6 lines of code starting with `g.ax_marg` in @JohanC's answer. If you want to adjust the looks of the ticks, you need to dig further. – Jelmer Mulder Nov 16 '22 at 10:53

1 Answers1

3

The axes of the marginal plots are 'shared' with the main plot. Default, they don't get tick labels. To turn the tick labels on again, tick_params has options such as labelleft=True and labelbottom=True.

When setting ticks for the histogram values, it can be handy to also set gridlines. The default number of gridlines might be too low, they can be increased with a tick locator. Also, the default linestyle might be too heavy. Linestyle, color and thickness can be adapted via grid()

import seaborn as sns; sns.set(style="ticks", color_codes=True)
import numpy as np
from matplotlib.ticker import MaxNLocator

tips = sns.load_dataset("tips")
g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g = g.plot_joint(sns.scatterplot, color="m")
_ = g.ax_marg_x.hist(tips["total_bill"], color="b", alpha=.6,
                     bins=np.arange(0, 60, 5))
_ = g.ax_marg_y.hist(tips["tip"], color="r", alpha=.6,
                     orientation="horizontal",
                     bins=np.arange(0, 12, 1))
g.ax_marg_y.tick_params(labeltop=True)
g.ax_marg_y.grid(True, axis='x', ls=':')
g.ax_marg_y.xaxis.set_major_locator(MaxNLocator(4))

g.ax_marg_x.tick_params(labelleft=True)
g.ax_marg_x.grid(True, axis='y', ls=':')
g.ax_marg_x.yaxis.set_major_locator(MaxNLocator(4))

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • I meant to plot the yaxis and not the xaxis of the marginal plots, but I have managed to do that using tick_params options. Thank you it is now working! – Grog Apr 23 '20 at 10:48