如何在系列中的数据和时间戳中的索引中使用 plotly
How to use plotly with data in Series and indexes in Timestamps
我正在尝试绘制过去 5 天的股票价格。我正在使用 yfinance 每分钟导入具有相应日期的数据。
import yfinance as yf
data = yf.download(tickers='SOF.BR', period="5d" , interval="1m").CLose
print(data)
输出:
Datetime
2021-06-24 09:00:00+02:00 360.000000
2021-06-24 09:01:00+02:00 360.200012
2021-06-24 09:02:00+02:00 360.600006
2021-06-24 09:04:00+02:00 361.399994
2021-06-24 09:05:00+02:00 361.600006
...
2021-06-30 17:26:00+02:00 364.000000
2021-06-30 17:27:00+02:00 364.000000
2021-06-30 17:28:00+02:00 363.799988
2021-06-30 17:29:00+02:00 363.799988
2021-06-30 17:35:00+02:00 363.799988
现在如果我绘制这个系列,问题是当市场收盘时我会得到这些长的恒定线,所以我试图摆脱这些。
到目前为止,我发现的解决此问题的每种方法都依赖于两个数组中的数据。大多数解决方案使用 plotly.graph_objects 然后更新 xaxis。
我已经成功地尝试将我的 2 列放入一个数组中,但是因为系列的索引是一个时间戳,并且最后有这个奇怪的“+02:00”,所以它并不真正可用。带有日期的数组如下所示:
[Timestamp('2021-06-24 09:00:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:01:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:02:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:04:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:05:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:07:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:10:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:11:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:13:00+0200', tz='Europe/Brussels'),...]
所以我的问题是,如何转换这些数据以便我可以使用 plotly 处理它?
(或者如果您认为我搜索方向错误,我应该如何解决这个问题?)
由于您知道需要排除的时间,因此可以使用 rangebreaks 来完成此操作。它应该看起来像这样:
fig.update_xaxes(rangebreaks=[dict(values=breaks)])
整个代码应该是这样的:
fig.update_xaxes(
rangebreaks=[
# Below values are bound (not single values), ie. hide x to y
dict(bounds=["sat", "mon"]), # hide weekends, eg. hide sat to before mon
dict(bounds=[16, 9.5], pattern="hour"), # hide hours outside of 9.30am-4pm
# dict(values=["2020-12-25", "2021-01-01"]) # holidays
]
)
用法示例:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
fig.update_xaxes(
rangeslider_visible=True,
rangebreaks=[
# NOTE: Below values are bound (not single values), ie. hide x to y
#dict(bounds=["sat", "mon"]), # hide weekends, eg. hide sat to before mon
#dict(bounds=[16, 9.5], pattern="hour"), # hide hours outside of 9.30am-4pm
dict(values=["2015-02-18", "2017-01-04"]) # hide holidays (Christmas and New Year's, etc)
]
)
fig.show()
我正在尝试绘制过去 5 天的股票价格。我正在使用 yfinance 每分钟导入具有相应日期的数据。
import yfinance as yf
data = yf.download(tickers='SOF.BR', period="5d" , interval="1m").CLose
print(data)
输出:
Datetime
2021-06-24 09:00:00+02:00 360.000000
2021-06-24 09:01:00+02:00 360.200012
2021-06-24 09:02:00+02:00 360.600006
2021-06-24 09:04:00+02:00 361.399994
2021-06-24 09:05:00+02:00 361.600006
...
2021-06-30 17:26:00+02:00 364.000000
2021-06-30 17:27:00+02:00 364.000000
2021-06-30 17:28:00+02:00 363.799988
2021-06-30 17:29:00+02:00 363.799988
2021-06-30 17:35:00+02:00 363.799988
现在如果我绘制这个系列,问题是当市场收盘时我会得到这些长的恒定线,所以我试图摆脱这些。
到目前为止,我发现的解决此问题的每种方法都依赖于两个数组中的数据。大多数解决方案使用 plotly.graph_objects 然后更新 xaxis。
我已经成功地尝试将我的 2 列放入一个数组中,但是因为系列的索引是一个时间戳,并且最后有这个奇怪的“+02:00”,所以它并不真正可用。带有日期的数组如下所示:
[Timestamp('2021-06-24 09:00:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:01:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:02:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:04:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:05:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:07:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:10:00+0200', tz='Europe/Brussels'), Timestamp('2021-06-24 09:11:00+0200', tz='Europe/Brussels'),
Timestamp('2021-06-24 09:13:00+0200', tz='Europe/Brussels'),...]
所以我的问题是,如何转换这些数据以便我可以使用 plotly 处理它?
(或者如果您认为我搜索方向错误,我应该如何解决这个问题?)
由于您知道需要排除的时间,因此可以使用 rangebreaks 来完成此操作。它应该看起来像这样:
fig.update_xaxes(rangebreaks=[dict(values=breaks)])
整个代码应该是这样的:
fig.update_xaxes(
rangebreaks=[
# Below values are bound (not single values), ie. hide x to y
dict(bounds=["sat", "mon"]), # hide weekends, eg. hide sat to before mon
dict(bounds=[16, 9.5], pattern="hour"), # hide hours outside of 9.30am-4pm
# dict(values=["2020-12-25", "2021-01-01"]) # holidays
]
)
用法示例:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
fig.update_xaxes(
rangeslider_visible=True,
rangebreaks=[
# NOTE: Below values are bound (not single values), ie. hide x to y
#dict(bounds=["sat", "mon"]), # hide weekends, eg. hide sat to before mon
#dict(bounds=[16, 9.5], pattern="hour"), # hide hours outside of 9.30am-4pm
dict(values=["2015-02-18", "2017-01-04"]) # hide holidays (Christmas and New Year's, etc)
]
)
fig.show()