python - 在Python Plotly中是否有相当于直方图分箱的条形图?

标签 python python-3.x pandas plotly

我正在寻找一个路线图,使用 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/

相关文章:

python - 将二维数组中的元素更改为数组

python - 难以使用具有不同格式字符串的 Python struct.Struct.unpack_from

Python多线程和HTTP请求

python-3.x - 如何在一次或多次调用中为多个变量 reshape 数据框,从宽到长?

python - 谁能明白为什么我的 python 正则表达式搜索只输出 "0"s?

python - 获取从 -X 到 X 均匀分布的值的 N 元素向量

python-3.x - 如何将权重从 2D 卷积网络复制到 Keras 上的 3D 卷积网络?

python - 我们能否通过接受(或忽略)新功能使 ML 模型(pickle 文件)更加健壮?

python - 如果满足条件,如何将两列添加或组合到数据框中的另一列中

python - Pandas read_csv() 损失数字精度