简体   繁体   English

情节:如何在烛台图表中的日期之间绘制垂直线?

[英]Plotly: How to draw vertical lines between dates in a candlestick chart?

Take a look at the third example here (in the section Adding Customized Text and Annotations ).看看这里的第三个例子(在添加自定义文本和注释部分)。 If you zoom in in the chart, you can see that the example inserted a vertical line on the date '2007-12-01' .如果放大图表,您可以看到该示例在日期'2007-12-01'上插入了一条垂直线。 If the date were a trading day (eg '2007-11-29' ), one would have seen the vertical line go through that day's candlestick right in the middle.如果日期是一个交易日(例如'2007-11-29' ),人们就会看到垂直线穿过当天的烛台正中间。

情节的例子。在日期上添加 v 线。

I want to draw a vertical line between two dates (eg between Nov 29 and Nov 30 - the two candlesticks just before the v-line in the above example).我想在两个日期之间画一条垂直线(例如,在 11 月 29 日和 11 月 30 日之间 - 上例中 v 线之前的两个烛台)。 How can I do that?我该怎么做?

If you'd like to find a date or time between two dates, you should consider building your candlestick charts on dates as pandas timestamps.如果您想查找两个日期之间的日期或时间,您应该考虑在日期上构建烛台图表作为熊猫时间戳。 You'll find an example under Simple Example with datetime Objects a bit further down on the same page that you are referring to.您将在Simple Example with datetime ObjectsSimple Example with datetime Objects示例下找到一个示例,该示例位于您所指的同一页面的更下方。 But don't worry, you'll find a complete code snippet for a similar approach in this answer.但别担心,您会在此答案中找到类似方法的完整代码片段。

If your dates are in fact formatted as pandas datestamps, you can easily find a date between two dates using pd.to_pydatetime and various approaches as described here .如果您的日期实际上被格式化为Pandas日期,您可以使用pd.to_pydatetime此处描述的各种方法轻松找到两个日期之间的日期。 And since plotly is already interpreting your x-axis as a timeline, it will accept dates that occur between dates in your dataframe.并且由于 plotly 已经将您的 x 轴解释为时间轴,因此它将接受数据框中日期之间发生的日期。 Plotly will even handle timestamps are not only dates, but time of day as well. Plotly 甚至会处理时间戳不仅是日期,而且是一天中的时间。

So if your dates in question are:因此,如果您的日期有问题:

datetime.datetime(2020, 10, 11, 0, 0)

and:和:

datetime.datetime(2020, 10, 12, 0, 0)

then the approach below will give you:那么下面的方法会给你:

datetime.datetime(2020, 10, 11, 12, 0)

which will give you a line between two dates, just like you asked for.就像您要求的那样,这将为您提供两个日期之间的界线。

Take a look:看一看:

在此处输入图片说明

Complete snippet with data and plotly code:包含数据和情节代码的完整片段:

import pandas as pd
import plotly.graph_objects as go
from datetime import datetime

# data
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
dates = [datetime(year=2020, month=10, day=10),
         datetime(year=2020, month=10, day=11),
         datetime(year=2020, month=10, day=12),
         datetime(year=2020, month=10, day=13),
         datetime(year=2020, month=10, day=14)]


# data organized in a pandas dataframe
df=pd.DataFrame(dict(open=open_data,
                    high=high_data,
                    low=low_data,
                    close=close_data,
                    dates=dates))

# calculations using to_pydatetime() to get the date/time
# between your dates
a=df.dates.iloc[1].to_pydatetime()
b=df.dates.iloc[2].to_pydatetime()
linedate = a + (b - a)/2

# plotly figure setup
fig = go.Figure(data=[go.Candlestick(x=df.dates,
                       open=open_data, high=high_data,
                       low=low_data, close=close_data)])

# edit layouts
fig.update_layout(
    title='Dates are pandas timestamps',
    yaxis_title='AAPL Stock',
    shapes = [dict(
        x0=linedate, x1=linedate, y0=0, y1=1, xref='x', yref='paper',
        line_width=2)],
    annotations=[dict(x=linedate, y=0.8, xref='x', yref='paper',font=dict(
                        color="blue",size=14),
        showarrow=False, xanchor='left', text='Vertical line between two dates')]
)

fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM