PLOTLY TypeError:绘制2个图表时'NoneType'对象不可下标

时间:2019-01-26 18:36:19

标签: python pandas plotly

我有一张苹果股价的图表。我想在图表底部添加一个条形图以显示一些计算结果(类似于体积条形图)。这两个图必须在同一张图上。

如果我运行我的代码,错误是

Traceback (most recent call last):
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-54-c742720836cd>", line 33, in <module>
    fig_apple.append_trace(trace1,2,1)
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/plotly/basedatatypes.py", line 1225, in append_trace
    self.add_trace(trace=trace, row=row, col=col)
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/plotly/basedatatypes.py", line 1072, in add_trace
    cols=[col] if col is not None else None
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/plotly/basedatatypes.py", line 1155, in add_traces
    self._set_trace_grid_position(trace, row, col)
  File "/anaconda3/envs/py35thesis/lib/python3.5/site-packages/plotly/basedatatypes.py", line 1242, in _set_trace_grid_position
    ref = grid_ref[row - 1][col - 1]
TypeError: 'NoneType' object is not subscriptable

我的代码:

import plotly as py
from plotly.tools import FigureFactory as FF
from datetime import datetime


df = web.DataReader("aapl", 'yahoo', datetime(2010, 10, 1), datetime(2019, 1, 15))
fig_apple = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)

df = pd.read_csv('results_data_bar.csv', encoding="ISO-8859-1")

import plotly.graph_objs as go

trace1 = go.Bar(
        x=df['Date'],
        y=df['count_sentiment_svm'],
        marker=dict(
            color='rgb(0,100,0)',
            line=dict(
                color='rgb(100,100,100)',
                width=1.5,
            )
        )

    )
trace2 = go.Bar(
        x=df['Date'],
        y=df['count_sentiment_svm_neg'],
        marker=dict(
            color='rgb(100,0,0)',
            line=dict(
                color='rgb(100,100,100)',
                width=1.5,
            )
        ))


layout = go.Layout(
    barmode='stack',
    title='Sentiment'
)

fig_apple.append_trace(trace1,1,1)
fig_apple.append_trace(trace2,2,1)
py.offline.plot(fig_apple, validate=False)

0 个答案:

没有答案