Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 741
dingaro Avatar asked Mar 09 '20 19:03

dingaro


People also ask

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

Why plotly is not working?

If you are encountering problems using plotly with Dash please first ensure that you have upgraded dash to the latest version, which will automatically upgrade dash-core-components to the latest version, ensuring that Dash is using an up-to-date version of the Plotly. js rendering engine for plotly .

How do you display plots in Jupyter notebook?

IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.


2 Answers

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

like image 66
nicolaskruchten Avatar answered Oct 05 '22 20:10

nicolaskruchten


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

like image 39
Ilyas Moutawwakil Avatar answered Oct 05 '22 19:10

Ilyas Moutawwakil