绘制带有通过循环追加的痕迹的图表时,无效的'figure_or_data'参数 - 图示

时间:2016-11-17 15:35:14

标签: python pandas charts plotly

我将可插入循环的代码放在一起,以绘制折线图,​​为数据框中每列的值附加跟踪。 我一直在努力解决这个问题,我无法弄清楚如何调用图形图和布局对象中的键。

import pandas as pd
import plotly.plotly as py
from plotly import tools, utils
from plotly.graph_objs import *


d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
'three' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd','e'])}
df = pd.DataFrame(d)

print(df.head())

data = []

for column in df:

    trace = Scatter(
    x = df.index, 
    y = df[column],
    mode = 'lines',
    line = Line(
        color='#FFD700',
        width=1
        ),
    name = column
    )
    data.append([trace])


layout = dict(
    title= 'Room Ambient Temperatures (with range slider and selectors)',

    yaxis=dict(
        range=[20,30],
        showgrid=True,
        zeroline=True,
        showline=False,
    ),

)
fig = dict(data=data, layout=layout)    #fig = Figure(data=data, layout=layout)    

chart_url = py.plot(fig, filename = 'test/_T_Line_Chart', auto_open=False)

我得到的错误是:

Invalid entry found in 'data' at index, '0'
Path To Error: ['data'][0]
Valid items for 'data' at path ['data'] under parents ['figure']:
    ['Contour', 'Box', 'Scatter3d', 'Bar', 'Mesh3d', 'Histogram', 'Surface',
    'Pie', 'Histogram2d', 'Scattergeo', 'Scattergl', 'Scattermapbox',
    'Scatterternary', 'Candlestick', 'Ohlc', 'Scatter', 'Area',
    'Histogram2dcontour', 'Choropleth', 'Heatmap', 'Pointcloud']
Entry should subclass dict.

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

只需将data.append([trace])替换为data.append(trace)

您最终传递的内容是[[trace1],[trace2],...[trace_n]]而不是[trace1, trace2,...,trace_n]

网站说明:您也可以查看cufflinks。这将使这很容易:

import cufflinks as cf

df.iplot(layout=layout)