-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractiveSalesAnalysis.py
88 lines (83 loc) · 2.78 KB
/
InteractiveSalesAnalysis.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# Convert ORDER_YEAR and ORDER_MONTH to datetime for easier plotting
invoices['ORDER_DATE'] = pd.to_datetime(invoices['ORDER_YEAR'].astype(str)
+ '-' + invoices['ORDER_MONTH'].astype(str) + '-01')
# Plot 1: Interactive Line Plot using Plotly
fig1 = px.line(
invoices,
x = 'ORDER_DATE',
y = 'InvoiceTotal',
color = 'FullName',
labels = {'ORDER_DATE': 'Date', 'InvoiceTotal': 'Invoice Total', 'FullName': 'Employee'},
title = 'Comparison of Monthly Sales Across Employees'
)
# Update layout to move the legend outside the plot
fig1.update_layout(
legend = dict(
title = 'Employee',
x = 1.05,
y = 1,
traceorder = 'normal'
),
margin = dict(l = 0, r = 150, t = 50, b = 50)
)
# Show plot
fig1.show()
# Aggregate data to yearly totals
yearly_totals = invoices.groupby(['FullName', 'ORDER_YEAR'])['InvoiceTotal'].sum().reset_index()
# Plot 2: Interactive Bar Plot using Plotly
fig2 = px.bar(
yearly_totals,
x = 'ORDER_YEAR',
y = 'InvoiceTotal',
color = 'FullName',
labels = {'ORDER_YEAR': 'Year', 'InvoiceTotal': 'Invoice Total', 'FullName': 'Employee'},
title = 'Total Sales Per Year for Each Employee'
)
# Update layout to move the legend outside the plot
fig2.update_layout(
legend = dict(
title = 'Employee',
x = 1.05,
y = 1,
traceorder = 'normal'
),
margin = dict(l = 0, r = 150, t = 50, b = 50)
)
# Show plot
fig2.show()
# Calculate the rankings for each year
yearly_totals['Rank'] = yearly_totals.groupby('ORDER_YEAR')['InvoiceTotal'].rank(method = 'first',
ascending = False
)
# Pivot the data to get the rankings in the correct format for a bump chart
pivot = yearly_totals.pivot(index = 'FullName', columns = 'ORDER_YEAR', values = 'Rank')
# Plot 3: Interactive Bump Chart using Plotly
fig3 = go.Figure()
for name in pivot.index:
fig3.add_trace(go.Scatter(
x = pivot.columns,
y = pivot.loc[name],
mode = 'lines+markers',
name = name,
hoverinfo = 'text',
text = [f'{name}<br>Year: {year}<br>Rank: {rank}' for year, rank in zip(pivot.columns, pivot.loc[name])]
))
# Update the layout
fig3.update_layout(
title = 'Rankings of Total Sales Per Year for Each Employee',
xaxis_title = 'Year',
yaxis_title = 'Rank',
yaxis = dict(autorange='reversed'), # Invert the y-axis to have rank 1 at the top
legend = dict(
title = 'Employee',
x = 1.05,
y = 1,
traceorder = 'normal'
),
margin = dict(l = 0, r = 150, t = 50, b = 50)
)
# Show plot
fig3.show()