袖扣 - OHLC图表上的放错了注释

发布于 2025-02-07 20:33:16 字数 840 浏览 1 评论 0原文

注释Y位置与我在API调用中设置的位置不符。我在这里做错了吗?

X位置是正确的。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-02-14 20:33:18

我认为add_annotations的方式来自cufflinks的工作方式与add_annotation在Plotly中的函数不同。如果您查看袖扣用于add_annotations。

代码的第一部分是使用一些示例库存数据重现烛台图:

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',
)

然后,如果我使用袖扣中的add_annotations方法,我可以重现与您相同的问题:注释的y值不正确。

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

但是,如果我使用add_annotations f的方法(这是一个绘图图对象),则注释在正确的位置显示:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文