@@ -181,44 +181,32 @@ centroids["winner"] = np.where(centroids['votes_gop'] > centroids['votes_dem'],
181
181
182
182
centroids['lon'] = centroids['geometry'].x
183
183
centroids['lat'] = centroids['geometry'].y
184
- centroids = pd.DataFrame(centroids[["county_name",'lon','lat','winner', 'CENSUS_2010_POP']])
184
+ centroids = pd.DataFrame(centroids[["county_name",'lon','lat','winner', 'CENSUS_2010_POP',"state_name" ]])
185
185
groups = centroids.groupby('winner')
186
186
187
187
df = centroids.copy()
188
-
189
- df['text'] = df["county_name"] + '<br>Population ' + (df['CENSUS_2010_POP']/1e3).astype(str)+' thousands'
190
- limits = ["republican","democrats"]
191
- colors = ['#FF0000', '#0000FF']
192
188
```
193
189
194
190
``` {python, include = TRUE, echo=TRUE}
195
191
df['color'] = df['winner'].replace(color_dict)
196
192
df['size'] = df['CENSUS_2010_POP']/6000
193
+ df['text'] = df['CENSUS_2010_POP'].astype(int).apply(lambda x: '<br>Population: {:,} people'.format(x))
194
+ df['hover'] = df['county_name'].astype(str) + df['state_name'].apply(lambda x: ' ({}) '.format(x)) + df['text']
197
195
198
- df['color'] = df['winner'].replace(color_dict)
199
- df['size'] = df['CENSUS_2010_POP']/6000
200
- df['text'] = df["county_name"] + '<br>Population ' + (df['CENSUS_2010_POP']/1e3).astype(str)+' thousands'
201
-
202
- fig_plotly = pgo.Figure(
203
- data=pgo.Scattergeo(
204
- locationmode = 'USA-states',
205
- lon=df["lon"], lat=df["lat"],
206
- text = df["text"],
207
- mode = 'markers',
208
- marker_color = df["color"],
209
- marker_size = df['size']
210
- )
211
- )
196
+ fig_plotly = go.Figure(data=go.Scattergeo(
197
+ locationmode = 'USA-states',
198
+ lon=df["lon"], lat=df["lat"],
199
+ text = df["hover"],
200
+ mode = 'markers',
201
+ marker_color = df["color"],
202
+ marker_size = df['size'],
203
+ hoverinfo="text"
204
+ ))
212
205
```
213
206
214
207
``` {python, include = TRUE, echo=TRUE}
215
208
fig_plotly.update_traces(
216
- marker = dict(
217
- opacity=0.5,
218
- line_color='rgb(40,40,40)',
219
- line_width=0.5,
220
- sizemode = 'area'
221
- )
209
+ marker = {'opacity': 0.5, 'line_color': 'rgb(40,40,40)', 'line_width': 0.5, 'sizemode': 'area'}
222
210
)
223
211
224
212
fig_plotly.update_layout(
0 commit comments