即使数据集中缺少某些日期,也可以将日期作为日期值进行解释,甚至在时间轴上显示丢失的日期。一种解决方案是获取第一个和最后一个日期,构建完整的时间轴,找出原始数据集中缺少的日期,并将这些日期包括在以下内容中:
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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)