-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGroupbar.py
More file actions
37 lines (31 loc) · 1.14 KB
/
Groupbar.py
File metadata and controls
37 lines (31 loc) · 1.14 KB
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
32
33
34
35
36
37
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)
# Colours for the category
colours = ['green','black']
# Bar Width
widths = [0.5,0.25]
# Prepare data
data =[]
for cate, colour, width in zip(df['expense_category'].unique(),colours, widths):
df_temp = df[df['expense_category']==cate]
data.append(go.Bar(name=cate,
x=df_temp['name'], y=df_temp['amount'],
marker_color=colour,
width=[width]*len(df_temp['name']),
text=df_temp['amount'], textposition='auto',
textfont={'color':'white'}))
fig_title = 'Everybody\'s Expense'
layout = dict(title={'text':fig_title, 'x':0.5},
barmode='group',
xaxis=dict(tickmode='linear',tickangle=-45))
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='groupbar.html')