Repository navigation
Expand file tree
/
Copy pathAutoStatsDash.py
More file actions
169 lines (148 loc) · 7.21 KB
/
Copy pathAutoStatsDash.py
File metadata and controls
169 lines (148 loc) · 7.21 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
# Load the data using pandas
data = pd.read_csv('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/historical_automobile_sales.csv')
# Initialize the Dash app
app = dash.Dash(__name__)
# Set the title of the dashboard
app.title = "Automobile Statistics Dashboard"
#---------------------------------------------------------------------------------
#The dropdown menu options
dropdown_options = [
{'label': 'Yearly Statistics', 'value': 'Yearly Statistics'},
{'label': 'Recession Period Statistics', 'value': 'Recession Statistics'}
]
# List of years
year_list = [i for i in range(1980, 2024, 1)]
#---------------------------------------------------------------------------------------
# Create the layout of the app
app.layout = html.Div([
html.H1(app.title,
style={'textAlign': 'center', 'color': '#503D36', 'fontSize': 24}
),
html.Div([
html.Label("Select Statistics:"),
dcc.Dropdown(
id='dropdown-statistics',
options=dropdown_options,
placeholder='Select a report type',
value='Select Statistics',
style={'width': '80%', 'padding': '3px', 'fontSize': '20px', 'textAlignLast': 'center'}
)
]),
html.Div([
dcc.Dropdown(
id='select-year',
options=[{'label': i, 'value': i} for i in year_list],
placeholder='Select a year',
style={'width': '80%', 'padding': '3px', 'fontSize': '20px', 'textAlignLast': 'center'}
)
]),
html.Div([
html.Div(id='output-container', className='chart-grid', style={'display': 'flex'}),
])
])
#Creating Callbacks
#Callback function to update the input container based on the selected statistics
@app.callback(
Output(component_id='select-year', component_property='disabled'),
Input(component_id='dropdown-statistics', component_property='value'))
def update_input_container(selected_statistics):
if selected_statistics =='Yearly Statistics':
return False
else:
return True
#Callback for plotting
#Callback function to update the input container based on the selected statistics
@app.callback(
Output(component_id='output-container', component_property='children'),
[Input(component_id='select-year', component_property='value'),
Input(component_id='dropdown-statistics', component_property='value')])
def update_output_container(input_year, selected_statistics):
if selected_statistics == 'Recession Statistics':
# Filter the data for recession periods
recession_data = data[data['Recession'] == 1]
#Create and display graphs for Recession Report Statistics
#Plot 1 Automobile sales fluctuate over Recession Period (year wise) using line chart
yearly_rec = recession_data.groupby('Year')['Automobile_Sales'].mean().reset_index()
# Plotting the line graph
R_chart1 = dcc.Graph(
figure=px.line(yearly_rec,
x='Year',
y='Automobile_Sales',
title="Yearly Average Automobile Sales During Recession"))
# Plot 2 The average number of vehicles sold by vehicle type and represent as a Bar chart
average_sales = recession_data.groupby('Vehicle_Type')['Automobile_Sales'].mean().reset_index()
R_chart2 = dcc.Graph(
figure=px.bar(average_sales,
x='Vehicle_Type',
y='Automobile_Sales',
title="Average Vehicle Type Sales During Recession"))
# Plot 3 Pie chart for total expenditure share by vehicle type during recessions
exp_rec = recession_data.groupby('Vehicle_Type')['Advertising_Expenditure'].sum().reset_index()
R_chart3 = dcc.Graph(
figure=px.pie(exp_rec,
values='Advertising_Expenditure',
names='Vehicle_Type',
title="Advertisement Expenditure Share by Vehicle Type During Recession"
)
)
# Plot 4 Bar chart for the effect of unemployment rate on vehicle type and sales
unemployment_effect = recession_data.groupby('Vehicle_Type')[['unemployment_rate', 'Automobile_Sales']].mean().reset_index()
R_chart4 = dcc.Graph(
figure=px.bar(unemployment_effect,
x='Vehicle_Type',
y='Automobile_Sales',
color_discrete_sequence=['#50C878'],
title="Effect of Unemployment Rate on Vehicle Type and Sales During Recession"))
return [
html.Div(className='chart-item', children=[R_chart1, R_chart2]),
html.Div(className='chart-item', children=[R_chart3, R_chart4])
]
# TASK 2.6: Create and display graphs for Yearly Report Statistics
# Yearly Statistic Report Plots
elif (input_year and selected_statistics=='Yearly Statistics') :
yearly_data = data[data['Year'] == int(input_year)]
#TASK 2.5: Creating Graphs Yearly data
#plot 1 Yearly Automobile sales using line chart for the whole period.
yas = data.groupby('Year')['Automobile_Sales'].mean().reset_index()
Y_chart1 = dcc.Graph(figure=px.line(yas, x="Year", y="Automobile_Sales",
title="Yearly Average Automobile Sales"))
# Plot 2 Total Monthly Automobile sales using line chart.
monthly_sales = yearly_data.groupby('Month')['Automobile_Sales'].sum().reset_index()
Y_chart2 = dcc.Graph(
figure=px.line(monthly_sales,
x='Month',
y='Automobile_Sales',
title='Total Monthly Automobile Sales'))
# Plot bar chart for average number of vehicles sold during the given year
avr_vdata = yearly_data.groupby('Vehicle_Type')['Automobile_Sales'].mean().reset_index()
Y_chart3 = dcc.Graph(
figure=px.bar(avr_vdata,
x='Vehicle_Type',
y='Automobile_Sales',
title='Average Monthly Automobile Sales by Vehicle Type'))
# Total Advertisement Expenditure for each vehicle using pie chart
exp_data = yearly_data.groupby('Vehicle_Type')['Advertising_Expenditure'].sum().reset_index()
Y_chart4 = dcc.Graph(
figure=px.pie(exp_data,
values='Advertising_Expenditure',
names='Vehicle_Type',
title='Total Advertisement Expenditure for Each Vehicle'))
# Returning the graphs for displaying Yearly data
return [
html.Div(className='chart-item', children=[Y_chart1, Y_chart2]),
html.Div(className='chart-item', children=[Y_chart3, Y_chart4])
]
else:
return None
# Run Dash app
if __name__ == '__main__':
app.run_server(debug=True)