-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStackbar.py
More file actions
31 lines (25 loc) · 974 Bytes
/
Stackbar.py
File metadata and controls
31 lines (25 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pandas as pd
import plotly
import plotly.graph_objs as go
from plotly.offline import *
# To initiate ploty to run offline
init_notebook_mode(connected=True)
# Import data
df = pd.read_csv('../Data/expense_everybody.csv')
df = df.groupby(['name','expense_category'])['amount'].sum().reset_index()
# Round number for amount column
df['amount'] = df['amount'].round(2)
# Prepare data
data =[]
for cate in df['expense_category'].unique():
df_temp = df[df['expense_category']==cate]
data.append(go.Bar(name=cate,
x=df_temp['name'], y=df_temp['amount'],
text=df_temp['amount'], textposition='auto',
textfont=dict(color='white')))
fig_title = 'Everybody\'s Expense'
layout = dict(title={'text':fig_title, 'x':0.5},
barmode='stack',
xaxis=dict(tickmode='linear',categoryorder='total descending'))
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='stackbar.html')