gpt4 book ai didi

python - Plotly:如何向烛台图表添加交易量

转载 作者:行者123 更新时间:2023-12-03 15:15:36 25 4
gpt4 key购买 nike

代码:

from plotly.offline import init_notebook_mode, iplot, iplot_mpl

def plot_train_test(train, test, date_split):
data = [Candlestick(x=train.index, open=train['open'], high=train['high'], low=train['low'], close=train['close'],name='train'),
Candlestick(x=test.index, open=test['open'], high=test['high'], low=test['low'], close=test['close'],name='test')
]
layout = {
'shapes': [
{'x0': date_split, 'x1': date_split, 'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
'line': {'color': 'rgb(0,0,0)', 'width': 1}}],
'annotations': [{'x': date_split, 'y': 1.0, 'xref': 'x', 'yref': 'paper', 'showarrow': False, 'xanchor': 'left','text': ' test data'},
{'x': date_split, 'y': 1.0, 'xref': 'x', 'yref': 'paper', 'showarrow': False, 'xanchor': 'right', 'text': 'train data '}] }
figure = Figure(data=data, layout=layout)
iplot(figure)
上面的代码没问题。但现在我想在这个烛台图表中“成交量”
代码:
from plotly.offline import init_notebook_mode, iplot, iplot_mpl

def plot_train_test(train, test, date_split):
data = [Candlestick(x=train.index, open=train['open'], high=train['high'], low=train['low'], close=train['close'],volume=train['volume'],name='train'),
Candlestick(x=test.index, open=test['open'], high=test['high'], low=test['low'],close=test['close'],volume=test['volume'],name='test')]
layout = {
'shapes': [
{'x0': date_split, 'x1': date_split, 'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
'line': {'color': 'rgb(0,0,0)', 'width': 1}}
],
'annotations': [
{'x': date_split, 'y': 1.0, 'xref': 'x', 'yref': 'paper', 'showarrow': False, 'xanchor': 'left',
'text': ' test data'},
{'x': date_split, 'y': 1.0, 'xref': 'x', 'yref': 'paper', 'showarrow': False, 'xanchor': 'right',
'text': 'train data '}
]
}
figure = Figure(data=data, layout=layout)
iplot(figure)
错误:

ValueError: Invalid property specified for object of typeplotly.graph_objs.Candlestick: 'volume'

最佳答案

您尚未提供带有数据示例的完整代码片段,因此我将不得不提出一个基于示例的解决方案 here .
在任何情况下,您都会收到该错误消息只是因为 go.Candlestick没有 Volume属性。乍一看似乎并非如此,但您可以轻松设置 go.Candlestick作为个体跟踪,然后包含个体 go.Bar()使用以下方法跟踪卷:

  • fig = make_subplots(specs=[[{"secondary_y": True}]])
  • fig.add_traces(go.Candlestick(...), secondary_y=True)
  • fig.add_traces(go.Bar(...), secondary_y=False)

  • 阴谋:
    enter image description here
    完整代码:
    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')

    # Create figure with secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    # include candlestick with rangeselector
    fig.add_trace(go.Candlestick(x=df['Date'],
    open=df['AAPL.Open'], high=df['AAPL.High'],
    low=df['AAPL.Low'], close=df['AAPL.Close']),
    secondary_y=True)

    # include a go.Bar trace for volumes
    fig.add_trace(go.Bar(x=df['Date'], y=df['AAPL.Volume']),
    secondary_y=False)

    fig.layout.yaxis2.showgrid=False
    fig.show()

    关于python - Plotly:如何向烛台图表添加交易量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64689342/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com