Skip to content

Commit 3704213

Browse files
committed
essaye avec un dict classique
1 parent 85565d5 commit 3704213

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

content/course/modelisation/0_preprocessing.Rmd

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,44 +181,32 @@ centroids["winner"] = np.where(centroids['votes_gop'] > centroids['votes_dem'],
181181
182182
centroids['lon'] = centroids['geometry'].x
183183
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"]])
185185
groups = centroids.groupby('winner')
186186
187187
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']
192188
```
193189

194190
```{python, include = TRUE, echo=TRUE}
195191
df['color'] = df['winner'].replace(color_dict)
196192
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']
197195
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+
))
212205
```
213206

214207
```{python, include = TRUE, echo=TRUE}
215208
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'}
222210
)
223211
224212
fig_plotly.update_layout(

0 commit comments

Comments
 (0)