Andreuccio
Andreuccio

Reputation: 1083

Invalid 'figure_or_data' argument when plotting a chart with traces appended through loops - plotly

I put together a code that can be inserted into a loop, in order to plot a line chart, appending a trace for the values of each column in a dataframe. I have been struggling with this for a while, and I cannot quite figure out how keys in plotly figures and layout objects should be called.

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)

Error I get is:

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.

Any idea why?

Upvotes: 0

Views: 6765

Answers (1)

Julien Marrec
Julien Marrec

Reputation: 11895

Just replace data.append([trace]) by data.append(trace).

What you end up passing right now is [[trace1],[trace2],...[trace_n]]instead of [trace1, trace2,...,trace_n]


Site note: you might want to look at cufflinks too. This would make this very easy:

import cufflinks as cf

df.iplot(layout=layout)

Upvotes: 1

Related Questions