0

Is there a way to specify both hue and have the marginal histograms be plotted with stat=density ? It seems I can have one or the other -- it will graph either without the hue kwa or without stat='density', but it errors out when I include both parameters.

Here is some example code:

penguins=sns.load_dataset('penguins')
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue='species',marginal_kws=dict(color='r',stat='density'))

And here is the error:

`---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-e8d515eeeab0> in <module>
----> 1 sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue='species',marginal_kws=dict(color='r',stat='density'))

12 frames
/usr/local/lib/python3.8/dist-packages/matplotlib/artist.py in _update_property(self, k, v)
    999                 func = getattr(self, 'set_' + k, None)
   1000                 if not callable(func):
-> 1001                     raise AttributeError('{!r} object has no property {!r}'
   1002                                          .format(type(self).__name__, k))
   1003                 return func(v)

AttributeError: 'PolyCollection' object has no property 'stat'`

Thanks!

I can get a jointplot with two colors which are designated by a categorical value in my dataframe. However, I would like for the marginal histograms to be plotted by density as specified in histplot but not absolute value, because one of the categories has many more entries than the other. I cannot get both hue and stat=density without the code erroring out as above.

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • With `hue`, you get density curves using `kdeplot` on the marginal axes, so not `histplot`. – BigBen Feb 09 '23 at 14:54
  • Maybe in two steps: `g = sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue='species')` and `g.plot_marginals(sns.histplot, stat='density')` – JohanC Feb 09 '23 at 16:26
  • 1
    What you want to accomplish is a little confusing — you're using `hue` but also passing `marginal_kws(color="r")`. Are you expecting to get one histogram (i.e., disabling `hue` for the marginal plots) or three red histograms? – mwaskom Feb 09 '23 at 18:28
  • Thanks, I was able to get what I needed by modifying kdeplot instead of histplot. kdeplot did what I needed, at least for now. – jialumaomao Feb 10 '23 at 21:19

0 Answers0