✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Countplot with plotly express

Hi there! I was wonder if there is an easy way of doing a countplot using plotly express from a pandas dataframe, as it is possible to do with seaborn? I looked for several possibilities using bar plots and histograms, but none seems to be able to work in that way. Thanks!

For example (using the well-known Titanic dataset):
sns.countplot(x='Survived', data=train, hue='Sex')


px.histogram(train, x="Survived", color="Sex") should do it?


I was just writing the response when you beat me to it @nicolaskruchten :slight_smile:

1 Like

Hi! It almost worked out. I needed to change just the barmode parameter:

px.histogram(train_data, x='Survived', color="Sex", barmode='group')

I started trying with the histogram, but then I abandoned it because when I didn’t use the color parameter (just one column for each target), the columns appeard all joined, and in the x axis a continue label was shown. So I thought that histogram was not the way to go.

From I’ve learned just now it’s an unrelated issue regarding how it treats the numerical values even when they are categorical in the dataframe. I have just read your comments in this GitHub issue:

I understand the point there. But nevertheless it would be nice to be able to control some how, how the API treats this kind of variables, without needing to update the layout.


This doesn’t work, because once you use the value_counts() the sex is no longer a name of a column in dataframe.