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')

__results___27_0

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

3 Likes

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.

Thanks!

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

A more tedious response to that will be to turn the value_count into a dataframe.