lanery
lanery

Reputation: 5364

Add yticks to marginals in Seaborn JointGrid

There doesn't seem to be any examples in the documentation for JointGrid or JointPlot that show yticks in the marginal plots. How can I add yticks to the marginal plots in the seaborn JointGrid plot below?

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset('tips')
g = sns.JointGrid(x='total_bill', y='tip', data=tips)
g = g.plot_joint(plt.scatter, color='#334f6d')
g = g.plot_marginals(sns.distplot, color='#418470')

enter image description here

I know what the yticks are from g.ax_marg_x.get_yticks(), but setting them with

g.ax_marg_x.set_yticks(g.ax_marg_x.get_yticks())

doesn't seem to do anything, nor do other simple attempts like g.ax_marg_x.yaxis.set_visible(True).

Upvotes: 2

Views: 2018

Answers (1)

sascha
sascha

Reputation: 33512

This plots something, but i'm not sure how to interpret these results (or how they were calculated; the right one does not look like a density).

    import matplotlib.pyplot as plt
    import seaborn as sns

    tips = sns.load_dataset('tips')

    g = sns.JointGrid(x='total_bill', y='tip', data=tips)
    g = g.plot_joint(plt.scatter, color='#334f6d')
    g = g.plot_marginals(sns.distplot, color='#418470', )

    plt.setp(g.ax_marg_x.get_yticklabels(), visible=True)
    plt.setp(g.ax_marg_y.get_xticklabels(), visible=True)

    plt.show()

enter image description here

The relevant part to reverse-engineer is in seaborn's sources @ seaborn/axisgrid.py (link):

# LINE 1623 #
# Set up the subplot grid
f = plt.figure(figsize=(size, size))
gs = plt.GridSpec(ratio + 1, ratio + 1)

ax_joint = f.add_subplot(gs[1:, :-1])
ax_marg_x = f.add_subplot(gs[0, :-1], sharex=ax_joint)
ax_marg_y = f.add_subplot(gs[1:, -1], sharey=ax_joint)

self.fig = f
self.ax_joint = ax_joint
self.ax_marg_x = ax_marg_x
self.ax_marg_y = ax_marg_y

# Turn off tick visibility for the measure axis on the marginal plots
plt.setp(ax_marg_x.get_xticklabels(), visible=False)
plt.setp(ax_marg_y.get_yticklabels(), visible=False)

# Turn off the ticks on the density axis for the marginal plots
plt.setp(ax_marg_x.yaxis.get_majorticklines(), visible=False)
plt.setp(ax_marg_x.yaxis.get_minorticklines(), visible=False)
plt.setp(ax_marg_y.xaxis.get_majorticklines(), visible=False)
plt.setp(ax_marg_y.xaxis.get_minorticklines(), visible=False)
plt.setp(ax_marg_x.get_yticklabels(), visible=False)
plt.setp(ax_marg_y.get_xticklabels(), visible=False)
ax_marg_x.yaxis.grid(False)
ax_marg_y.xaxis.grid(False)

Upvotes: 4

Related Questions