Getting plotly.graph_objs attribute error for FigureWidget - python

I get the following error in Jupyter Notebook:
AttributeError: module 'plotly.graph_objs' has no attribute 'FigureWidget'
I have verified that Plotly package is installed as is ipywidgets, Flask and Dash.
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from plotly import figure_factory as FF
init_notebook_mode(connected=True)
Not sure what I need to install or update to get this to work.

For me even when plotly was upgraded ipywidget wasn't.
executing pip3 install ipywidgets --upgrade
make sure you enable these extensions on this notebook:
jupyter nbextension enable --py widgetsnbextension --sys-prefix
jupyter nbextension enable --py plotlywidget --sys-prefix

Related

Module plotly.offline not found

I get this error importing in a Python virtual environment,
import plotly.offline
ModuleNotFoundError: No module named 'plotly.offline'
I installed plotly using pip install plotly. Tried to uninstall and reinstall. But still same eror. I'm using Python3 in the venv. I can import plotly with import plotly but none of its modules I can't.
But when I tried a global install (not using a venv) I am able to import plotly.offline.

Pandas Plots and Google Colab

I am trying to use plotly as backend in Google Colab to plot from Pandas.
import pandas as pd
!pip install plotly==4.14.1
df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1]))
df.plot(backend='plotly')
fig= df.plot()
fig.show()
When I execute the code above I get the error:
ValueError: Could not find plotting backend 'plotly'. Ensure that you've installed the package providing the 'plotly' entrypoint, or that the package has a top-level `.plot` method.
I am crearly using !pip to install plotly, So I don't know how to fix it. Any help on how to fix this is appreciated
You can try this
!pip uninstall -y -q plotly; pip install -q plotly
!pip uninstall -y -q pandas; pip install -q pandas
import pandas as pd
import plotly
pd.set_option('plotting.backend','plotly')
print(pd.__version__, plotly.__version__) # pd 1.2.5 and plotly 4.4.1
def enable_plotly_in_cell():
''' Use this function in each cell to show plot in Google Colaboratory.'''
import IPython
from plotly.offline import init_notebook_mode
display(IPython.core.display.HTML('''<script src="/static/components/requirejs/require.js"></script>'''))
init_notebook_mode(connected=False)
get_ipython().events.register('pre_run_cell', enable_plotly_in_cell)
df = pd.DataFrame({'a':[10,20,30]})
fig = df.plot()
fig.show()

Plotly Graphs not showing in Jupyterlab despite installed extensions

I have a weird problem. Jupyterlab doesn't show plotly graphs, but in Jupyter notebook, everything works fine.
I've searched everywhere, many said plotly-extension should be installed. I have installed plotly extension. The notebook I'm working on is trusted. I don't know what should I do.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(y=[2, 1, 4, 3]))
fig.add_trace(go.Bar(y=[1, 4, 3, 2]))
fig.update_layout(title = 'Hello Figure')
fig.show()
This is what jupyterlab shows me
Just had the same problem and, as I am using Anaconda, in the "Server" environment, apart from installing Plotly, I installed the following JupyterLab extension(s):
jupyterlab-plotly
jupyter labextension install jupyterlab-plotly#4.14.1
plotlywidget [optional]
jupyter labextension install #jupyter-widgets/jupyterlab-manager plotlywidget#4.14.1
In order to install the extensions nodejs is required.
For a more detailed answer check my answer here.
Consider adding the following lines of code in your notebook:
from plotly.offline import init_notebook_mode
init_notebook_mode(connected=True)
plotly.offline.init_notebook_mode(connected=True)

How to resolve import cufflinks error "The plotly.plotly module is deprecated"?

On importing cufflinks I am getting error in Jupyter Notebook:
import cufflinks as cf
error:
The plotly.plotly module is deprecated,
please install the chart-studio package and use the
chart_studio.plotly module instead.
What to do? Is there any other way I can plot my data frames using plotly?
This is because you have to use the chart_studio.plotly module instead.
Install chart-studio package pip install chart_studio
Use the chart_studio.plotly module instead of plotly.plotly
Replace Code: import plotly.plotly as py
By: import chart_studio.plotly as py

How to install 'plotly' from inside Anaconda under Colab on Mac

I tried to import plotly in Anaconda with code
import plotly.graph_objs as go
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
but received error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-11-978bd9a5088a> in <module>
14
15 # import plotly.plotly as py
---> 16 import plotly.graph_objs as go
17 from plotly import __version__
18 from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
ModuleNotFoundError: No module named 'plotly'
I have checked a number of similar posts to install 'plotly' on Jupyter but somehow I couldn't do it on Mac. I usually use Google Colab, could it be the reason that I am using different platforms that caused the confusion when installing?
Jupyter Notebooks start in an "environment". Anaconda created the environment and it contains a lot of useful libraries. It also holds a self-contained python interpreter. In this instance, the environment didn't have the library plotly installed (it's not a default library that Anaconda provides) so you had to install plotly in your environment that the notebook lives in.
The environments that are used with Jupyter Notebooks are a little tricky to get to in order to install things, so this way, using import sys then installing the library with !{sys.executable} -m pip install plotly finds the python interpreter with !{sys.executable} and installs plotly using pip right in the Notebook itself.
More reading:
Environments
Pip
Packages included with Anaconda
try:
import sys
!{sys.executable} -m pip install plotly
import plotly.graph_objs as go
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

Categories