【问题标题】:plotly express choropleth map customizing hover data error: ValueError: Value of 'hover_data_0' is not the name of a column in 'data_frame'plotly express choropleth map customizing hover data error: ValueError: Value of 'hover_data_0' is not the name of a column in 'data_frame'
【发布时间】:2020-08-25 22:04:48
【问题描述】:

我尝试使用 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

【问题讨论】:

    标签: python hover plotly choropleth


    【解决方案1】:

    刚刚设置:

    hover_data=['text']
    

    它需要一个列表

    【讨论】:

    • 但是 hover_data 现在不是包含单词“text”和我们刚刚创建的三行吗?如,文本 = 确认:xxxxx 死亡:yyy,测试:zzzzzzzz?有没有办法获得only Confirmed, Deaths and Tests?
    猜你喜欢
    • 2023-02-09
    • 2021-12-28
    • 2022-12-02
    • 2022-12-02
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多