Stacked bars with hue in seaborn and pandas [duplicate] - pandas
This question already has answers here:
How to have clusters of stacked bars
(10 answers)
Closed 2 years ago.
I have a dataframe that looks like this:
df = pd.DataFrame(columns=["type", "App","Feature1", "Feature2","Feature3",
"Feature4","Feature5",
"Feature6","Feature7","Feature8"],
data=[["type1", "SHA",0,0,1,5,1,0,1,0],
["type2", "LHA",1,0,1,1,0,1,1,0],
["type2", "FRA",1,0,2,1,1,0,1,1],
["type1", "BRU",0,0,1,0,3,0,0,0],
["type2", "PAR",0,1,1,4,1,0,1,0],
["type2", "AER",0,0,1,1,0,1,1,0],
["type1", "SHE",0,0,0,1,0,0,1,0]])
I want to make a stacked bar with type as a hue. This is, in the x axis I want the features, and for each feature I want 2 stacked bars, one for type1 and one for type2.
For instance, here they explain how to make a stacked bar plot with seaborn when the column type is dropped. Instead, I want for each feature two stacked bars. Note: the values of App are shared for type1 and type2
For instance, if I just plot the stacked bars corresponding to type1, I get this:
I want to make a stacked bar plot where for each feature there are two stacked bars, one for type1, and the other one for type2
I don't think seaborn has a function for barplots that are both stacked and grouped. But you can do it in matplotlib itself by hand. Here is an example.
I think what you are looking for is the melt Function
d = df.drop(columns='App')
d = d.melt('type', var_name='a', value_name='b')
sns.barplot(x='a', y='b', data=d, hue='type')
Related
Stacked bar chart for a pandas df [duplicate]
This question already has answers here: Using pandas crosstab to create a bar plot (2 answers) count plot with stacked bars per hue [duplicate] (1 answer) How to have clusters of stacked bars (10 answers) Closed 7 months ago. I have a df like this and would like to plot stacked bar chart where in the x axis is Component and the y-axis shows the count by 'Major', 'Minor' etc. Component Priority 0 Browse Groups Minor 1 Notifications Major 2 BI Major 3 BI Minor 4 BI Minor For example, the first bar would have 1st component with a count of 1 minor,..so on.. and 3rd would have 'BI' in x-axis with 1 count of Major and 2 counts of Minor stacked. What is the simplest way to do this in seaborn or something similar?
You can groupby both columns and count on Priority, then unstack and plot as stacked bar chart: df.groupby(['Component', 'Priority']).Priority.count().unstack().plot.bar(stacked=True) Example: import pandas as pd df = pd.DataFrame({'Component': list('abccc'), 'Priority': ['Minor', 'Major', 'Major', 'Minor', 'Minor']}) df.groupby(['Component', 'Priority']).Priority.count().unstack().plot.bar(stacked=True) As an alternative, you can use a crosstab: pd.crosstab(df.Component, df.Priority).plot.bar(stacked=True) If you want to use seaborn (I only now saw the seaborn tag), you can use a displot: import seaborn as sns sns.displot(x='Component', hue='Priority', data=df, multiple='stack')
How to chart two different pandas data frames into one chart on matplotlib?
I have two separate sets of data using pandas: >>> suicides_sex = suicides_russia.groupby("sex")["suicides_no"].sum() >>> suicides_sex sex female 214330 male 995412 & >>> suicides_age = suicides_russia.groupby("age") >>> ["suicides_no"].sum().sort_values() >>> suicides_age age 5-14 years 8840 75+ years 74211 15-24 years 148611 25-34 years 231187 55-74 years 267753 35-54 years 479140 I want to learn how to create either a double bar chart using matplotlib or two separate bar charts where I can separate each age group by gender. How can I combine both sets of data to create either a single bar chart with double columns or two separate bar charts for each gender?
You can use boolean masks to separate the data and then group by age as you did. import matplotlib.pyplot as plt suicide_male = suicide_russia.loc[suicide_russia['sex']=='male', :] # now you basically have the same dataframe but for male only suicide_male_age = suicides_male.groupby("age")["suicides_no"].sum() plt.bar(height=suicide_male_age.values, x=np.arange(suicide_male_age.index)) plt.xticks(labels=suicide_male_age.index) plt.show() Then you can repeat the same for female. That is probably not the most efficient way of doing it, but it works. Also, I assumed the 'age' column values are strings, so I put np.arange as x positions of the bars and the values themselves as xticks. Hope it helps!
How to plot a stacked bar using the groupby data from the dataframe in python?
I am reading huge csv file using pandas module. filename = pd.read_csv(filepath) Converted to Dataframe, df = pd.DataFrame(filename, index=None) From the csv file, I am concerned with the three columns of name country, year, and value. I have groupby the country names and sum the values of it as in the following code and plot it as a bar graph. df.groupby('country').value.sum().plot(kind='bar') where, x axis is country and y axis is value. Now, I want to make this bar graph as a stacked bar and used the third column year with different color bars representing each year. Looking forward for an easy way. Note that, year column contains years from 2000 to 2019. Thanks.
from what i understand you should try something like : df.groupby(['country', 'Year']).value.sum().unstack().plot(kind='bar', stacked=True)
Can you make 4 bars with 4 columns of data using matplotlib?
I need to make a bar graph where x-axis plot the index ([1992,1993,1994,1995]) and y-axis will plot 4 columns of random data as shown below. It will generate 4 bars for 4 columns of data. How can I do that? I am pretty new to matplotlib. Please help. This is the DataFrame: np.random.seed(12345) df = pd.DataFrame([np.random.normal(32000,200000,3650), np.random.normal(43000,100000,3650), np.random.normal(43500,140000,3650),np.random.normal(48000,70000,3650)], index=[1992,1993,1994,1995]) df.head()
plot a stacked bar chart matplotlib pandas
I want to plot this data frame but I get an error. this is my df: 6month final-formula Question Text 166047.0 1 0.007421 bathing 166049.0 1 0.006441 dressing 166214.0 1 0.001960 feeding 166216.0 2 0.011621 bathing 166218.0 2 0.003500 dressing 166220.0 2 0.019672 feeding 166224.0 3 0.012882 bathing 166226.0 3 0.013162 dressing 166229.0 3 0.008821 feeding 160243.0 4 0.023424 bathing 156876.0 4 0.000000 dressing 172110.0 4 0.032024 feeding how can I plot a stacked bar based on the Question text? I tried some codes but raises error. dffinal.groupby(['6month','Question Text']).unstack('Question Text').plot(kind='bar',stacked=True,x='6month', y='final-formula') import matplotlib.pyplot as plt plt.show() Actually I want the 6month column be in the x-axis, final-formula in the y-axis and Question text being stacked. so as here I have three kind of Question text, three stacked bar should be there. and as I have 4 month, 4 bars totally. Something like this but I applied this and did not work. Am I missing something? this picture is without stacking them. its like all question text has been summed up. I want for each Question Text there be stacked.
You missed aggregation step after groupby, namely, sum() df = dffinal.groupby(['6month','Question Text']).sum().unstack('Question Text') df.columns = df.columns.droplevel() df.plot(kind='bar', stacked=True) I dropped multiindex level from columns just for legend consistency.