我正在寻找一个路线图,使用 Plotly,X 轴上带有日期的数据,Y 轴上带有表格中的值。然后我希望能够按周、日、月等动态对数据进行分组(即使用小部件),就像 Plotly 直方图分箱示例 https://plot.ly/python/aggregations/#histogram-binning 中那样
只要我可以让图表动态执行此操作,即无需创建全新的绘图,则具有相同或相似的功能是可以接受的
我已经尝试使用文档中概述的直方图分箱,但这不起作用,因为直方图依赖于计算表中的行,而不是直接从表中读取值并将其用作直方图高度。
但是相同的功能似乎不适用于条形图
我的数据在 python pandas 数据框中是这样排列的
Date Count
0 2018-01-23 28418
1 2018-08-01 25403
现在的代码本身是:
data = [dict(
x = final['Date'],
y = final['Cage Poll [cases]'],
autobinx = False,
autobiny = True,
marker = dict(color = 'rgb(220, 20, 127)'),
name = 'test',
type = 'histogram',
normed = 'True',
xbins = dict(
#end = '2016-12-31 12:00',
size = 'M1',
#start = '1983-12-31 12:00'
)
)]
layout = dict(
paper_bgcolor = 'rgb(240, 240, 240)',
plot_bgcolor = 'rgb(240, 240, 240)',
title = '<b>Data sampled from daily reports</b>',
showlegend = True,
xaxis = dict(
title = 'Date',
type = 'date'
),
yaxis = dict(
title = 'Count',
type = 'linear'
),
updatemenus = [dict(
x = 0.1,
y = 1.15,
xref = 'paper',
yref = 'paper',
yanchor = 'top',
active = 1,
showactive = True,
buttons = [
dict(
args = ['xbins.size', 'D1'],
label = 'Day',
method = 'restyle',
), dict(
args = ['xbins.size', 'D7'],
label = 'Week',
method = 'restyle',
), dict(
args = ['xbins.size', 'M1'],
label = 'Month',
method = 'restyle',
), dict(
args = ['xbins.size', 'M3'],
label = 'Quarter',
method = 'restyle',
), dict(
args = ['xbins.size', 'M6'],
label = 'Half Year',
method = 'restyle',
), dict(
args = ['xbins.size', 'M12'],
label = 'Year',
method = 'restyle',
)]
)]
)
fig_dict = dict(data=data, layout=layout)
iplot(fig_dict, validate=False)
最佳答案
是的,您可以将直方图
跟踪的histfunc
参数设置为“sum”
(默认为“count”
)。有关 histfunc
的更多信息可以在这里找到:https://plot.ly/python/histograms/
关于python - 在Python Plotly中是否有相当于直方图分箱的条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57228543/