Plotly:如何设置绘图图形的样式,以使其不显示缺失日期的间隙?

Plotly:如何设置绘图图形的样式,以使其不显示缺失日期的间隙?,第1张

Plotly:如何设置绘图图形的样式,以使其不显示缺失日期的间隙?

即使数据集中缺少某些日期,也可以将日期作为日期值进行解释,甚至在时间轴上显示丢失的日期。一种解决方案是获取第一个和最后一个日期,构建完整的时间轴,找出原始数据集中缺少的日期,并将这些日期包括在以下内容中:

fig.update_xaxes(rangebreaks=[dict(values=dt_breaks)])

这将使这个数字:

变成这个:

完整的代码:

import plotly.graph_objects as gofrom datetime import datetimeimport pandas as pdimport numpy as np# sample datadf = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')# remove some dates to build a similar case as in the questiondf = df.drop(df.index[75:110])df = df.drop(df.index[210:250])df = df.drop(df.index[460:480])# build complete timepline from start date to end datedt_all = pd.date_range(start=df['Date'].iloc[0],end=df['Date'].iloc[-1])# retrieve the dates that ARE in the original datsetdt_obs = [d.strftime("%Y-%m-%d") for d in pd.to_datetime(df['Date'])]# define dates with missing valuesdt_breaks = [d for d in dt_all.strftime("%Y-%m-%d").tolist() if not d in dt_obs]# make fiugefig = go.Figure(data=[go.Candlestick(x=df['Date'],     open=df['AAPL.Open'], high=df['AAPL.High'],     low=df['AAPL.Low'], close=df['AAPL.Close'])])# hide dates with no valuesfig.update_xaxes(rangebreaks=[dict(values=dt_breaks)])fig.update_layout(yaxis_title='AAPL Stock')fig.show()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5629694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存