Lucy
Lucy

Reputation: 21

How to show only what is in the legend for marginal plots on Seaborn Jointplot?

I am looking to only have the marginal plots show only the 6 colors and value ranges that are shown in the legend. Right now, it is showing many more and looks confusing in the plot to determine where one line tracks. I am using Seaborn jointplot for marginal colored histogram plots. See plot here! :)

Here is my code in python for some pandas data frame(df):

mask = df['rf_number'].notnull()

%matplotlib inline
plt.figure(figsize=(10,10)) 

#sns.scatterplot(x=df['x'][mask], y=df['y'][mask],color='black', linewidth=0)
colored_hist = sns.jointplot(data= df[mask], x='x', y= 'y', hue='rf_number', 
                             kind='scatter', palette='Spectral', linewidth=0, 
                             size=12, xlim=[-11,11], ylim=[-10,10],
                             marginal_kws=dict(fill=False)
                             ) 


Any help appreciated!

Upvotes: 1

Views: 104

Answers (1)

JohanC
JohanC

Reputation: 80449

By rounding 'rf_number' to a multiple of 0.2, only these rounded values will be taken into account for the kdeplot.

Note that you need to remove plt.figure(figsize=(10,10)) because sns.jointplot() creates its own figure. sns.jointplot(..., height=10) would make the fig size 10x10.

import seaborn as sns
import numpy as np
import pandas as pd

# create some random test data
df = pd.DataFrame({'x': np.random.randn(1000) * 3, 'y': np.random.randn(1000) * 3})
df['rf_number'] = ((np.sin(df['x'] * np.cos(df['y'] / 7) + 4) + np.sin(df['y'] / 3 + 1)) ** 2) / 4

# round the hue column to multiples of 0.2
df['rf_rounded'] = (df['rf_number'] * 5).round() / 5

mask = df['rf_rounded'].notnull()
colored_hist = sns.jointplot(data=df[mask], x='x', y='y', hue='rf_rounded',
                             kind='scatter', palette='Spectral', linewidth=0,
                             size=12, xlim=[-11, 11], ylim=[-10, 10],
                             marginal_kws=dict(fill=False),
                             height=10)
sns.move_legend(colored_hist.ax_joint, loc='best', title='DEE probability')

sns.jointplot with 6 hue values

Upvotes: 0

Related Questions