dingaro
dingaro

Reputation: 2342

Disappearing Plotly plots in Jupyter Notebook?

I created some plots in Jupyter Notebook using Plotly in Python, unfortunately, every time I open Jupyter Notebook I have to reload data to be able to see these plots in Plotly, why is this happening and if I can somehow make the plots generate themselves every time I run Jupyter Notebook ? Please give me some advise it is really huge problem for me.

For instance something like this code I have to reload dataset to display it again in Jupyter Notebook when I open it:

#Size of the plot
figsize=(10,5)

#Count of values of good and bad credits in the dataset
goodCount = data[data["Risk"]== 'good']["Risk"].value_counts().values
badCount = data[data["Risk"]== 'bad']["Risk"].value_counts().values

#Bar fo good credit
trace0 = go.Bar(x = data[data["Risk"]== 'good']["Risk"].value_counts().index.values,
               y = data[data["Risk"]== 'good']["Risk"].value_counts().values,
               name='Good credit',
               text= goodCount, 
               textposition="auto", 
               marker = dict(color = "green", line=dict(color="black", width=1),),opacity=1)

#Bar of bad credit
trace1 = go.Bar(x = data[data["Risk"]== 'bad']["Risk"].value_counts().index.values,
               y = data[data["Risk"]== 'bad']["Risk"].value_counts().values,
               name='Bad credit', 
               text= badCount, 
               textposition="auto", 
               marker = dict(color = "red", line=dict(color="black", width=1),),opacity=1)

#Creation of bar plot 
data = [trace0, trace1]
layout = go.Layout()
layout = go.Layout(yaxis=dict(title='Count'),
                   xaxis=dict(title='Risk variable'),
                   title='Distribution of target variable in the dataset')


fig = go.Figure(data=data, layout=layout)
fig.show()

Upvotes: 9

Views: 7686

Answers (4)

MattheW
MattheW

Reputation: 1

Had the same problem with disappearing plotly plots in a large jupyter notebook containing a bunch of plots using plotly as backend in pandas dataframe plot method. All mentioned advices did not succeed. Finally I executed all cells in one run again and voilà all plots were shown after reopening the notebook.

Upvotes: 0

Hamzah Al-Qadasi
Hamzah Al-Qadasi

Reputation: 9786

I can add to the answer of @nicolaskruchten:

The reason as documented here:

Note: Default renderers persist for the duration of a single session, but they do not persist across sessions. If you are working in an IPython kernel, this means that default renderers will persist for the life of the kernel, but they will not persist across kernel restarts.

As indicated in the troubleshooting documentation, "JupyterLab Problems" section, You have two options to solve the problem:

  1. calling fig.show("notebook") instead of just fig.show(). (the easiest)
  2. If this problem is recurrent, you can use this:
import plotly.io as pio
pio.renderers.default='notebook'

Upvotes: 2

Ilyas Moutawwakil
Ilyas Moutawwakil

Reputation: 109

Actually I had the same problem. You should make sure your notebook is marked as "trusted" when saving it, you should see a small box either in the top-right corner of the notebook, or in the File menu. If it's not marked as such, just click on it (make sure it is trusted because whatever code that's supposed to run on notebook openning, such as plotly figures, will be executed).

Upvotes: 2

nicolaskruchten
nicolaskruchten

Reputation: 27370

The troubleshooting guide suggests either running a "restart and clear output" menu command, or executing this block at any time in any cell to restore the figures if things get out of sync:

import plotly.io as pio
pio.renderers.default='notebook'

What you're seeing is a limitation of the Plotly Notebook integration, and is much better in JupyterLab if you can upgrade to that :)

Upvotes: 8

Related Questions