AttributeError:模块“ plotly”没有属性“ plotly”

时间:2018-07-09 17:38:20

标签: python time-series visualization plotly

我不明白为什么这段代码不起作用。数据是虚构的,我只希望能够使用plotly进行时间序列可视化。

该模块曾经在Kaggle内核中对我有用:

https://www.kaggle.com/aubertsigouin/organizing-macrohistorical-datasets/data

奇怪的是,我无法使其再次运行。它说:«AttributeError:模块'plotly'没有属性'plotly'»。

任何提示吗?

import plotly
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import init_notebook_mode, plot, iplot
init_notebook_mode()
import pandas as pd
import numpy as np
data = []
array_of_time = pd.to_datetime(np.arange('2013-01-01', '2013-03-01', dtype='datetime64[M]'))

raw_data = [[20,29], [30,33]]

trace_1 = go.Scatter(
    x=array_of_time, 
    y=raw_data[0],
    name = 'data_1',
    line = dict(color = '#aeb9ba'),
    opacity = 0.8
)

trace_2 = go.Scatter(
    x=array_of_time, 
    y=raw_data[1],
    name = 'data_2',
    line = dict(color = '#ffd800'),
    opacity = 0.8
)


data.append(trace_1)
data.append(trace_2)

layout = dict(
    title = 'Exemple de titre',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(
                    count=1,
                    label='1m',
                    step='month',
                    stepmode='backward'
                ),
                dict(
                    count=6,
                    label='6m',
                    step='month',
                    stepmode='backward'
                ),
                dict(
                    step='all'
                )
            ]
            )
        ),
        rangeslider=dict(),
        type='date'
    )
)

fig = dict(data=data, layout=layout)
iplot(fig, filename = 'graph_1.html')

它给了我以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-59-81b77c8cb926> in <module>()
     53 
     54 fig = dict(data=data, layout=layout)
---> 55 iplot(fig, filename = 'graph_1.html')

~\Anaconda3\lib\site-packages\plotly\offline\offline.py in iplot(figure_or_data, show_link, link_text, validate, image, filename, image_width, image_height, config)
    342                                      cls=plotly.utils.PlotlyJSONEncoder))
    343 
--> 344     fig = {'data': data, 'layout': layout}
    345     if frames:
    346         fig['frames'] = frames

~\Anaconda3\lib\site-packages\plotly\offline\offline.py in _plot_html(figure_or_data, config, validate, default_width, default_height, global_requirejs)
    206         'plotGlPixelRatio',
    207         'setBackground',
--> 208         'topojsonURL'
    209     )
    210 

AttributeError: module 'plotly' has no attribute 'plotly'

3 个答案:

答案 0 :(得分:3)

尝试import plotly.plotly

我建议给它一个别名,例如import plotly.plotly as plt

本质上,在plotly.plotly中,第一个正在调用plotly程序包,第二个在.之后正在从程序包中调用函数。

编辑:不管是否对您有用,请告诉我。

答案 1 :(得分:1)

首先我建议安装chart_studio(如果您在conda promt中使用anaconda安装)

pip install chart-studio

然后使用以下方法在jupyter笔记本中导入: import chart_studio.plotly as py

答案 2 :(得分:0)

import plotly.express as px

这是最新更新。