简体   繁体   中英

Set up the size of backgound colour plot to the size of axes in seaborn jointplot

I am using seaborn for create plot according to this example .

import numpy as np
import pandas as pd
import seaborn as sns
sns.set(style="white")

rs = np.random.RandomState(5)
mean = [0, 0]
cov = [(1, .5), (.5, 1)]
x1, x2 = rs.multivariate_normal(mean, cov, 500).T
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")

g = sns.jointplot(x1, x2, kind="kde", size=7, space=0)

However, when I change the last line of code to

g = sns.jointplot(x1, x2, kind="kde", size=7, space=0, xlim=(-5,5), ylim=(-5,5))

the background color does not change correctly: 在此处输入图片说明

How I can fix the background color so that it fills the whole plot?

You'll need to tell the underlying function ( kdeplot ) to extend its KDE estimate farther out. This is accomplished through the cut argument, which is a function of the KDE bandwidth. It defaults to 3, and there's no obvious way to tell exactly how you need to set it, but it shouldn't be that hard to play around and find values that work. When using jointplot , you'll want to pass this in the joint_kws dictionary so that it gets sent to the appropriate plotting function.

sns.jointplot(x1, x2, kind="kde", size=7, space=0,
              joint_kws={"cut": 10},
              xlim=(-5,5), ylim=(-5,5))

Voila:

在此处输入图片说明

This is very close, but I can't quite match the colour. If you find the colourmap in the cm module ("cool"?) you can find the exact colour.

ax = plt.gcf().axes[0]
ax.set_axis_bgcolor((.93,.93,1))

If you are doing this interactively, you will need a plt.draw() to have the new colour show.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM