【问题标题】:In Python Plotly is there a bar chart equivilant to histogram binning?在 Python Plotly 中是否有与直方图分箱等效的条形图?
【发布时间】:2019-07-27 01:45:52
【问题描述】:

我正在寻找一种方式图,使用 Plotly,X 轴上带有日期的数据和 Y 轴上的表格中的值。然后我希望能够按周、日、月等动态(即使用小部件)对数据进行分组,就像在 Plotly Histogram 分箱示例中一样 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)

【问题讨论】:

    标签: python python-3.x pandas plotly


    【解决方案1】:

    是的,您可以将Histogram 跟踪的histfunc 参数设置为"sum"(默认为"count")。有关histfunc 的更多信息可以在这里找到:https://plot.ly/python/histograms/

    【讨论】:

      猜你喜欢
      • 2017-01-08
      • 2021-11-04
      • 2022-12-21
      • 1970-01-01
      • 2021-08-07
      • 2011-05-08
      • 2017-01-04
      • 2012-09-05
      • 2021-11-28
      相关资源
      最近更新 更多