Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I don't understand why this snippet of code does not work. The data is fictional, I just want to be able to make time-series visualization with plotly.

This module once worked for me in a Kaggle kernel :

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

Curiously, I am not able to make it work again. It says « AttributeError: module 'plotly' has no attribute 'plotly' ».

Any tips ?

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')

It gives me the following error :

---------------------------------------------------------------------------
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'
like image 591
asl Avatar asked Jul 09 '18 17:07

asl


2 Answers

Try import plotly.plotly.

I recommend giving it an alias like import plotly.plotly as plt.

Essentially in plotly.plotly, the first one is calling the plotly package and the second one after . is calling the function plotly from the package.

Edit: Please do let me know, if it worked for you or not.

like image 105
ColdMeteor Avatar answered Oct 21 '22 02:10

ColdMeteor


import plotly.express as px

It's the latest update.

like image 30
sargupta Avatar answered Oct 21 '22 03:10

sargupta