Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

python - Stacked Bar Chart count by a certain column which contains string values

I used seaborn to get a barchart where my data is counted by the column MONTH.

sns.catplot(x = 'MONTH',kind="count",data=df)

What I now wanted to get is a stacked barchart where the data is counted by another column TASKTYPE. The entries in this column are strings.

My dataframe looks like this:

MONTH TASKTYPE
09/19 A
09/19 B
10/19 B
10/19 B
01/20 A

I now want to get a stacked bar chart with MONTH on the x-axis, count on y-axis and the bars stacked by TASKTYPE. I think I could do something like this using pandas.

How could I do this?

question from:https://stackoverflow.com/questions/66061962/stacked-bar-chart-count-by-a-certain-column-which-contains-string-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
df.groupby('MONTH')['TASKTYPE'].value_counts().unstack().plot.bar(stacked=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...