简体   繁体   中英

Cufflinks - misplaced annotations on OHLC chart

The annotation y-position doesn't correspond to the position I set in the API call. Am I doing something wrong here?

The x-position is correct.

qf=cf.QuantFig(
    df,
    title=f'{symbol} - {date}',
    name=symbol,
    theme='pearl',
    up_color='green',
    down_color='red',
)
qf.add_annotations(
    [
        {
            'x': idx_high,
            'y': df.loc[idx_high]['high'],
            'text': f'High: {df.loc[idx_high]["high"]}',
            'showarrow':False,
            'textangle': 0
        },
        {
            'x': idx_low,
            'y': df.loc[idx_low]['low'],
            'text': f'Low: {df.loc[idx_low]["low"]}',
            'showarrow':False,
            'textangle': 0
        },
    ]
)
f = qf.figure()
f.show()

阴谋

I think the way add_annotations function from cufflinks works differently than the add_annotation function in plotly. You could probably figure out the exact reason if you look into the cufflinks source for add_annotations.

The first part of the code is reproducing a candlestick plot using some sample stocks data:

import pandas as pd
import cufflinks as cf

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df.set_index("Date", inplace=True)

## rename implied column names from your data
## APPL.High --> high
df.columns = [name.split('.')[-1].lower() for name in df.columns]

## choose a random subset of data
df = df.loc["2016-01-01":"2016-06-01"]
idx_high = df['high'].idxmax()
idx_low = df['low'].idxmin()

qf=cf.QuantFig(
    df,
    # title=f'{symbol} - {date}',
    # name=symbol,
    theme='pearl',
    up_color='green',
    down_color='red',
)

Then if I use the add_annotations method from cufflinks, I can reproduce the same issue as you: the y values of the annotations aren't correct.

qf.add_annotations(
    [
        {
            'x': idx_high,
            'y': df.loc[idx_high]['high'],
            'text': f'High: {df.loc[idx_high]["high"]}',
            'showarrow':False,
            'textangle': 0
        },
        {
            'x': idx_low,
            'y': df.loc[idx_low]['low'],
            'text': f'Low: {df.loc[idx_low]["low"]}',
            'showarrow':False,
            'textangle': 0
        },
    ]
)
f = qf.figure()
f.show()

在此处输入图像描述

But if I instead use the add_annotations method for f (which is a plotly graph object), then the annotations appear in the correct location:

f = qf.figure()
f.add_annotation(
    {
        'x': idx_high,
        'y': df.loc[idx_high]['high'],
        'text': f'High: {df.loc[idx_high]["high"]}',
        'showarrow':False,
        'textangle': 0
    },
)
f.add_annotation(
    {
        'x': idx_low,
        'y': df.loc[idx_low]['low'],
        'text': f'Low: {df.loc[idx_low]["low"]}',
        'showarrow':False,
        'textangle': 0
    },
)
f.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