0

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

Plot

Eugen
  • 2,292
  • 3
  • 29
  • 43
  • you could try passing the argument `'yref':'y'` to each annotation. what are the expected y positions of the annotations? – Derek O Jun 16 '22 at 19:00
  • Tried but I get the same output. The positions of the labels on Y axis should be 145.39 for the lower annotation and 149.64 for the other one. – Eugen Jun 16 '22 at 19:14
  • yref='paper' and y=0.5 for example works as expected (but is not what I need) – Eugen Jun 16 '22 at 19:16
  • I also tried it and it displayed correctly when the x-axis was specified with the date in string format. – r-beginners Jun 17 '22 at 04:51

1 Answers1

1

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

enter image description here

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

enter image description here

Derek O
  • 16,770
  • 4
  • 24
  • 43