@@ -300,6 +300,8 @@ def _intervals_as_labels(array_of_intervals, round_legend_values, exponent_forma
300
300
def _calculations (df , fips , values , index , f , simplify_county , level ,
301
301
x_centroids , y_centroids , centroid_text , x_traces ,
302
302
y_traces , fips_polygon_map ):
303
+ # 0-pad FIPS code to ensure exactly 5 digits
304
+ padded_f = str (f ).zfill (5 )
303
305
if fips_polygon_map [f ].type == 'Polygon' :
304
306
x = fips_polygon_map [f ].simplify (
305
307
simplify_county
@@ -311,10 +313,11 @@ def _calculations(df, fips, values, index, f, simplify_county, level,
311
313
x_c , y_c = fips_polygon_map [f ].centroid .xy
312
314
county_name_str = str (df [df ['FIPS' ] == f ]['COUNTY_NAME' ].iloc [0 ])
313
315
state_name_str = str (df [df ['FIPS' ] == f ]['STATE_NAME' ].iloc [0 ])
316
+
314
317
t_c = (
315
318
'County: ' + county_name_str + '<br>' +
316
319
'State: ' + state_name_str + '<br>' +
317
- 'FIPS: ' + str ( f ) + '<br>Value: ' + str (values [index ])
320
+ 'FIPS: ' + padded_f + '<br>Value: ' + str (values [index ])
318
321
)
319
322
320
323
x_centroids .append (x_c [0 ])
@@ -337,7 +340,7 @@ def _calculations(df, fips, values, index, f, simplify_county, level,
337
340
text = (
338
341
'County: ' + county_name_str + '<br>' +
339
342
'State: ' + state_name_str + '<br>' +
340
- 'FIPS: ' + str ( f ) + '<br>Value: ' + str (values [index ])
343
+ 'FIPS: ' + padded_f + '<br>Value: ' + str (values [index ])
341
344
)
342
345
t_c = [text for poly in fips_polygon_map [f ]]
343
346
x_centroids = x_c + x_centroids
@@ -352,12 +355,11 @@ def _calculations(df, fips, values, index, f, simplify_county, level,
352
355
353
356
def create_choropleth (fips , values , scope = ['usa' ], binning_endpoints = None ,
354
357
colorscale = None , order = None , simplify_county = 0.02 ,
355
- simplify_state = 0.02 , asp = None , offline_mode = False ,
356
- show_hover = True , show_state_data = True ,
357
- state_outline = None , county_outline = None ,
358
- centroid_marker = None , round_legend_values = False ,
359
- exponent_format = False , legend_title = '' ,
360
- ** layout_options ):
358
+ simplify_state = 0.02 , asp = None , show_hover = True ,
359
+ show_state_data = True , state_outline = None ,
360
+ county_outline = None , centroid_marker = None ,
361
+ round_legend_values = False , exponent_format = False ,
362
+ legend_title = '' , ** layout_options ):
361
363
"""
362
364
Returns figure for county choropleth. Uses data from package_data.
363
365
@@ -399,12 +401,6 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
399
401
Default = 0.02
400
402
:param (float) asp: the width-to-height aspect ratio for the camera.
401
403
Default = 2.5
402
- :param (bool) offline_mode: if set to True, the centroids of each county
403
- are invisible until selected over with a dragbox. Warning: this can
404
- only be used if you are plotting in offline mode with validate set to
405
- False as the params that are being added to the fig dictionary are not
406
- yet part of the plotly.py python library. Stay tuned for updates.
407
- Default = False
408
404
:param (bool) show_hover: show county hover and centroid info
409
405
:param (bool) show_state_data: reveals state boundary lines
410
406
:param (dict) state_outline: dict of attributes of the state outline
@@ -416,8 +412,9 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
416
412
https://plot.ly/python/reference/#scatter-marker-line for all valid
417
413
params
418
414
:param (dict) centroid_marker: dict of attributes of the centroid marker.
419
- See https://plot.ly/python/reference/#scatter-marker for all valid
420
- params
415
+ The centroid markers are invisible by default and appear visible on
416
+ selection. See https://plot.ly/python/reference/#scatter-marker for
417
+ all valid params
421
418
:param (bool) round_legend_values: automatically round the numbers that
422
419
appear in the legend to the nearest integer.
423
420
Default = False
@@ -587,9 +584,11 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
587
584
county_outline = {'color' : 'rgb(0, 0, 0)' ,
588
585
'width' : 0 }
589
586
if not centroid_marker :
590
- centroid_marker = {'size' : 2 ,
591
- 'color' : 'rgb(255, 255, 255)' ,
592
- 'opacity' : 0 }
587
+ centroid_marker = {'size' : 3 , 'color' : 'white' , 'opacity' : 1 }
588
+
589
+ # ensure centroid markers appear on selection
590
+ if 'opacity' not in centroid_marker :
591
+ centroid_marker .update ({'opacity' : 1 })
593
592
594
593
if len (fips ) != len (values ):
595
594
raise exceptions .PlotlyError (
@@ -788,7 +787,7 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
788
787
fill = 'toself' ,
789
788
fillcolor = color_lookup [lev ],
790
789
name = lev ,
791
- hoverinfo = 'text ' ,
790
+ hoverinfo = 'none ' ,
792
791
)
793
792
plot_data .append (county_data )
794
793
@@ -802,19 +801,14 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
802
801
text = centroid_text ,
803
802
name = 'US Counties' ,
804
803
mode = 'markers' ,
805
- marker = centroid_marker ,
804
+ marker = { 'color' : 'white' , 'opacity' : 0 } ,
806
805
hoverinfo = 'text'
807
806
)
808
- if offline_mode :
809
- centroids_on_select = dict (
810
- selected = dict (
811
- marker = dict (size = 2 , color = 'white' , opacity = 1 )
812
- ),
813
- unselected = dict (
814
- marker = dict (opacity = 0 )
815
- )
816
- )
817
- hover_points .update (centroids_on_select )
807
+ centroids_on_select = dict (
808
+ selected = dict (marker = centroid_marker ),
809
+ unselected = dict (marker = dict (opacity = 0 ))
810
+ )
811
+ hover_points .update (centroids_on_select )
818
812
plot_data .append (hover_points )
819
813
820
814
if show_state_data :
0 commit comments