python - 烛台图 add_trace(mode="markers") 给出错误的输出
问题描述
我目前正在用破折号和情节构建一个财务仪表板。我在仪表板上添加了以下烛台图表:
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'])])
我希望能够突出显示特定的烛台(例如使用标记)
我试图用add_trace
函数和以下代码来实现这一点:
candlestick_chart.add_trace(
go.Scatter(
x=["2020-07-01"],
y=["350"],
mode="markers",
marker=dict(symbol="6")
)
)
但这破坏了图表。
为什么会这样?我怎样才能解决这个问题?
编辑:添加的数据源
我从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)
EDIT2:系统和版本
我的包有以下版本:
print(pd.__version__) # 1.2.3
print(plotly.__version__) # 4.14.3
我正在与:
- Windows 10 家庭版(64 位)
- 蟒蛇 3.9
- Python 3.8 也不起作用
解决方案
这可以被视为版本问题,但核心问题是您已将您定义y-value
为字符串列表,["350"]
而不是数字[350]
,例如:
go.Scatter(
x=["2020-07-01"],
y=["350"],
mode="markers",
marker=dict(symbol="6")
)
)
不同版本的 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()
推荐阅读
- python - 如何从python中的文本文件中读取现在未显示的某些行?
- ios - 如何在 SwiftUI 中预填表单
- python - 如何使用分页将数据抓取到 Excel 文件中
- node.js - 电子邮件 Node.js 类型错误的基础
- sql - 将 SQL 查询复制并粘贴到 MS Word
- email - 使用命令行从应用程序在浏览器中打开 GMail 帐户
- c - 在linux shell脚本中用特殊字符替换文本?
- ruby-on-rails - 相册中所有照片的 Rails 标签
- java - LinkedList .equals vs == 整数上的运算符
- c++ - C ++用户定义的函数来填充数组