烛台图 add_trace(mode="markers") 给出错误的输出

Ph.*_*lpp 3 python candlestick-chart plotly

我目前正在构建一个带有破折号和绘图的财务仪表板。我将以下烛台图添加到我的仪表板中:

    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'])])
Run Code Online (Sandbox Code Playgroud)

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

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

我尝试使用该add_trace函数和以下代码来实现此目的:

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

    )
)
Run Code Online (Sandbox Code Playgroud)

但这破坏了图表。

在此输入图像描述

为什么会发生这种情况?我怎样才能解决这个问题?

编辑:添加数据源

我从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)
Run Code Online (Sandbox Code Playgroud)

EDIT2:系统和版本

我的软件包有以下版本:

print(pd.__version__) # 1.2.3
print(plotly.__version__) # 4.14.3
Run Code Online (Sandbox Code Playgroud)

我正在与:

  • Windows 10 家庭版(64 位)
  • Python 3.9
  • Python 3.8 也不起作用

ves*_*and 7

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

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

    )
)
Run Code Online (Sandbox Code Playgroud)

不同版本的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()
Run Code Online (Sandbox Code Playgroud)