0

I'd like to color my histogram according to my palette. Here's the code I used to make this, and here's the error I received when I tried an answer I found on here.

g = sns.jointplot(data=emb_df, x='f0', y='y', kind="hist", hue='klabels', palette='tab10', marginal_kws={'hist_kws': {'palette': 'tab10'}})
plt.show()

 UserWarning: The marginal plotting function has changed to `histplot`, which does not accept the following argument(s): hist_kws.

enter image description here

I have also tried this:

plt.setp(g.ax_marg_y.patches, color='grey')

But this does not color my histogram according my 'klabels' parameter, just a flat grey.

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Bigboss01
  • 438
  • 6
  • 21
  • @JohanC thank you johan. If possible- do you have a similarly straightforward solution for plotting these side by side as subplots? Either way- feel free to make this an answer so that I may accept. – Bigboss01 Oct 26 '21 at 15:16

1 Answers1

3

The marginal plot is colored by default using the same palette with corresponding hue. So, you could just run it without marginal_kws=. The marginal_kws= go directly to the histplot; instead of marginal_kws={'hist_kws': {'palette': 'tab10'}}, the correct use would be marginal_kws={'palette': 'tab10'}. If you would like stacked bars, you could try marginal_kws={'multiple': 'stack'})

If you want the marginal plots to be larger, the ratio= parameter can be altered. The default is 5, meaning the central plot is 5 times as large as the marginal plots.

Here is an example:

from matplotlib import pyplot as plt
import seaborn as sns

iris = sns.load_dataset('iris')
g = sns.jointplot(data=iris, x='petal_length', y='sepal_length', kind="hist", hue='species', palette='tab10',
                  ratio=2, marginal_kws={'multiple': 'stack'})
sns.move_legend(g.ax_joint, loc='upper left') # optionally move the legend; seaborn >= 0.11.2 needed
plt.show()

sns.jointplot with histograms

To have these plots side-by-side as subplots, you can call the underlying sns.histplot either with both x= and y= filled in (2D histogram), only x= given (horizontal histogram) or only y= given (vertical histogram).

from matplotlib import pyplot as plt
import seaborn as sns

iris = sns.load_dataset('iris')
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15, 4))
sns.histplot(data=iris, x='petal_length', y='sepal_length', hue='species', palette='tab10', legend=False, ax=ax1)
sns.histplot(data=iris, x='petal_length', hue='species', palette='tab10', multiple='stack', legend=False, ax=ax2)
sns.histplot(data=iris, y='sepal_length', hue='species', palette='tab10', multiple='stack', ax=ax3)
sns.move_legend(ax3, bbox_to_anchor=[1.01, 1.01], loc='upper left')
plt.tight_layout()
plt.show()

jointplot as separate subplots

JohanC
  • 71,591
  • 8
  • 33
  • 66