6
votes

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()
2
Please show us your current code! - megargayu
done :))))))))) - pablo1212
Plotly runs active JavaScript but that is blocked upon reopening the notebook for security reasons. View the saved notebook with nbviewer or save an image and load that. For the record, duplicate question here and probably elsewhere. - Wayne
how to use nbviewer ? - pablo1212

2 Answers

5
votes

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

1
votes

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