Seaborn Jointplot add colors for each class

seaborn jointplot pearsonr
seaborn color palette
seaborn jointplot title
matplotlib colors
seaborn jointplot hue
seaborn scatter plot
seaborn jointplot axis limits
seaborn jointplot bins

I want to plot the correlation plot of 2 variables using seaborn jointplot. I have tried a lot of different things but I am not able to add colors to the points according to class.

Here is my code:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()

X = np.array([5.2945 , 3.6013 , 3.9675 , 5.1602 , 4.1903 , 4.4995 , 4.5234 ,
              4.6618 , 0.76131, 0.42036, 0.71092, 0.60899, 0.66451, 0.55388,
              0.63863, 0.62504, 0.     , 0.     , 0.49364, 0.44828, 0.43066,
              0.57368, 0.     , 0.     , 0.64824, 0.65166, 0.64968, 0.     ,
              0.     , 0.52522, 0.58259, 1.1309 , 0.     , 0.     , 1.0514 ,
              0.7519 , 0.78745, 0.94873, 1.0169 , 0.     , 0.     , 1.0416 ,
              0.     , 0.     , 0.93648, 0.92801, 0.     , 0.     , 0.89594,
              0.     , 0.80455, 1.0103 ])

y = np.array([ 93, 115, 107, 115, 110, 107, 102, 113,  95, 101, 116,  74, 102,
               102,  78,  85, 108, 110, 109,  80,  91,  88,  99, 110, 108,  96,
               105,  93, 107,  98,  88,  75, 106,  92,  82,  84,  84,  92, 115,
               107,  97, 115,  85, 133, 100,  65,  96, 105, 112, 107, 107, 105])

ax = sns.jointplot(X, y, kind='reg' )
ax.set_axis_labels(xlabel='Brain scores', ylabel='Cognitive scores')
plt.tight_layout()
plt.show()

Now, I want to add colors for each point according to a class variable classes.

The obvious solution is to let the regplot only draw the regression line, but not the points and add those via a usual scatter plot, which has the color c argument.

g = sns.jointplot(X, y, kind='reg', scatter = False )
g.ax_joint.scatter(X,y, c=classes)

seaborn.jointplot, seaborn. jointplot (x, y, data=None, kind='scatter', stat_func=None, This function provides a convenient interface to the JointGrid class, with several Color used for the plot elements. Draw a scatterplot, then add a joint density estimate:. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

I managed to find a solution that is exactly what I need. Thank to @ImportanceOfBeingErnest that gave me the idea to let the regplot only draw the regression line.

Solution:

import pandas as pd

classes = np.array([1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.,
                    2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 
                    2., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 
                    3., 3., 3., 3., 3., 3., 3.])

df = pd.DataFrame(map(list, zip(*[X.T, y.ravel().T])))
df = df.reset_index()
df['index'] = classes[:]

g = sns.jointplot(X, y, kind='reg', scatter = False )
for i, subdata in df.groupby("index"):
    sns.kdeplot(subdata.iloc[:,1], ax=g.ax_marg_x, legend=False)
    sns.kdeplot(subdata.iloc[:,2], ax=g.ax_marg_y, vertical=True, legend=False)
    g.ax_joint.plot(subdata.iloc[:,1], subdata.iloc[:,2], "o", ms = 8)
plt.tight_layout()
plt.show()

can jointplot visualize different classes? · Issue #365 · mwaskom , Is it possible to have different color for different classes in joinplot? Even better, would be possible to have a color for each value after a function is petebachant added a commit to petebachant/seaborn that referenced this  where a different color is used for each different value in the category column. Even better, would be possible to have a color for each value after a function is mapped to the column? In my example, assume I would like to have a color for odd values, and another one for even values of the column, i.e.

To build off Ernest's answer:

After you set scatter = False in sns.jointplot build the scatterplot using sns.scatterplot with the hue = classes argument equal to the categorical variable array. I find it cleanest to merge your data into a pandas dataframe with the columns x, y and classes and use this as the data for the scatterplot, but you don't have to do it this way...

classes = np.array([1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.,
                    2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 
                    2., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 
                    3., 3., 3., 3., 3., 3., 3.])

# make them look a little more 'categorical'
classes = classes.astype('int')

x = np.array([5.2945 , 3.6013 , 3.9675 , 5.1602 , 4.1903 , 4.4995 , 4.5234 ,
              4.6618 , 0.76131, 0.42036, 0.71092, 0.60899, 0.66451, 0.55388,
              0.63863, 0.62504, 0.     , 0.     , 0.49364, 0.44828, 0.43066,
              0.57368, 0.     , 0.     , 0.64824, 0.65166, 0.64968, 0.     ,
              0.     , 0.52522, 0.58259, 1.1309 , 0.     , 0.     , 1.0514 ,
              0.7519 , 0.78745, 0.94873, 1.0169 , 0.     , 0.     , 1.0416 ,
              0.     , 0.     , 0.93648, 0.92801, 0.     , 0.     , 0.89594,
              0.     , 0.80455, 1.0103 ])

y = np.array([ 93, 115, 107, 115, 110, 107, 102, 113,  95, 101, 116,  74, 102,
               102,  78,  85, 108, 110, 109,  80,  91,  88,  99, 110, 108,  96,
               105,  93, 107,  98,  88,  75, 106,  92,  82,  84,  84,  92, 115,
               107,  97, 115,  85, 133, 100,  65,  96, 105, 112, 107, 107, 105])

sns.jointplot(x, y, kind='reg', scatter = False )
sns.scatterplot(x, y, hue=classes)

seaborn.jointplot, Seaborn's jointplot displays a relationship between 2 variables (bivariate) as well as This plot is a convenience class that wraps JointGrid. Change the color. color matplotlib color, optional. Color used for the plot elements. height numeric, optional. Size of the figure (it will be square). ratio numeric, optional. Ratio of joint axes height to marginal axes height. space numeric, optional. Space between the joint and marginal axes. dropna bool, optional. If True, remove observations that are

python: Seaborn Jointplot add colors for each class, I want to plot the correlation plot of 2 variables using seaborn jointplot. I have tried a lot of different things but I am not able to add colors to the  When importing seaborn, the default color cycle is changed to a set of ten colors that evoke the standard matplotlib color cycle while aiming to be a bit more pleasing to look at. current_palette = sns . color_palette () sns . palplot ( current_palette )

Visualization with Seaborn, In order to visualize data from a Pandas DataFrame , you must extract each Series a simple random-walk plot in Matplotlib, using its classic plot formatting and colors. with sns.axes_style('white'): sns.jointplot("x", "y", data, kind='kde'); For the purpose of our Seaborn plotting utilities, let's next add columns that give the  The easiest way to do this in seaborn is to just use the jointplot() function, which creates a multi-panel figure that shows both the bivariate (or joint) relationship between two variables along with the univariate (or marginal) distribution of each on separate axes.

#100 Calling a color with seaborn – The Python Graph Gallery, However, here is a list of the available colors if you want to call them by their name (source). #196 Matplotlib colors. Previous Post. #197 Available color palettes  Tidy (“long-form”) dataframe where each column is a variable and each row is an observation. palette palette name, list, or dict, optional. Colors to use for the different levels of the hue variable. Should be something that can be interpreted by color_palette(), or a dictionary mapping hue levels to matplotlib colors. hue_order list, optional

Comments
  • Please do not answer your own question inside the question. If you think the existing answer(s) do not answer the question, and feel that you have a better/different solution, please provide that solution as answer to your question.
  • I agree. I just did what you suggested.
  • Based on your idea, I managed to solve my problem. Now, at a last step, is there any way to add a legend for the color-coding ?
  • That is a frequently asked question, because there is currently no direct and easy solution for adding legends to scatters. Possible ways are e.g. this one, this one, or this one. Future versions of matplotlib may contain a better solution I designed.
  • Thank you for the references