<i id='JtV2Q'><tr id='JtV2Q'><dt id='JtV2Q'><q id='JtV2Q'><span id='JtV2Q'><b id='JtV2Q'><form id='JtV2Q'><ins id='JtV2Q'></ins><ul id='JtV2Q'></ul><sub id='JtV2Q'></sub></form><legend id='JtV2Q'></legend><bdo id='JtV2Q'><pre id='JtV2Q'><center id='JtV2Q'></center></pre></bdo></b><th id='JtV2Q'></th></span></q></dt></tr></i><div id='JtV2Q'><tfoot id='JtV2Q'></tfoot><dl id='JtV2Q'><fieldset id='JtV2Q'></fieldset></dl></div>

      <legend id='JtV2Q'><style id='JtV2Q'><dir id='JtV2Q'><q id='JtV2Q'></q></dir></style></legend>
      <tfoot id='JtV2Q'></tfoot>
    1. <small id='JtV2Q'></small><noframes id='JtV2Q'>

        <bdo id='JtV2Q'></bdo><ul id='JtV2Q'></ul>

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

        时间:2023-09-29

          <legend id='wwjlh'><style id='wwjlh'><dir id='wwjlh'><q id='wwjlh'></q></dir></style></legend>
              • <i id='wwjlh'><tr id='wwjlh'><dt id='wwjlh'><q id='wwjlh'><span id='wwjlh'><b id='wwjlh'><form id='wwjlh'><ins id='wwjlh'></ins><ul id='wwjlh'></ul><sub id='wwjlh'></sub></form><legend id='wwjlh'></legend><bdo id='wwjlh'><pre id='wwjlh'><center id='wwjlh'></center></pre></bdo></b><th id='wwjlh'></th></span></q></dt></tr></i><div id='wwjlh'><tfoot id='wwjlh'></tfoot><dl id='wwjlh'><fieldset id='wwjlh'></fieldset></dl></div>

                  <tbody id='wwjlh'></tbody>
                <tfoot id='wwjlh'></tfoot>
                • <bdo id='wwjlh'></bdo><ul id='wwjlh'></ul>

                  <small id='wwjlh'></small><noframes id='wwjlh'>

                  本文介绍了Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个以 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:如何设置绘图图形的样式,使其不显示缺失日期的间隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python将绘图保存到本地文件并插入到html中 下一篇:将配置模式添加到 Plotly.Py 离线 - 模式栏

                  相关文章

                    <i id='22nlP'><tr id='22nlP'><dt id='22nlP'><q id='22nlP'><span id='22nlP'><b id='22nlP'><form id='22nlP'><ins id='22nlP'></ins><ul id='22nlP'></ul><sub id='22nlP'></sub></form><legend id='22nlP'></legend><bdo id='22nlP'><pre id='22nlP'><center id='22nlP'></center></pre></bdo></b><th id='22nlP'></th></span></q></dt></tr></i><div id='22nlP'><tfoot id='22nlP'></tfoot><dl id='22nlP'><fieldset id='22nlP'></fieldset></dl></div>

                  1. <tfoot id='22nlP'></tfoot>

                    <small id='22nlP'></small><noframes id='22nlP'>

                    • <bdo id='22nlP'></bdo><ul id='22nlP'></ul>
                    1. <legend id='22nlP'><style id='22nlP'><dir id='22nlP'><q id='22nlP'></q></dir></style></legend>