用其他列的值标记每根蜡烛的高位

Labelling on each candle high with value from other column

我想增加第 perDel 列的值。

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go

import pandas as pd
from datetime import datetime

df = pd.read_csv(r'..\CandlesPlotPython\APOLLOALLN.csv')

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Checklist(
        id='toggle-rangeslider',
        options=[{'label': 'Include Rangeslider', 
                  'value': 'slider'}],
        value=['slider']
    ),
    dcc.Graph(id="graph"),
])

@app.callback(
    Output("graph", "figure"), 
    [Input("toggle-rangeslider", "value")])
def display_candlestick(value):
    fig = go.Figure(go.Candlestick(
        x=df['Date'],
        open=df['Open Price'],
        high=df['High Price'],
        low=df['Low Price'],
        close=df['Close Price'],
        text=df['perDel']
        
    ))
    
    
    
    
    fig.update_layout(
        xaxis_rangeslider_visible='slider' in value,
        annotations=[dict(
        x=df['Date'], y=df['High Price'], xref='x', yref='paper',
        showarrow=False, xanchor='left', text=df['perDel'].astype(str))]
        
    )

    return fig

app.run_server(debug=True)

错误-

ValueError: Invalid value of type 'pandas.core.series.Series' received for the 'text' property of layout.annotation Received value: 0 52.5 1 35.54 2 42.56 3 39.88 4 49.67 ... 246 59.7 247 47.73 248 59.1 249 55.53 250 42.03 Name: perDel, Length: 251, dtype: object

The 'text' property is a string and must be specified as:
  - A string
  - A number that will be converted to a string

有什么解决办法吗?

就像错误说的那样,问题是您将 pandas Series 设置为 [=13= 的 text 属性 的值] 组件。

传递数字或字符串。

我不完全清楚您想使用文本 属性 显示什么,但您可以将 df['perDel'] 数据转换为字符串并将其传递给 text

text=",".join(df["perDel"].astype(str).values)

以上将显示 perDel Series 中的所有数字,并以逗号分隔。