1
votes

I am trying to use the hoverinfo attribute on create_choropleth. I want my hover info to include data from additional columns in my dataframe.

My approach as been as follows (i've already defined 'values' and 'count' see my complete code below) where this line would be included with all my fig arguments:

text=count, x=values, hoverinfo='text+x',

I have a dataset with a column named 'count' (i also want to include the column named 'District'). here is my complete code (with the hoverinfo line removed from fig (since it doesnt work)):

import plotly.plotly as py
from plotly.figure_factory._county_choropleth import create_choropleth
import numpy as np
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/468bd2205d85c7b0bfb4ebcd4bc4bf0ba408efb4/RRC_Spill_table/county_name%20%26%20fips%20%26%20net%20loss%20%26%20count%20(ordered%20by%20district%20%26%20grouped).csv')

colorscale = ["#f7fbff","#ebf3fb","#deebf7","#d2e3f3","#c6dbef","#b3d2e9","#9ecae1",
              "#85bcdb","#6baed6","#57a0ce","#4292c6","#3082be","#2171b5","#1361a9",
              "#08519c","#0b4083","#08306b"]

endpts = [415, 830, 1245, 1660, 2075, 2490, 2905, 3320, 3735, 4150, 4565, 4980, 5395, 5810, 6225]
fips = df['fips'].tolist()
values = df['Net spill volume (BBL)'].tolist()
count=df['number_of_oil_spills'].tolist()
x=values
fig = create_choropleth(
    fips=fips, values=values,
    binning_endpoints=endpts,
    colorscale=colorscale,
    show_state_data=False,
    show_hover=True, centroid_marker={'opacity': 0},
    scope=['TX'],
    state_outline={'color': 'rgb(15, 15, 55)', 'width': 3},
    asp=2.9, title='Oil Spills from 12/1/16 - 5/14/18',
    legend_title='Net spill Volume (BBL)'
)



fig['layout']['legend'].update({'x': 0})
fig['layout']['annotations'][0].update({'x': -0.12, 'xanchor': 'left'})
py.plot(fig, filename='oil spill net loss')

Additional background information:

plotly hoverinfo documentation: https://plot.ly/python/reference/#scatter-hoverinfo

help(ff.create_choropleth)

Help on function create_choropleth in module plotly.figure_factory._county_choropleth:

:param (dict) centroid_marker: dict of attributes of the centroid marker.
    The centroid markers are invisible by default and appear visible on
    selection. See https://plot.ly/python/reference/#scatter-marker for
    all valid params

bonus questions: what does opacity even do? why wont my line outline of the state show?

1

1 Answers

1
votes

I think adding this to your code just before the plot will solve your issue.

 df['text'] = [str(a)+" "+str(b) for a,b in zip(count,df['District'].tolist())]
 hover_trace = [t for t in fig['data'] if 'text' in t][0]
 for i, label in enumerate(hover_trace['text']):
    # Remove FIPS
    if(i<len(fips)):
        new_label = label.replace("FIPS: %s<br>" % fips[i], "")

        # Add a new value
        new_label +=  "<br>Other: %s" % df['text'][i]

        # Update trace text
        hover_trace['text'][i] = new_label

you can even do something like this:

 hover_trace = [t for t in fig['data'] if 'text' in t][0]
 for i, label in enumerate(hover_trace['text']):
    # Remove FIPS
    if(i<len(fips)):
        new_label = label.replace("FIPS: %s<br>" % fips[i], "")

        # Add a new value
        new_label +=  "<br>Other_1: %d" % count[i]

        # Add a new value
        new_label +=  "<br>Other_2: %s" %df['District'][i]

        # Update trace text
        hover_trace['text'][i] = new_label

for more info see this