Plotly Choropleth County Maps - If Hover Information Not Showing, try this solution

Hi, all.

I’ve been working with Plotly at my work recently, and I ran into some trouble with the county map of Ohio. In particular, when I hover over a county centroid, the hover text does not show up. I found a solution that helps.

First, I noticed that when I called fig[‘data’], the Scatter object with the value ‘US Counties’ for the key ‘name’ had x- and y-coordinates formatted as lists. (These coordinates were associated with counties that had duplicate entries for the ‘text’ key of the same Scatter object.) I wasn’t sure why that was, so I took a shot in the dark and decided to make them floats like the rest of the coordinates. After that, every county had hover information. Here is the code:

Import libraries

import pandas as pd
import plotly.figure_factory as ff
import plotly.offline as pyo

Load data set and engineer features

df = pd.read_csv(‘ohio_fips.csv’)
df[‘FIPS’] = df[‘FIPS’].map(lambda x: x.replace(’ FIPS Code’, ‘’).strip())

fips = df[‘FIPS’].tolist()
values = list(range(len(fips)))

Create figure

fig = ff.create_choropleth(fips=fips,values=values,scope=[‘OH’])

Plot figure. If I do not run anything below line 19, the figure

generates, but the hover information does not show.

pyo.plot(fig)

Re-assign the x- and y-coorinates of the counties

Running everything below this comment will show the hover information

for the counties.

fig_counties_info = [t for t in fig[‘data’] if t.text][0]
x_pts = list(fig_counties_info[‘x’])
y_pts = list(fig_counties_info[‘y’])

new_x_pts = []
for coordinate in x_pts:
if isinstance(coordinate,list):
new_coordinate = float(coordinate[0])
else:
new_coordinate = coordinate
new_x_pts.append(new_coordinate)
new_x_pts = tuple(new_x_pts)

new_y_pts = []
for coordinate in y_pts:
if isinstance(coordinate,list):
new_coordinate = float(coordinate[0])
else:
new_coordinate = coordinate
new_y_pts.append(new_coordinate)
new_y_pts = tuple(new_y_pts)

for t in fig[‘data’]:
if t.text:
t[‘x’] = new_x_pts
t[‘y’] = new_y_pts

pyo.plot(fig)

First, does anyone know why for some states that their county map contains duplicate entries for the ‘text’ key? Also, is there an easier way to work around this if I encounter another state with duplicate county entries and thus, x- and y-coordinates that are formatted as lists? Thank you!

Here is the data:

County,FIPS,State
Adams County, 39001 FIPS Code, Ohio
Allen County, 39003 FIPS Code, Ohio
Ashland County, 39005 FIPS Code, Ohio
Ashtabula County, 39007 FIPS Code, Ohio
Athens County, 39009 FIPS Code, Ohio
Auglaize County, 39011 FIPS Code, Ohio
Belmont County, 39013 FIPS Code, Ohio
Brown County, 39015 FIPS Code, Ohio
Butler County, 39017 FIPS Code, Ohio
Carroll County, 39019 FIPS Code, Ohio
Champaign County, 39021 FIPS Code, Ohio
Clark County, 39023 FIPS Code, Ohio
Clermont County, 39025 FIPS Code, Ohio
Clinton County, 39027 FIPS Code, Ohio
Columbiana County, 39029 FIPS Code, Ohio
Coshocton County, 39031 FIPS Code, Ohio
Crawford County, 39033 FIPS Code, Ohio
Cuyahoga County, 39035 FIPS Code, Ohio
Darke County, 39037 FIPS Code, Ohio
Defiance County, 39039 FIPS Code, Ohio
Delaware County, 39041 FIPS Code, Ohio
Erie County, 39043 FIPS Code, Ohio
Fairfield County, 39045 FIPS Code, Ohio
Fayette County, 39047 FIPS Code, Ohio
Franklin County, 39049 FIPS Code, Ohio
Fulton County, 39051 FIPS Code, Ohio
Gallia County, 39053 FIPS Code, Ohio
Geauga County, 39055 FIPS Code, Ohio
Greene County, 39057 FIPS Code, Ohio
Guernsey County, 39059 FIPS Code, Ohio
Hamilton County, 39061 FIPS Code, Ohio
Hancock County, 39063 FIPS Code, Ohio
Hardin County, 39065 FIPS Code, Ohio
Harrison County, 39067 FIPS Code, Ohio
Henry County, 39069 FIPS Code, Ohio
Highland County, 39071 FIPS Code, Ohio
Hocking County, 39073 FIPS Code, Ohio
Holmes County, 39075 FIPS Code, Ohio
Huron County, 39077 FIPS Code, Ohio
Jackson County, 39079 FIPS Code, Ohio
Jefferson County, 39081 FIPS Code, Ohio
Knox County, 39083 FIPS Code, Ohio
Lake County, 39085 FIPS Code, Ohio
Lawrence County, 39087 FIPS Code, Ohio
Licking County, 39089 FIPS Code, Ohio
Logan County, 39091 FIPS Code, Ohio
Lorain County, 39093 FIPS Code, Ohio
Lucas County, 39095 FIPS Code, Ohio
Madison County, 39097 FIPS Code, Ohio
Mahoning County, 39099 FIPS Code, Ohio
Marion County, 39101 FIPS Code, Ohio
Medina County, 39103 FIPS Code, Ohio
Meigs County, 39105 FIPS Code, Ohio
Mercer County, 39107 FIPS Code, Ohio
Miami County, 39109 FIPS Code, Ohio
Monroe County, 39111 FIPS Code, Ohio
Montgomery County, 39113 FIPS Code, Ohio
Morgan County, 39115 FIPS Code, Ohio
Morrow County, 39117 FIPS Code, Ohio
Muskingum County, 39119 FIPS Code, Ohio
Noble County, 39121 FIPS Code, Ohio
Ottawa County, 39123 FIPS Code, Ohio
Paulding County, 39125 FIPS Code, Ohio
Perry County, 39127 FIPS Code, Ohio
Pickaway County, 39129 FIPS Code, Ohio
Pike County, 39131 FIPS Code, Ohio
Portage County, 39133 FIPS Code, Ohio
Preble County, 39135 FIPS Code, Ohio
Putnam County, 39137 FIPS Code, Ohio
Richland County, 39139 FIPS Code, Ohio
Ross County, 39141 FIPS Code, Ohio
Sandusky County, 39143 FIPS Code, Ohio
Scioto County, 39145 FIPS Code, Ohio
Seneca County, 39147 FIPS Code, Ohio
Shelby County, 39149 FIPS Code, Ohio
Stark County, 39151 FIPS Code, Ohio
Summit County, 39153 FIPS Code, Ohio
Trumbull County, 39155 FIPS Code, Ohio
Tuscarawas County, 39157 FIPS Code, Ohio
Union County, 39159 FIPS Code, Ohio
Van Wert County, 39161 FIPS Code, Ohio
Vinton County, 39163 FIPS Code, Ohio
Warren County, 39165 FIPS Code, Ohio
Washington County, 39167 FIPS Code, Ohio
Wayne County, 39169 FIPS Code, Ohio
Williams County, 39171 FIPS Code, Ohio
Wood County, 39173 FIPS Code, Ohio
Wyandot County, 39175 FIPS Code, Ohio

Thanks for writing in! Looks like your question will be best answered in #api:python

Thank you, etienne! Do you know how I can move this post over to that without having to create a new post?

Do you know why all the maps except the first one in this website do not have hover text? https://plot.ly/python/county-choropleth/
I still can’t get have hover text shown in my map.
Thanks! I feel it should be so simple, but the hover text is just not there.