问题描述
我有一个以 15 分钟为间隔的几个月内欧元/日元汇率的图表,因此没有从周五晚上到周日晚上的数据.
I have a plotly graph of the EUR/JPY exchange rate across a few months in 15 minute time intervals, so as a result, there is no data from friday evenings to sunday evenings.
这里是部分数据,注意周末索引中的跳过(类型:DatetimeIndex):
Here is a portion of the data, note the skip in the index (type: DatetimeIndex) over the weekend:
以 plotly 的形式绘制此数据会导致缺失日期的差距使用上面的数据框:
Plotting this data in plotly results in a gap over the missing dates Using the dataframe above:
import plotly.graph_objs as go
candlesticks = go.Candlestick(x=data.index, open=data['Open'], high=data['High'],
low=data['Low'], close=data['Close'])
fig = go.Figure(layout=cf_layout)
fig.add_trace(trace=candlesticks)
fig.show()
输出:
如您所见,缺失日期的位置存在空白.我在网上找到的一种解决方案是使用以下方法将索引更改为文本:
As you can see, there are gaps where the missing dates are. One solution I've found online is to change the index to text using:
data.index = data.index.strftime("%d-%m-%Y %H:%M:%S")
并再次绘制它,这确实有效,但有它自己的问题.x 轴标签看起来很糟糕:
and plotting it again, which admittedly does work, but has it's own problem. The x-axis labels look atrocious:
我想生成一个图形,该图形绘制第二个图中没有间隙的图形,但 x 轴的显示方式与第一个图形上的一样.或者至少以更简洁和响应式的格式显示,尽可能接近第一个图表.
I would like to produce a graph that plots a graph like in the second plot where there are no gaps, but the x-axis is displayed like as it is on the first graph. Or at least displayed in a much more concise and responsive format, as close to the first graph as possible.
提前感谢您的帮助!
推荐答案
即使您的数据集中缺少某些日期,也会将您的日期解释为日期值,并在您的时间轴上显示甚至丢失的日期.一种解决方案是获取第一个和最后一个日期,构建完整的时间线,找出原始数据集中缺少哪些日期,并将这些日期包含在:
Even if some dates are missing in your dataset, plotly interprets your dates as date values, and shows even missing dates on your timeline. One solution is to grab the first and last dates, build a complete timeline, find out which dates are missing in your original dataset, and include those dates in:
fig.update_xaxes(rangebreaks=[dict(values=dt_breaks)])
这将变成这个数字:
进入这个:
完整代码:
import plotly.graph_objects as go
from datetime import datetime
import pandas as pd
import numpy as np
# sample data
df = 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 question
df = 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 date
dt_all = pd.date_range(start=df['Date'].iloc[0],end=df['Date'].iloc[-1])
# retrieve the dates that ARE in the original datset
dt_obs = [d.strftime("%Y-%m-%d") for d in pd.to_datetime(df['Date'])]
# define dates with missing values
dt_breaks = [d for d in dt_all.strftime("%Y-%m-%d").tolist() if not d in dt_obs]
# make fiuge
fig = 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 values
fig.update_xaxes(rangebreaks=[dict(values=dt_breaks)])
fig.update_layout(yaxis_title='AAPL Stock')
fig.show()
这篇关于Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!