Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python's seaborn jointplot, different colors for each histograms

Tags:

python

seaborn

I would like to change the colors for each histogram in a jointplot, created with seaborn.

I managed to change the color for both plots using marginal_kws, but how can I set a color for one histogram each? (e. g. red and green histogram)

A minimal example of my jointplot:

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

x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0,  0.5]], 1000).T

with sns.axes_style("white"):
  g = sns.jointplot(x=x, y=y, kind="hex", stat_func=None, marginal_kws={'color': 'green'})
plt.show()

enter image description here

like image 748
sklingel Avatar asked Oct 16 '25 12:10

sklingel


2 Answers

iayork's answer about using the axes objects directly is good, although another option would be to change the color of the bars after plotting:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="white", color_codes=True)

x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0,  0.5]], 1000).T
g = sns.jointplot(x=x, y=y, kind="hex", stat_func=None, marginal_kws={'color': 'green'})
plt.setp(g.ax_marg_y.patches, color="r")

enter image description here

like image 174
mwaskom Avatar answered Oct 19 '25 01:10

mwaskom


I think you need to use jointgrid rather than jointplot here. Here's an attempt to get something close to your present plot; you will probably need to play with colors and cmaps more to make the hexbin plot look more attractive.

x, y = np.random.multivariate_normal([2, 3], [[0.3, 0], [0,  0.5]], 1000).T

def hexbin(x, y): 
    plt.hexbin(x, y, gridsize=20, cmap='Blues')

with sb.axes_style("white"):
    g = sb.JointGrid(x=x, y=y, ylim=(0,6))
    g = g.plot_joint(hexbin)
    g.ax_marg_x.hist(x, color="b", alpha=.6) 
    g.ax_marg_y.hist(y, color="r", alpha=.6, orientation="horizontal")

enter image description here

like image 28
iayork Avatar answered Oct 19 '25 01:10

iayork