-2

I'm sure I'm just searching with the wrong keywords here, but I have the following code:

for var in ['lsc596']:
    fig, ax = plt.subplots()
    plt.title('Distributional shift of {}'.format(var))
    for rep in ['2022-06', '2022-07']:
        sns.histplot(data[(data.rep_date == rep)&(data.lsc596>0)&(data.lsc596<100)][var], bins = 100,ax=ax, kde=False)
  plt.legend(['2022-06', '2022-07'])
  plt.show();

Which produces two histograms, one for each month, on the same axes. However, they are also very close to being indistinguishable in colour, and I'm looking for an option that will let me manually change them to be more distinct.

BigBen
  • 46,229
  • 7
  • 24
  • 40
user13948
  • 443
  • 1
  • 6
  • 14
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Sep 01 '22 at 19:32
  • Additionally, you are incorrectly using `sns.histplot`, as iterating through different column values will overlay the histograms in a manner which makes them unreadable. You should use `g = sns.displot(data=data[data.lsc596.between(0, 100)], x='lsc596', bins=100, kde=False, hue='rep_date', aspect=2)` or `g = sns.displot(data=data[data.lsc596.between(0, 100)], x='lsc596', bins=100, kde=False, col='rep_date')`. Different colors may be specified for each group, and `alpha=` can be set for transparency. However, there is no parameter for changing the color of the overlapping regions. – Trenton McKinney Sep 01 '22 at 19:33

1 Answers1

1

have you considered seaborn displot()? The default setting is histplot. This will give you different colors for one/both bars and distinct colors for each, so you will know which one has more data in the bin. If you want, you can use multiple="stack" to keep them one on top of another. Documentation here.

An example using penguins dataset.

penguins = sns.load_dataset("penguins")
sns.displot(data=penguins, x="flipper_length_mm", hue="sex", alpha = 0.3)#, multiple="stack")

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • What is the grey color even though there are only 2 classifications? And how can it be removed? – daniel ajj Dec 10 '22 at 13:56
  • Hi @danielajj - `alpha=0.3` gives some transparency. Change it to 1 and it will show solid colors... gray is just the area where both bars are present. Remember that making the bars solid mean that you may not be able to see both bars correctly – Redox Dec 12 '22 at 09:31