首页 > 解决方案 > 用其他列的值标记每根蜡烛高点

问题描述

我想从 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:为 layout.annotation 的“text”属性接收到的“pandas.core.series.Series”类型的无效值接收到的值: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 名称:perDel,长度:251,dtype:对象

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

有什么解决办法吗?

标签: plotly-dashcandlestick-chart

解决方案


就像错误所说的问题是您将熊猫设置为组件属性的Series值。textCandlestick

传递一个数字或一个字符串。

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

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

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


推荐阅读