2

This might be a simple task but I am new to plotting in python and is struggling to convert logic into code. I have 2 columns like below. 0 mean not churned and 1 means churned. gender is an object column and churned is a category column

gender|churned
--------------
male   |0
male   |1
female |0
female |1
female |1
male   |1

I simply want a stacked bar graph (please correct me if this is not the right choice of graph) with 0 and 1 on x axis (churn column) and for each of those 2 categories I want a stacked bar graph with 2 different colours for each gender showing the total number of males and females under 0 (not churned) and total number of males and females under 1(churned).

I tried:

df.Churn.value_counts().plot(kind='bar') 

it gave me the total count for each 0 and 1 but i need it divided by gender aswell.

Hope I am making sense

1
  • you have 2 tables, churned and not churned right? Commented May 19, 2020 at 10:32

3 Answers 3

3

You can table it:

import pandas as pd
df = pd.DataFrame({'gender':['male','male','female','female','female','male'],
                'churned':[0,1,0,1,1,1]})
pd.crosstab(df['churned'],df['gender']).plot(kind="bar",stacked=True)

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Great solution @StupidWolf. Please can you share what would be the code to do the same in matplotlib?
sorry i might be missing something. You are calling matplotlib onto a pandas dataframe with this.. Do you actually mean doing it from scratch using matplotlib
Sorry let me be clear. I am learning matplotlib and excuse my ignorance. When i try your code it works perfectly but when i try to plot this in a specific subplot it is not working and is plotting outside as this is not plotted using the pyplot module. hope it makes sense?
fig, axs = plt.subplots(1, 2) pd.crosstab(df['churned'],df['gender']).plot(kind="bar",stacked=True,ax=axs[0])
|
3

If you wanted an interactive version then you could use hvplot:

import pandas as pd
import hvplot.pandas #noqa
# 1. CREATE DF:
churn = pd.DataFrame({"gender":["male","male","female","female","female","male"],
                     "churned":[0,1,0,1,1,1]})
churn

Out[2]: 
   gender  churned
0    male        0
1    male        1
2  female        0
3  female        1
4  female        1
5    male        1
# 2. GROUP THE DATA BY "churned" THEN "gender":
plot_me = churn.groupby(["churned","gender"])[["gender"]].count().rename(columns={"gender":"count"})
plot_me

Out[3]: 
                count
churned gender       
0       female      1
        male        1
1       female      2
        male        2
# 3. PLOT:
plot_me.hvplot.bar(stacked=True,color=["maroon","teal"],line_width=3,
                   line_color="black",height=350,width=500)

Out[4]: 

enter image description here

1 Comment

Thank you @mmRmmR. I am learning matplotlib and excuse my ignorance. I would like to use the pyplot module or seaborn to plot this as that will help me with the learning. Please can you help me in how to plot the same using the pyplot module or seaborn? hope i am making sense.many thanks
0
import seaborn as sns

sns.countlot(data=df, x='churned' ,hue = 'gender') 

enter image description here

With this you can't you stacking

But you can do the same thing, plus stacking with histplot. Hist is usually used for numerical variables, but it does counting so it will do a job

sns.histplot(data=df, x='churned', hue='gender', nultiple='stack')

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.