ritaycw commited on
Commit
eaa6f81
·
verified ·
1 Parent(s): 411fe56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -173
app.py CHANGED
@@ -1,175 +1,3 @@
1
- # start with the setup
2
-
3
- # supress warnings about future deprecations
4
- import warnings
5
- warnings.simplefilter(action='ignore', category=FutureWarning)
6
-
7
- import pandas as pd
8
- import altair as alt
9
- import numpy as np
10
- import pprint
11
- import datetime as dt
12
- from vega_datasets import data
13
- import matplotlib.pyplot as plt
14
-
15
- # Solve a javascript error by explicitly setting the renderer
16
- alt.renderers.enable('jupyterlab')
17
-
18
- #load data
19
- df1=pd.read_csv("https://raw.githubusercontent.com/dallascard/SI649_public/main/altair_hw3/approval_polllist.csv")
20
- df2=pd.read_csv("https://raw.githubusercontent.com/dallascard/SI649_public/main/altair_hw3/approval_topline.csv")
21
-
22
- #change the approval ratings into percentage
23
- df1['approve_percent']=df1['approve']/100
24
- df1.head()
25
-
26
- df2['timestamp']=pd.to_datetime(df2['timestamp'])
27
- df2=pd.melt(df2, id_vars=['president', 'subgroup', 'timestamp'], value_vars=['approve','disapprove']).rename(columns={'variable':'choice', 'value':'rate'})
28
- df2.head()
29
-
30
-
31
- ##TODO: replicate vis 1
32
-
33
- ##Static Component - Bars
34
- barchart1_1 = alt.Chart(df1).transform_joinaggregate(
35
- groupby=['pollster']
36
- ).mark_bar(height=15).encode(
37
- alt.X('mean(approve_percent):Q', axis=alt.Axis(labels=True, title=None)),
38
- alt.Y('pollster:N', axis=alt.Axis(labels=True, title=None)),
39
- alt.Tooltip('mean(approve_percent):Q', format='.0%')
40
- ).properties(
41
- title='Average Approval Ratings for Joe Biden'
42
- )
43
-
44
- ##Static Component - Vertical Line
45
- vline1_1 = alt.Chart(df1).mark_rule(size=3, color="firebrick").encode(
46
- alt.X('mean(approve_percent):Q')
47
- )
48
-
49
- ##Static Component - Text
50
- text1_1 = vline1_1.mark_text(
51
- color='firebrick',
52
- fontSize=12,
53
- align='left',
54
- dx=7
55
- ).encode(
56
- alt.Text('mean(approve_percent):Q', format='.2%'),
57
- )
58
-
59
- ##Put all together
60
- selection1 = alt.selection_interval(encodings=["y"])
61
- condition1 = alt.condition(selection1, alt.value(1.0), alt.value(0.6))
62
-
63
- barchart1_2 = barchart1_1.add_params(selection1).encode(
64
- opacity = condition1
65
- )
66
-
67
- vline1_2 = vline1_1.add_params(selection1).transform_filter(selection1)
68
-
69
- text1_2 = text1_1.transform_filter(selection1)
70
-
71
- final_viz1 = barchart1_2 + vline1_2 + text1_2
72
- final_viz1
73
-
74
-
75
-
76
- #TODO: replicate vis2
77
-
78
- # Create selection and condition
79
- selection2 = alt.selection_interval(encodings=["x"])
80
- condition2 = alt.condition(selection2, alt.value(1.0), alt.value(0.6))
81
-
82
- # scatter plot
83
- scatter2_1 = alt.Chart(df1).mark_point().transform_joinaggregate(
84
- groupby=['pollster']
85
- ).encode(
86
- alt.X('startdate:T', axis=alt.Axis(labels=True, title=None)),
87
- alt.Y('mean(adjusted_approve):Q', axis=alt.Axis(title='Approval ratings')),
88
- color='pollster:N',
89
- )
90
-
91
- # bar chart
92
- bar2_1 = alt.Chart(df1).mark_bar().transform_joinaggregate(
93
- groupby=['pollster:N']
94
- ).encode(
95
- alt.X('mean(adjusted_approve):Q', axis=alt.Axis(title = 'Mean of Approval Ratings')),
96
- alt.Y('pollster:N', axis=alt.Axis(labels=True, title=None)),
97
- color='pollster:N'
98
- )
99
-
100
- # Put them all together
101
- # scatter2_1 & bar2_1
102
-
103
- scatter2_2 = scatter2_1.add_params(selection2).encode(
104
- opacity = condition2
105
- )
106
-
107
- bar2_2 = bar2_1.add_params(selection2).transform_filter(selection2)
108
-
109
- final_viz2 = (scatter2_2 & bar2_2).properties(
110
- title='Recently Reported Approval Ratings for Joe Biden'
111
- )
112
-
113
- final_viz2
114
-
115
-
116
- #TODO: replicate vis3
117
- # https://altair-viz.github.io/gallery/multiline_tooltip.html
118
-
119
- # Create a selection for zooming and panning across the x-axis
120
- scale = alt.selection_interval(bind='scales', encodings=['x'])
121
-
122
- # Create a selection and condition for the vertical line, annotation dots, and text annotations
123
- nearest = alt.selection_point(on='mouseover', encodings=['x'], nearest=True, empty=False)
124
- opacityCondition = alt.condition(nearest, alt.value(1), alt.value(0))
125
-
126
- # Create the base chart and filter to All polls
127
- base3 = alt.Chart(df2).mark_line(size=2.5).transform_filter(
128
- alt.datum.subgroup =='All polls'
129
- ).encode(
130
- alt.X('timestamp:T', axis=alt.Axis(labels=True, title=None)),
131
- y='rate:Q',
132
- color='choice:N'
133
- ).add_params(scale).properties(
134
- title='Approval Ratings for Joe Biden 2021-2023'
135
- )
136
-
137
- # Static line chart
138
- # Vertical line
139
- selectors = alt.Chart(df2).mark_point().encode(
140
- x='timestamp:T',
141
- opacity=alt.value(0),
142
- ).add_params(
143
- nearest
144
- )
145
-
146
- rules = alt.Chart(df2).mark_rule(size=4, color='lightgray').encode(
147
- x='timestamp:T'
148
- ).transform_filter(
149
- nearest
150
- )
151
-
152
- #interaction dots
153
- points = base3.mark_point(size=90).encode(
154
- opacity= opacityCondition # alt.condition(nearest, alt.value(1), alt.value(0))
155
- )
156
-
157
- #interaction text labels
158
- text = base3.mark_text(fontSize=14, align='left', dx=7).transform_filter(
159
- alt.datum.subgroup =='All polls'
160
- ).encode(
161
- text=alt.condition(nearest, 'rate:Q', alt.value(' '), format='.2f')
162
- )
163
-
164
- #Put them all together
165
- alt.layer(
166
- base3, selectors, points, rules, text
167
- ).properties(
168
- width=400, height=300
169
- )
170
-
171
-
172
-
173
  ## Viz 4
174
  # Import panel and vega datasets
175
 
@@ -177,7 +5,7 @@ import panel as pn
177
  import vega_datasets
178
 
179
  # Enable Panel extensions
180
- pn.extension()
181
 
182
  # Define a function to create and return a plot
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ## Viz 4
2
  # Import panel and vega datasets
3
 
 
5
  import vega_datasets
6
 
7
  # Enable Panel extensions
8
+ pn.extension(design='bootstrap')
9
 
10
  # Define a function to create and return a plot
11