question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create country choropleth hover regression

See original GitHub issue

Originally reported at https://community.plot.ly/t/county-choropleths-not-displaying-hoverinfo-in-offline-mode/19241

The hover tooltips for the figure produced by create_choropleth are sometimes not being displayed properly with recent versions of plotly.js.

import plotly as py
import plotly.figure_factory as ff

import numpy as np
import pandas as pd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')
df_sample_r = df_sample[df_sample['STNAME'] == 'California']

values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()

colorscale = [
    'rgb(193, 193, 193)',
    'rgb(239,239,239)',
    'rgb(195, 196, 222)',
    'rgb(144,148,194)',
    'rgb(101,104,168)',
    'rgb(65, 53, 132)'
]

fig = ff.create_choropleth(
    fips=fips, values=values, scope=['CA', 'AZ', 'Nevada', 'Oregon', ' Idaho'],
    binning_endpoints=[14348, 63983, 134827, 426762, 2081313], colorscale=colorscale,
    county_outline={'color': 'rgb(255,255,255)', 'width': 0.5}, round_legend_values=True,
    legend_title='Population by County', title='California and Nearby States'
)

Plotting this figure in the most recent version of plotly.js (1.44.3) results in no hover tooltips

py.offline.plot(fig,
                filename='choropleth_california_and_surr_states_outlines',
                include_plotlyjs='https://cdn.plot.ly/plotly-1.44.3.min.js')

But using 1.42.3 the tooltips are dipslayed as expected

py.offline.plot(fig,
                filename='choropleth_california_and_surr_states_outlines',
                include_plotlyjs='https://cdn.plot.ly/plotly-1.42.3.min.js')

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
p3zocommented, Jun 30, 2019

It looks like the bug creates duplicate hover tooltips and stores their x, y coordinates as lists instead of floats.

e.g.

[-86.1224], [-88.2717], [-94.2067], [-84.6049]

instead of

-86.1224, -88.2717, -94.2067, -84.6049

De-duplicating and flattening restores functionality. Building on the code snippet from @jonmmease’s original post…

hover_ix, hover = [(ix, t) for ix, t in enumerate(fig['data']) if t.text][0]

# mismatching lengths indicates bug
if len(hover['text']) != len(df_sample_r):

    ht = pd.Series(hover['text'])

    no_dupe_ix = ht.index[~ht.duplicated()]

    hover_x_deduped = np.array(hover['x'])[no_dupe_ix]
    hover_y_deduped = np.array(hover['y'])[no_dupe_ix]

    new_hover_x = [x if type(x) == float else x[0] for x in hover_x_deduped]
    new_hover_y = [y if type(y) == float else y[0] for y in hover_y_deduped]

    fig['data'][hover_ix]['text'] = ht.drop_duplicates()
    fig['data'][hover_ix]['x'] = new_hover_x
    fig['data'][hover_ix]['y'] = new_hover_y
5reactions
harish2rbcommented, Apr 25, 2019

Any update on when this bug might be fixed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to step up your Folium Choropleth Map skills
To create a choropleth, we need two things: First, we need a geoJSON file that gives us the geographical coordinates for the layers....
Read more >
Creating a Choropleth Map of State Unemployment Rates ...
This data can be sourced to create a choropleth map that highlights the differences in labor market conditions across the country using ...
Read more >
Choropleth Maps Using Python - DevSkrol
To make a Choropleth Map, we need two main types of inputs. ... GeoJSON-formatted geometry information can be obtained either using FIPS IDs...
Read more >
Beautiful Interactive Choropleth & Scatter Maps with Plotly
R Maps: Beautiful Interactive Choropleth & Scatter Maps with PlotlyTimeline0:00 Intro0:28 Reading in data ( Choropleth )1:44 Building base ...
Read more >
Free Choropleth Map Maker - Displayr
The simplest way to create and customize choropleth maps ... Connect or paste your list of geographic entities (countries, continents, states, regions, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found