Tim Williams
Tim Williams

Reputation: 303

Change position of marginal axis in seaborn jointplot

Seaborn by default plots the marginal distributions on the top and right of the main plot. Is it possible to change this location (e.g., to bottom and left)?

A minimal example, using the seaborn documentation:

tips = sns.load_dataset("tips")
g = sns.jointplot(x="total_bill", y="tip", data=tips)

gives...

enter image description here

Upvotes: 2

Views: 1664

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

It is a bit tedious, but you can adapt this example to your needs. It uses a make_axes_locatable divider. Changing this from top to bottom and from right to left is no problem, but then you need to change the labelling and ticks on all axes.

import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable


tips = sns.load_dataset("tips")
x = tips["total_bill"]
y = tips["tip"]

fig, axScatter = plt.subplots(figsize=(5.5, 5.5))
fig.subplots_adjust(.1,.1,.9,.9)

axScatter.scatter(x, y)

divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("bottom", 1.2, pad=0.1, sharex=axScatter)
axHisty = divider.append_axes("left", 1.2, pad=0.1, sharey=axScatter)

# make some labels invisible
axHistx.tick_params(labelbottom=False, bottom=False, 
                    left=False, labelleft=False, right=True, labelright=True)
axHisty.tick_params(labelleft=False, left=False, 
                    bottom=False, labelbottom=False, top=True, labeltop=True)
axHistx.invert_yaxis()
axHisty.invert_xaxis()
axScatter.xaxis.tick_top()
axScatter.yaxis.tick_right()
axScatter.xaxis.set_label_position('top')
axScatter.yaxis.set_label_position('right')
axScatter.set(xlabel="Total Bill", ylabel="Tip")

axHistx.hist(x, bins=16, density=True)
axHisty.hist(y, bins=16, density=True, orientation='horizontal')

plt.show()

enter image description here

Upvotes: 1

Related Questions