python - PlotLy:无法在子图上绘制烛台图
问题描述
我正在尝试绘制一个 3-subplot PlotLy 图。从上到下:烛台、法线图、法线图。
这是我的代码:
def chart_strategy(df):
# Initialize figure with subplots
fig = make_subplots(rows=3, cols=1)
fig.add_trace(go.Figure( data = [go.Candlestick( x = df.index,
open = df['Open'],
close = df['Close'],
low = df['Low'],
high = df['High'])]),
row = 1, col = 1)
fig.add_trace(go.Scatter(x = df.index, y = df['System Quality Number']), row = 2, col = 1)
fig.add_trace(go.Scatter(x = df.index, y = df['Action']), row = 3, col =1)
return fig
我收到以下错误:
ValueError:
Invalid element(s) received for the 'data' property of
Invalid elements include: [Figure({
'data': [{'close': array([120.88, 120.93, 120.72, ..., 116.23, 115.78, 115.63]),
'high': array([121.31, 121.2 , 121.11, ..., 116.86, 116.43, 115.73]),
'low': array([120.8 , 120.79, 120.48, ..., 115.94, 115.68, 115.32]),
'open': array([121. , 120.81, 120.92, ..., 116.36, 116.23, 115.71]),
'type': 'candlestick',
'x': array([datetime.datetime(2019, 7, 19, 0, 0),
datetime.datetime(2019, 7, 22, 0, 0),
datetime.datetime(2019, 7, 23, 0, 0), ...,
datetime.datetime(2020, 5, 12, 0, 0),
datetime.datetime(2020, 5, 13, 0, 0),
datetime.datetime(2020, 5, 14, 0, 0)], dtype=object)}],
'layout': {'template': '...'}})]
The 'data' property is a tuple of trace instances
that may be specified as:
- A list or tuple of trace instances
(e.g. [Scatter(...), Bar(...)])
- A single trace instance
(e.g. Scatter(...), Bar(...), etc.)
- A list or tuple of dicts of string/value properties where:
- The 'type' property specifies the trace type
One of: ['area', 'bar', 'barpolar', 'box',
'candlestick', 'carpet', 'choropleth',
'choroplethmapbox', 'cone', 'contour',
'contourcarpet', 'densitymapbox', 'funnel',
'funnelarea', 'heatmap', 'heatmapgl',
'histogram', 'histogram2d',
'histogram2dcontour', 'image', 'indicator',
'isosurface', 'mesh3d', 'ohlc', 'parcats',
'parcoords', 'pie', 'pointcloud', 'sankey',
'scatter', 'scatter3d', 'scattercarpet',
'scattergeo', 'scattergl', 'scattermapbox',
'scatterpolar', 'scatterpolargl',
'scatterternary', 'splom', 'streamtube',
'sunburst', 'surface', 'table', 'treemap',
'violin', 'volume', 'waterfall']
- All remaining properties are passed to the constructor of
the specified trace type
(e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])
我试着单独绘制烛台图,效果很好,所以我很漂亮它与行和列部分有关,只是不确定接下来要搜索什么。
我使用以下资源编写此代码: https ://plotly.com/python/table-subplots/ https://plotly.com/python/candlestick-charts/
解决方案
问题是您如何添加第一个跟踪。您正在使用:
fig.add_trace(go.Figure( data = [go.Candlestick( x = df.index,...
但应该是:
fig.add_trace(go.Candlestick(x = df.index,...
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
N=14
df = pd.DataFrame({'Open': np.random.randint(1,29,N),
'Close': np.random.randint(1,29,N),
'Low': np.random.randint(1,29,N),
'High': np.random.randint(1,29,N),
'Action': np.random.choice(['sell', 'buy'],N),
'System Quality Number': np.random.randint(1,29,N)})
def chart_strategy(df):
fig = make_subplots(rows=3, cols=1)
fig.add_trace(go.Candlestick(x = df.index,
open = df['Open'],
close = df['Close'],
low = df['Low'],
high = df['High']),
row = 1, col = 1)
fig.add_trace(go.Scatter(x = df.index, y = df['System Quality Number']),
row = 2, col = 1)
fig.add_trace(go.Scatter(x = df.index, y = df['Action']), row = 3, col =1)
fig.update_xaxes(row=1, col=1, rangeslider_thickness=0.05)
fig.update_layout(width=900, height=900)
return fig
chart_strategy(df)
推荐阅读
- c - 真的很困惑像 fgets/scanf 这样的输入函数来分配一个结构指针到一个值
- python - 将具有两个参数的函数转换为只有一个参数
- javascript - 用于展平嵌套数组并跟踪所有父节点的递归函数(Javascript)
- python - 对列进行分组以获取其计数,同时保持所有其他列不变
- python - 如何防止 PysimpleGUI 关闭窗口
- css - 如何在 Angular scss 样式表中使用 ::before?
- c++ - 为什么嵌套的while循环在第二次尝试c ++中不起作用?
- php - 从基于 FK 的类的 PDO 类中获取
- python - 如何在python中将文档术语矩阵转换为tf-idf矩阵
- r - 如何在R中找到关于向量化变量的导数