AttributeError: module 'plotly' has no attribute 'plotly'
Asked Answered
F

5

6

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'
Funkhouser answered 9/7, 2018 at 17:38 Comment(0)
C
6

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.

Clavate answered 9/7, 2018 at 17:41 Comment(3)
Better yet, from plotly import plotly.Chesnut
It wasn't working for me until I realized that I had to restart the kernel. Thank you!!Funkhouser
Similar issue hier: AttributeError: module 'plotly' has no attribute 'subplots'. After implementing your solution: import plotly.subplots It works! Thank you.Musaceous
P
1
import plotly.express as px

It's the latest update.

Palestine answered 9/5, 2020 at 9:40 Comment(0)
C
0

I had to first install plotly using

pip install plotly==5.10.0 in the command prompt

Restart Spyder after installation of plotly5.10.0

Then,

import plotly.express as plt

Clavicorn answered 13/9, 2022 at 7:22 Comment(0)
C
-1

Firstly I suggest to install chart_studio(if you are using anaconda install in conda promt)

pip install chart-studio

Then in your jupyter notebook import plotly with the following way: import chart_studio.plotly as py

Craiova answered 3/8, 2020 at 13:52 Comment(0)
N
-3

Starting in plotly version 4.0, all Chart Studio related functionality has been moved to the chart_studio library.

To install Chart Studio's python package, use:

pip install chart_studio

Set your credentials as below.
First:

import chart_studio

Finally:

chart_studio.tools.set_credentials_file(
    username='xxxxxxxxx',
    api_key='xxxxxxxx'
)
Neath answered 3/4, 2022 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.