0

我一直在为仪表板进行 COVID19 分析,并且正在使用 JSON 数据源。我已将 json 转换为数据框。我正在绘制“状态”x 轴(分类值)上“死亡天数”的条形图。我正在尝试使用一个函数来更新slider.value。在使用 --log-level=DEBUG 运行散景服务时,我收到以下错误:

在此处输入图像描述

有人可以为我提供任何可能导致问题的方向或帮助,因为我是 Python 新手并且感谢任何帮助?或者,如果有任何其他选择。

请在下面找到代码:

cases_summary = requests.get('https://api.rootnet.in/covid19-in/stats/history')

json_data = cases_summary.json()

#Data Cleaning
cases_summary=pd.json_normalize(json_data['data'], record_path='regional', meta='day')

cases_summary['loc']=np.where(cases_summary['loc']=='Nagaland#', 'Nagaland', cases_summary['loc'])
cases_summary['loc']=np.where(cases_summary['loc']=='Madhya Pradesh#', 'Madhya Pradesh', cases_summary['loc'])
cases_summary['loc']=np.where(cases_summary['loc']=='Jharkhand#', 'Jharkhand', cases_summary['loc'])

#Calculate cumulative days since 1st case for each state
cases_summary['day_count']=(cases_summary['day'].groupby(cases_summary['loc']).cumcount())+1

#Initial plot for default slider value=35 
days_reach_death_count=cases_summary.loc[(cases_summary['deaths']>=35)].groupby(cases_summary['loc']).head(1).reset_index()

slider = Slider(start=10, end=max(cases_summary['deaths']), value=35, step=10, title="Total Deaths")

source = ColumnDataSource(data=dict(days_reach_death_count[['loc','day_count', 'deaths']]))

q = figure(x_range=days_reach_death_count['loc'], plot_width=1200, plot_height=600, sizing_mode="scale_both")


q.title.align = 'center'
q.title.text_font_size = '17px'
q.xaxis.axis_label = 'State'
q.yaxis.axis_label = 'Days since 1st Case'
q.xaxis.major_label_orientation = math.pi/2

q.vbar('loc', top='day_count', width=0.9, source=source)

deaths = slider.value
q.title.text = 'Days to reach %d Deaths' % deaths

hover = HoverTool(line_policy='next')
hover.tooltips = [('State', '@loc'),
                  ('Days since 1st Case', '@day_count'),  # @$name gives the value corresponding to the legend
                  ('Deaths', '@deaths')
                  ]
q.add_tools(hover)

def update(attr, old, new):
    days_death_count = cases_summary.loc[(cases_summary['deaths'] >= slider.value)].groupby(cases_summary['loc']).head(1).reindex()
    source.data = [ColumnDataSource().from_df(days_death_count)]

slider.on_change('value', update)

layout = row(q, slider)
tab = Panel(child=layout, title="New Confirmed Cases since Day 1")
tabs= Tabs(tabs=[tab])

curdoc().add_root(tabs)

4

1 回答 1

0

您的代码有 2 个问题

  • (critical)source.data必须是字典,但您要为其分配一个数组
  • (minor)from_df是一个类方法,你不必构造它的对象

尝试source.data = ColumnDataSource.from_df(days_death_count)改用。

于 2020-05-05T16:30:33.133 回答