问题描述
我想从 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)
错误 -
值错误: 为 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
有什么解决办法吗?
解决方法
就像错误说的那样,问题在于您将 Pandas Series
设置为 text
组件的 Candlestick
属性的值。
传递一个数字或一个字符串。
我不完全清楚您想使用 text 属性显示什么,但您可以将 df['perDel']
数据转换为字符串并将其传递给 text
:
text=",".join(df["perDel"].astype(str).values)
以上将显示 perDel
Series
中以逗号分隔的所有数字。