Python按天和月绘制直方图,但不按周



下面是这里的示例:https://plot.ly/python/aggregations/#histogram-装仓

他们的代码按预期工作,但我正试图将其扩展到按周、按天、按月等的autobin。我知道这个数据集没有时间,但我也想用小时来存储我自己的一组有时间的数据。这看起来很简单,但这段代码没有产生正确的结果:

import plotly.io as pio
import pandas as pd
df = pd.read_csv("https://plot.ly/~public.health/17.csv")
data = [dict(
x = df['date'],
autobinx = False,
autobiny = True,
marker = dict(color = 'rgb(68, 68, 68)'),
name = 'date',
type = 'histogram',
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>Shooting Incidents</b>',
xaxis = dict(
title = '',
type = 'date'
),
yaxis = dict(
title = 'Shootings Incidents',
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 = 'Quater',
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)
pio.show(fig_dict, validate=False)

有人知道如何让垃圾箱按周(以及假设的垃圾箱按小时(上班吗?谢谢

我想好了如何做我想做的事情。答案隐藏在Plot.ly文档中:https://plot.ly/python/reference/

特别是在xbins.size下,它们指的是遵循axis.dtick中的相同方案

dtick父级:数据[类型=直方图].marker.colorbar类型:数字或分类坐标字符串

设置此轴上刻度之间的步长。与tick0一起使用。必须是正数,或可用于"日志"one_answers"日期"轴的特殊字符串。如果轴type是"log",则每隔10^(n"dtick(设置一次刻度,其中n是刻度号。例如,要将刻度标记设置为1、10、100、1000,…请将dtick设置为1。要将勾号设置为1100、10000、。。。将dtick设置为2。要将勾号设置为1、5、25、125、625、3125。。。将dtick设置为log_10(5(或0.69897000433。"log"有几个特殊值;"L",其中f是一个正数,给出了值(但不是位置(线性间隔的刻度。例如,tick0=0.1,dtick="L0.5"会将刻度设置为0.1、0.6、1.1、1.6等。要显示10的幂加上其间的小数字,请使用"D1"(所有数字(或"D2"(仅2和5(。对于"D1"one_answers"D2",忽略tick0如果轴type是"日期",则必须将时间转换为毫秒。例如,要将刻度之间的间隔设置为一天,请将dtick设置为86400000.0。"日期"也有特殊的值"M"给出了以几个月为间隔的刻度。n必须是正整数。若要在每三个月的15日设置刻度,请将tick0设置为"2000-01-15",将dtick设置为"M3"。要每4年设置一次刻度,请将dtick设置为"M48">

因此,仓位大小的新代码片段是:

buttons = [
dict(
args = ['xbins.size', ' 3600000.0'],
label = 'Hour',
method = 'restyle',
), dict(
args = ['xbins.size', '86400000.0'],
label = 'Day',
method = 'restyle',
), dict(
args = ['xbins.size', ' 604800000.0'],
label = 'Week',
method = 'restyle',
), dict(
args = ['xbins.size', 'M1'],
label = 'Month',
method = 'restyle',
)]

但考虑到这一点,我会怀疑使用"D1"也不会奏效。如果Plot.ly的工作人员看到了这一点,你能记下更新示例以指出这一具体的细微差别吗?

最新更新