自定义悬停数据的正则表达式圆角图错误:ValueError:'hover_data_0'的值不是'data_frame'中列的名称]]

问题描述 投票:0回答:1

我尝试使用plotly.express.choropleth创建一个由美国各州确定的COVID-19确诊病例的交互式总色图,但是在尝试自定义hover_data时遇到了一个问题。这是代码:

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()

我得到的错误是:

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

以下是我的DataFrame的屏幕截图:DataFrame info and head

我尝试使用plotly.express.choropleth创建一个由美国各州确定的COVID-19确诊病例的交互式总色图,但是在尝试自定义hover_data时遇到了一个问题。在这里...

python hover plotly choropleth
1个回答
0
投票

刚刚设置:

© www.soinside.com 2019 - 2024. All rights reserved.