Skip to content

choropleth improvements #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [2.5.0] - UNRELEASED
### Updated
- `plotly.figure_factory.create_choropleth` has changed some of the default plotting options:
- 'offline_mode' param has been removed from call signature
- persistent selection api for the centroid points is automatically enabled. See https://plot.ly/python/reference/#scatter-selected and https://plot.ly/python/reference/#scatter-unselected for details
- FIPS values that appear on hover are 0-padded to ensure they are 5 digits
- for the county lines data `hover_info='none'` by default

## [2.4.1] - 2018-02-21
### Fixed
- The required shapefiles to generate the choropleths via `plotly.figure_factory.create_choropleth` are now shipped in the package data.
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ machine:

node:
# use a pre-installed version of node so we don't need to download it.
version: 4.2.2
version: 6.0.0

dependencies:

Expand Down
56 changes: 25 additions & 31 deletions plotly/figure_factory/_county_choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def _intervals_as_labels(array_of_intervals, round_legend_values, exponent_forma
def _calculations(df, fips, values, index, f, simplify_county, level,
x_centroids, y_centroids, centroid_text, x_traces,
y_traces, fips_polygon_map):
# 0-pad FIPS code to ensure exactly 5 digits
padded_f = str(f).zfill(5)
if fips_polygon_map[f].type == 'Polygon':
x = fips_polygon_map[f].simplify(
simplify_county
Expand All @@ -311,10 +313,11 @@ def _calculations(df, fips, values, index, f, simplify_county, level,
x_c, y_c = fips_polygon_map[f].centroid.xy
county_name_str = str(df[df['FIPS'] == f]['COUNTY_NAME'].iloc[0])
state_name_str = str(df[df['FIPS'] == f]['STATE_NAME'].iloc[0])

t_c = (
'County: ' + county_name_str + '<br>' +
'State: ' + state_name_str + '<br>' +
'FIPS: ' + str(f) + '<br>Value: ' + str(values[index])
'FIPS: ' + padded_f + '<br>Value: ' + str(values[index])
)

x_centroids.append(x_c[0])
Expand All @@ -337,7 +340,7 @@ def _calculations(df, fips, values, index, f, simplify_county, level,
text = (
'County: ' + county_name_str + '<br>' +
'State: ' + state_name_str + '<br>' +
'FIPS: ' + str(f) + '<br>Value: ' + str(values[index])
'FIPS: ' + padded_f + '<br>Value: ' + str(values[index])
)
t_c = [text for poly in fips_polygon_map[f]]
x_centroids = x_c + x_centroids
Expand All @@ -352,12 +355,11 @@ def _calculations(df, fips, values, index, f, simplify_county, level,

def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
colorscale=None, order=None, simplify_county=0.02,
simplify_state=0.02, asp=None, offline_mode=False,
show_hover=True, show_state_data=True,
state_outline=None, county_outline=None,
centroid_marker=None, round_legend_values=False,
exponent_format=False, legend_title='',
**layout_options):
simplify_state=0.02, asp=None, show_hover=True,
show_state_data=True, state_outline=None,
county_outline=None, centroid_marker=None,
round_legend_values=False, exponent_format=False,
legend_title='', **layout_options):
"""
Returns figure for county choropleth. Uses data from package_data.

Expand Down Expand Up @@ -399,12 +401,6 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
Default = 0.02
:param (float) asp: the width-to-height aspect ratio for the camera.
Default = 2.5
:param (bool) offline_mode: if set to True, the centroids of each county
are invisible until selected over with a dragbox. Warning: this can
only be used if you are plotting in offline mode with validate set to
False as the params that are being added to the fig dictionary are not
yet part of the plotly.py python library. Stay tuned for updates.
Default = False
:param (bool) show_hover: show county hover and centroid info
:param (bool) show_state_data: reveals state boundary lines
:param (dict) state_outline: dict of attributes of the state outline
Expand All @@ -416,8 +412,9 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
https://plot.ly/python/reference/#scatter-marker-line for all valid
params
:param (dict) centroid_marker: dict of attributes of the centroid marker.
See https://plot.ly/python/reference/#scatter-marker for all valid
params
The centroid markers are invisible by default and appear visible on
selection. See https://plot.ly/python/reference/#scatter-marker for
all valid params
:param (bool) round_legend_values: automatically round the numbers that
appear in the legend to the nearest integer.
Default = False
Expand Down Expand Up @@ -587,9 +584,11 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
county_outline = {'color': 'rgb(0, 0, 0)',
'width': 0}
if not centroid_marker:
centroid_marker = {'size': 2,
'color': 'rgb(255, 255, 255)',
'opacity': 0}
centroid_marker = {'size': 3, 'color': 'white', 'opacity': 1}

# ensure centroid markers appear on selection
if 'opacity' not in centroid_marker:
centroid_marker.update({'opacity': 1})

if len(fips) != len(values):
raise exceptions.PlotlyError(
Expand Down Expand Up @@ -788,7 +787,7 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
fill='toself',
fillcolor=color_lookup[lev],
name=lev,
hoverinfo='text',
hoverinfo='none',
)
plot_data.append(county_data)

Expand All @@ -802,19 +801,14 @@ def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
text=centroid_text,
name='US Counties',
mode='markers',
marker=centroid_marker,
marker={'color': 'white', 'opacity': 0},
hoverinfo='text'
)
if offline_mode:
centroids_on_select = dict(
selected=dict(
marker=dict(size=2, color='white', opacity=1)
),
unselected=dict(
marker=dict(opacity=0)
)
)
hover_points.update(centroids_on_select)
centroids_on_select = dict(
selected=dict(marker=centroid_marker),
unselected=dict(marker=dict(opacity=0))
)
hover_points.update(centroids_on_select)
plot_data.append(hover_points)

if show_state_data:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion plotly/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.4.1'
__version__ = '2.5.0'