pandas 绘制带有通过循环附加跟踪的图表时无效的“figure_or_data”参数 - plotly
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40658969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid 'figure_or_data' argument when plotting a chart with traces appended through loops - plotly
提问by Andreuccio
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?
知道为什么吗?
回答by Julien Marrec
Just replace data.append([trace])
by data.append(trace)
.
只需替换data.append([trace])
为data.append(trace)
.
What you end up passing right now is [[trace1],[trace2],...[trace_n]]
instead of [trace1, trace2,...,trace_n]
你现在最终通过的是[[trace1],[trace2],...[trace_n]]
而不是[trace1, trace2,...,trace_n]
Site note: you might want to look at cufflinkstoo. This would make this very easy:
网站说明:您可能也想看看袖扣。这将使这变得非常容易:
import cufflinks as cf
df.iplot(layout=layout)