Issue
I got a DF with two column that shows if a person have dept in this month and the previous one.
Moroso morosoant
1 0
1 1
0 0
So I'd like to plot the amount of paid and unpaid for both cols.
I've tried this for ploting the values for just on
sn.catplot(kind="bar",x="MOROSO",y="MOROSO",data=pd.DataFrame(df["MOROSO"].value_counts()))
I would like to add the morosoant col next to the moroso column.
Solution
Seaborn's works easiest with a dataframe in "long form". Pandas' melt()
can combine the columns. Using kind='count'
, seaborn does the counting. To have names for the 0
s and 1
s in the legend, .replace()
can change them to strings. Matplotlib's new bar_label
function labels the bars with their value.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df = pd.DataFrame({'Moroso': np.random.randint(0, 2, 100),
'Morosoant': np.random.randint(0, 2, 100)})
df_long = df.melt(value_vars=['Moroso', 'Morosoant'], value_name='Legend').replace({'Legend': {0: 'No', 1: 'Yes'}})
sns.set_style('whitegrid')
g = sns.catplot(kind='count', data=df_long,
x='variable', hue='Legend', palette='Set1', height=4, aspect=2)
g.set(xlabel='')
for ax in g.axes.flat:
for bars in ax.containers:
ax.bar_label(bars)
plt.subplots_adjust(left=0.07, bottom=0.15)
plt.show()
PS: If df
looks like:
Moroso Morosoant
0 0 1
1 1 1
2 1 0
3 1 1
4 0 1
Then df_long
(before the replace) will look like:
variable Legend
0 Moroso 0
1 Moroso 1
2 Moroso 1
3 Moroso 1
4 Moroso 0
5 Morosoant 1
6 Morosoant 1
7 Morosoant 0
8 Morosoant 1
9 Morosoant 1
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.