plotly-dash - 用其他列的值标记每根蜡烛高点
问题描述
我想从 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
有什么解决办法吗?
解决方案
就像错误所说的问题是您将熊猫设置为组件属性的Series
值。text
Candlestick
传递一个数字或一个字符串。
我并不完全清楚您想使用 text 属性显示什么,但您可以将df['perDel']
数据转换为字符串并将其传递给text
:
text=",".join(df["perDel"].astype(str).values)
以上将显示perDel
Series
以逗号分隔的所有数字。
推荐阅读
- excel - 如何将弹出窗口中的 ID 与 Excel 中的 VBA 对齐?
- python - python如何在调用流取消()后处理grpc流
- sql - 将几个查询“SELECT INTO..”合并为一个
- linux - 脚本应输出此文本“this is text”;“Nano”;”超过 10 个字符
- java - 如何在Android中使用后台服务覆盖两次甚至3次单击电源按钮甚至音量上/下键?
- testng-eclipse - 如何在testng中一个接一个地运行类
- java - 如何使用 Eclipse 和 Apache CXF 从 WSDL 创建的 java 类连接到工作日帐户
- java - 错误:发生了 JNI 错误。线程“主”java.lang.VerifyError 中的异常。此位置的预期堆栈图帧
- reactjs - 为什么我们在 axios 获取数据时使用 useeffect() react hook?
- node.js - 如何使用 axios 进行 OAuth 登录