作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 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
最佳答案
只需设置:
hover_data=['text']
关于python - plotly express choropleth map 自定义悬停数据错误 : ValueError: Value of 'hover_data_0' is not the name of a column in 'data_frame' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61711506/
我是一名优秀的程序员,十分优秀!