Python/Python For Analytics
[ Python ] pandas를 이용한 bar graph (stacked)
Pydole
2022. 3. 1. 12:33
1. 기본 bar 그래프 그리기
import pandas as pd
month = [ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec' ]
data = {
"Banana":[52, 83, 82, 53, 99, 94, 83, 74, 87, 70, 63, 74],
"Orange":[99, 71, 77, 57, 87, 50, 59, 58, 63, 76, 51, 88],
"mango":[50, 71, 93, 82, 70, 58, 55, 97, 76, 52, 97, 83],
}
df = pd.DataFrame(data,index=month)
df.plot(kind="bar",figsize=(15,10))
2. 스택그래프를 그리려면, stacked = True 추가
df.plot(kind="bar",stacked=True,figsize=(15,10)) # stacked
3. 그래프 값 표기
import pandas as pd
month = [ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec' ]
data = {
"Banana":[52, 83, 82, 53, 99, 94, 83, 74, 87, 70, 63, 30],
"Orange":[99, 71, 77, 57, 87, 50, 59, 58, 63, 76, 51, 88],
"mango":[50, 71, 93, 82, 70, 58, 55, 97, 76, 52, 97, 83],
}
df = pd.DataFrame(data,index=month)
ax = df.plot(kind="bar",stacked=True,figsize=(15,10)) # stacked
for c in ax.containers:
labels = [ x.get_height() for x in c ]
ax.bar_label(c, labels=labels, label_type='center')