How can I depict a variable as a color in pandas?

Evan

Consider the titanic dataset, I'd like to plot a bar graph showing how sex and passenger class impact survival. I used the code below to groupby sex and passenger class to produce the attached figure. How can I eliminate the gender variable from the x-axis and express it as a color (shading the men with blue and the women with red, for example)?

import pandas as pd
data = pd.read_csv("train.csv")
data[['Sex','Pclass','Survived']].groupby(['Sex','Pclass']).sum().plot.bar()

enter image description here

YevKad

This should work:

data=data[['Sex','Pclass','Survived']].groupby(['Sex','Pclass']).sum().reset_index()
data=data.pivot(index='Pclass',columns='Sex',values='Survived')
data.plot(kind='bar',stacked=True,color={'male':'b','female':'r'})

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make an array of frequencies accurately depict a decoded mp3 file?

How can I fill and color based on a variable

how can i use a backgroundresource with variable color

.tmux.conf: How can I conditionally set a color variable?

How can i change css variable to scss color type?

How can I set a string variable as a color in VBA?

How can I change the color of the variable in JS dom?

how can I pass a typescript color variable to an html div?

How can I color the empty rows in and export to an excel file with pandas?

How can I assign a color to a specific value in a column of a pandas dataframe?

How can I return the variable color value so that I can apply it in an onclick event?

How can I loop through the and adjust the variable every month in pandas?

How can I assign a pandas dataframe to a class variable?

How can I add multiple variable arrays to columns in a pandas dataframe?

Python pandas, how can I pass a colon ":" to an indexer through a variable

Can I set a variable color for a link in CSS?

How can i convert color?

How can i color the codes

How can I retain desired order in R Plotly bar chart with color variable

How can I get color decorations for CSS variable references to work in VS Code?

How can I inject a specific color as a .less variable into my CSS using Django?

How can I change the color to a variable other than cluster number in fviz_silhouette

How can I add fill gradient color to a density plot in R with only one variable

How do I change the color of a variable in CSS?

How can I tell python pandas bar plot to pick color from x label values?

How can I specify the color of the kmeans clusters in 3D plot (Pandas)?

How can I set the color of a bar (matplotlib) according to a value in a dataframe (pandas)?

Can I set variable column widths in pandas?

How to depict a Network with Donut shaped vertices with Igraph?