0

I have the following plotly histogram, where I want to show the distribution of the values for a variable which can have only two categorical values:

enter image description here

I am currently plotting it with fig = px.histogram(pd.DataFrame(hist_data, columns=['result']), x="result", histfunc='count', histnorm='probability density') and the plot is correct, but both bins are blue and I would like to have them of two different colors. So I tried to do the following: fig = px.histogram(pd.DataFrame(hist_data, columns=['result']), x="result", histfunc='count', histnorm='probability density', color="result", color_discrete_map={value1: color1, value2: color2}) In this way the two bars are correctly colored, but the plot is not anymore correct!

enter image description here

The two bars have both value 1, they don't represent anymore the distribution of the values of the variable result.

edit: for maximum clearness, hist_data is a list containing only no disease or cardiovascular disease as values:

hist_data = ['no disease',
 'cardiovascular disease',
 'no disease',
 'no disease',
 'cardiovascular disease',
 'cardiovascular disease',
 'cardiovascular disease',
 'cardiovascular disease',
 'cardiovascular disease',
 'no disease', ... ]
Vitto
  • 361
  • 3
  • 17
  • Please make you question reproducible by providing all necessary code and a sample of your data as described [here](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) – vestland Sep 19 '21 at 20:46
  • If you don't mind adding a column of colors to the dataframe, you can do it in the following way. `df=px.data.tips();df['color']=df['day'].map({'Sun':'red','Sat':'blue','Thur':'green','Fri':'yellow'});fig = px.histogram(df, x="day", color='color');fig.show()` – r-beginners Sep 20 '21 at 08:03
  • @r-beginners it still doesn't work correctly, it gives the same result I obtained above. I noticed that if I remove the `histnorm='probability density'` parameter it works correctly, showing the count, but this is not what I want to obtain, I would like to have the probability distribution, like in the first picture in the question – Vitto Sep 20 '21 at 08:59

0 Answers0