首页 > 解决方案 > 烛台图 add_trace(mode="markers") 给出错误的输出

问题描述

我目前正在用破折号和情节构建一个财务仪表板。我在仪表板上添加了以下烛台图表:

    candlestick_chart = go.Figure(data=[go.Candlestick(x=financial_data["Date"],
                                     open=financial_data['Open'],
                                     high=financial_data['High'],
                                     low=financial_data['Low'],
                                     close=financial_data['Close'])])

返回预期结果: 在此处输入图像描述

我希望能够突出显示特定的烛台(例如使用标记)

我试图用add_trace函数和以下代码来实现这一点:

    candlestick_chart.add_trace(
    go.Scatter(
        x=["2020-07-01"],
        y=["350"],
        mode="markers",
        marker=dict(symbol="6")

    )
)

但这破坏了图表。

在此处输入图像描述

为什么会这样?我怎样才能解决这个问题?

编辑:添加的数据源

我从https://finance.yahoo.com/quote/SPY/history?p=SPY获取数据,时间段设置为最大值。

我通过以下方式解析数据:

    start = "2000-01-01"
end = "2021-01-01"

# Get a pandas dataframe
datapath = ('D:\\Programmieren\\trading_bot\\etf_data\\SPY.csv')

financial_data = pd.read_csv(datapath,
                             parse_dates=True,
                             index_col=0)

financial_data= financial_data.loc[start:end]
# Process data
financial_data = financial_data["2020-06-01":"2021-01-01"]

financial_data.reset_index(inplace=True)

EDIT2:系统和版本

我的包有以下版本:

print(pd.__version__) # 1.2.3
print(plotly.__version__) # 4.14.3

我正在与:

标签: pythonplotlycandlestick-chart

解决方案


可以被视为版本问题,但核心问题是您已将您定义y-value为字符串列表,["350"]而不是数字[350],例如:

go.Scatter(
        x=["2020-07-01"],
        y=["350"],
        mode="markers",
        marker=dict(symbol="6")

    )
)

不同版本的 plotly 似乎以不同的方式处理这个问题。只需删除引号,让 Plotly 将值解释为数字,而不是生成:

在此处输入图像描述

带有示例数据的完整代码

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df=df.tail(10)

# set up figure with values not high and not low
# include candlestick with rangeselector
fig = go.Figure(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']))

fig.add_trace(
    go.Scatter(
        x=["2017-02-10"],
        y=[135],
        mode="markers+text",
        marker=dict(symbol='triangle-down-open', size = 12),
#         text = 'important',
#         textposition = 'middle right'

    )
)

fig.show()

推荐阅读