Grog
Grog

Reputation: 13

Show the yticks in the marginal plots of seaborn.JointGrid

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

Upvotes: 1

Views: 1496

Answers (1)

JohanC
JohanC

Reputation: 80509

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

Upvotes: 4

Related Questions