简体   繁体   中英

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' . 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.

情节的例子。在日期上添加 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). 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. 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 . And since plotly is already interpreting your x-axis as a timeline, it will accept dates that occur between dates in your dataframe. Plotly will even handle timestamps are not only dates, but time of day as well.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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