Color as the third axis in seaborn jointplots

665 Views Asked by At

I have three quantities, x, y, z, which I would like to see the distribution of two of them and have the value of the third one as color of each point. I use seaborn.jointplot to plot distributions of x and y and use the z as the color parameter in the jointplot. But, still I need to change the color palette and add a colorbar somewhere outside the plot. This is a minimal example of what I did:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

x = np.random.normal(loc=0, scale=1, size=1000)
y = np.random.normal(loc=0, scale=1, size=1000)
z = x**2  + y**2

plt.figure(num=1, figsize=(8, 5))
sns.jointplot(x=x, y=y, c=z, joint_kws={"color":None})
plt.show()
1

There are 1 best solutions below

0
On

by "changing the color palette", do you mean changing the colormap? If so, that can be done simply by passing the relevant info to joint_kws:

g = sns.jointplot(x=x, y=y, c=z, joint_kws={"color":None, 'cmap':'viridis'})

For the colorbar, it all depends on where you want it. Here is a solution that lets the axes to be resized automatically in order to make room. See the documentation for colorbar() to adjust the parameters as needed:

g.fig.colorbar(g.ax_joint.collections[0], ax=[g.ax_joint, g.ax_marg_y, g.ax_marg_x], use_gridspec=True, orientation='horizontal')

full code:

plt.figure(num=1, figsize=(8, 5))
g = sns.jointplot(x=x, y=y, c=z, joint_kws={"color":None, 'cmap':'viridis'})
g.fig.colorbar(g.ax_joint.collections[0], ax=[g.ax_joint, g.ax_marg_y, g.ax_marg_x], use_gridspec=True, orientation='horizontal')
plt.show()

enter image description here