4

I tried to create an interactive choropleth plot of COVID-19 Confirmed Cases by US State using plotly.express.choropleth, but I encountered an issue as I was trying to customize the hover_data. Here is the code:

import pandas as pd
import plotly.express as px

covtrack_states_hist_df = covtrack_states_hist_df.sort_values(by=['date'])

color_scale = ['#ffffff', '#ffe6e6', '#ffcccc', '#ff9999', '#ff6666', '#ff3333',
               '#ff0000', '#e60000', '#cc0000', '#b30000', '#990000', '#800000']

covtrack_states_hist_df = covtrack_states_hist_df.astype({'positive': str, 'death': str, 'test': str})
covtrack_states_hist_df['text'] = 'Confirmed: ' + covtrack_states_hist_df['positive'] + '<br>' + \ 
                                  'Deaths: ' + covtrack_states_hist_df['death'] + '<br>' + \
                                  'Tests: ' + covtrack_states_hist_df['test']
text = covtrack_states_hist_df['text'].tolist()

covtrack_states_hist_df[['positive', 'death', 'test']] = covtrack_states_hist_df[['positive', 'death', 'test']].apply(pd.to_numeric)

fig = px.choropleth(
    covtrack_states_hist_df,
    color='positive',
    locations='state',             
    locationmode = 'USA-states',
    scope='usa',
    hover_name='state',
    hover_data='text', # I've tried just text as well, but not working
    # I've also tried covtrack_states_hist_df['text'] and covtrack_states_hist_df.text but none of them worked
    animation_frame='date',
    title="Daily New COVID-19 Confirmed Cases",
    color_continuous_scale= color_scale, 
)

fig['layout'].pop('updatemenus')

fig.show()

The error I get is:

ValueError: Value of 'hover_data_0' is not the name of a column in 'data_frame'. 
Expected one of ['date', 'state', 'fips', 'positive', 'death', 'test', 'datetime', 'text'] but received: t

Here is a screenshot of what my DataFrame looks like: DataFrame info and head

1 Answer 1

6

just set:

hover_data=['text']

it is expecting a list

Sign up to request clarification or add additional context in comments.

1 Comment

But doesn't the hover_data now include the word "text" followed by the three lines we just created? As in, text = Confirmed: xxxxx Deaths: yyy, Tests: zzzzzzzz? Is there a way to obtain only Confirmed, Deaths and Tests?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.