为什么plotly.offline.iplot({'x':[1,2,3],'y':[5,2,7]})不起作用?

时间:2015-12-09 23:31:59

标签: python plotly

全新(今天)到plotly(Python)并逐步完成示例和概述。

plotly.offline.iplot的方法签名陈述:

figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or
                  dict or list that describes a Plotly graph.
                  See https://plot.ly/python/ for examples of
                  graph descriptions.

那么为什么它不接受标题中的字典?

PlotlyError                               Traceback (most recent call last)
<ipython-input-34-dc2bdcac2527> in <module>()
----> 1 plotly.offline.iplot({'x': [1, 2, 3], 'y': [5, 2, 7]})

/Users/Pyderman/anaconda/lib/python2.7/site-packages/plotly/offline/offline.pyc in iplot(figure_or_data, show_link, link_text, validate)
    104 
    105     from IPython.display import HTML, display
--> 106     figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
    107 
    108     width = figure.get('layout', {}).get('width', '100%')

/Users/Pyderman/anaconda/lib/python2.7/site-packages/plotly/tools.pyc in return_figure_from_figure_or_data(figure_or_data, validate_figure)
   1412                                          "plot option.\nHere's why you're "
   1413                                          "seeing this error:\n\n{0}"
-> 1414                                          "".format(err))
   1415         if not figure['data']:
   1416             raise exceptions.PlotlyEmptyDataError(

PlotlyError: Invalid 'figure_or_data' argument. Plotly will not be able to properly parse the resulting JSON. If you want to send this 'figure_or_data' to Plotly anyway (not recommended), you can set 'validate=False' as a plot option.
Here's why you're seeing this error:

'y' is not allowed in 'figure'

Path To Error: ['y']

Valid attributes for 'figure' at path [] under parents []:

    ['layout', 'data']

Run `<figure-object>.help('attribute')` on any of the above.
'<figure-object>' is the object at []

为什么字典必须包含在列表中:

plotly.offline.iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])

1 个答案:

答案 0 :(得分:0)

如果你提供了一个词典,它会假定你正在加入一个Figure,如果你列入一个列表,它会假定你正在放入DataData是一个列表,因为您可以在一个图中包含多个序列。 E.g。

提供数据(注意,两行):

plotly.offline.iplot([
  {'x': [1, 2, 3], 'y': [5, 2, 7]},
  {'x': [1, 2, 3], 'y': [1, 2, 3])}])

等效,但提供图代替:

plotly.offline.iplot({
  'data': [
    {'x': [1, 2, 3], 'y': [5, 2, 7]},
    {'x': [1, 2, 3], 'y': [1, 2, 3])}]
  #Could put layout, etc... here
  })