3

In jupyter notebook, My code do run.. just not showing graph.

I have tried many way..

My example code, picture.

My Code & picture

import plotly.offline as pyo
import plotly.graph_objs as go
pyo.init_notebook_mode()

trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[5, 8, 2, 7]
)
trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[1, 5, 13, 2]
)
data = [trace0, trace1]
pyo.iplot(data, filename = 'ex')

enter image description here

And, I click a button; Download plot as png...

downloaded newplot.svg; open a file...

explorer

Capture newplot.svg

enter image description here

In new tap explorer, This graph show well.

why in jupyter notebook poltly not showing graph?

Just in case version. Add my python package version;

  • Python 3.7.4
  • plotly 4.5.2
  • numpy 1.16.6
  • seaborn 0.9.0
  • pandas 0.25.3
RainOver
  • 25
  • 1
  • 1
  • 2
  • Are you using JupyterLab or the classic notebook interface? Have you tried adding `%matplotlib inline` as a line near top of your cell? Or `%matplotlib notebook`? Or `%matplotlib widget`? Have you tried just re-running the cell after running it a first time? Your code works in the typical/classic Jupyter notebook interface where I have `%matplotlib inline` and a working plotly plot [here](https://gist.github.com/fomightez/7c7e4ba777f5084b334c4407453e8dfb) in an earlier cell. In JupyterLab where I haven't added the specific JupyterLab plotly extension, I see white. – Wayne Feb 26 '20 at 16:14
  • Yes, My jupyter notebook include %matplotlib inline. Does Jupyter notebook has nothing to do with Jupyter Lab? hmm.. Jupyter Lab is not install my Anaconda Navigator. And, I also %matplotlib notebook and %matplot widget. Likewise, not showing graph.. – RainOver Mar 02 '20 at 08:21
  • JupyterLab is related to the classic notebook interface in that it is the fancier, more full-feature generation of the software. (And you can interchange between both from JupyterLab). However, good to eliminate it as the cause. I asked because JupyterLab has been evolving rapidly lately and not all of the more complex issues such as plotting work on all versions all the time. In other words, if you had said you were using JupyterLab, I would have suggested switching the interfaces by editing the end of the URL. – Wayne Mar 02 '20 at 15:49
  • Now that we established you have tried `%matplotlib inline`. Are you able to try your code (or toy version) of your code elsewhere? I have a notebook posted [here](https://gist.github.com/fomightez/7c7e4ba777f5084b334c4407453e8dfb) with directions on where it can be run actively on the third line below title. You can do what that suggests, especially about changing the URL because it defaults to JupyterLab, & then run the middle of the notebook, specifically where you see `%pip install plotly` & the cell below it, to see plotly run & work. Then you can try your data & code, or variations. – Wayne Mar 02 '20 at 15:57
  • That test will tell you if it is your code or your environment/Jupyter version. By the way, you never listed your Jupyter version when you posted version numbers. – Wayne Mar 02 '20 at 15:59
  • %pip install plotly, my package all satisfied. plotly, six, retrying. hmm. I tried running your linked site. But there seems to be no problem. My Jupyter notebook version is 6.0.3. um. Maybe.. My notebook cell don't drag and drop. This problem relation notebook version..? – RainOver Mar 03 '20 at 06:19
  • 1
    Thank you sir. I tried several method. Jupyter notebook, Default program Explorer -> Chrome. Plotly graph well showing. and Drag and drop well working. Thank you Wayne. – RainOver Mar 03 '20 at 06:41
  • So is yours working? I had a hard time following that last comment. – Wayne Mar 03 '20 at 15:14
  • I have experienced (today) where plotly does not render in a Jupyter Notebook cell. Interestingly, it _had_ rendered earlier today. The only way to correct was to restart the kernel. Irritating but sometimes effective. – MikeB2019x Dec 14 '20 at 16:00

4 Answers4

3

please refer to plotly's getting started documentation...

For use in the classic Jupyter Notebook, install the notebook and ipywidgets packages using pip...

$ pip install "notebook>=5.3" "ipywidgets>=7.2"

For use in JupyterLab, install the jupyterlab and ipywidgets packages using pip...

$ pip install jupyterlab "ipywidgets>=7.5"

Then run the following commands to install the required JupyterLab extensions (note that this will require node to be installed):

JupyterLab renderer support

jupyter labextension install jupyterlab-plotly@4.13.0

OPTIONAL: Jupyter widgets extension

jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget@4.13.0

Arun Kumar Khattri
  • 1,519
  • 15
  • 25
0
# Import the necessaries libraries
import plotly.offline as pyo
import plotly.graph_objs as go
# Set notebook mode to work in offline
pyo.init_notebook_mode()
# Create traces
trace0 = go.Bar(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)
trace1 = go.Bar(
    x=[1, 2, 3, 4],
    y=[16, 5, 11, 9]
)
# Fill out data with our traces
data = [trace0, trace1]
# Plot it and save as basic-line.html
pyo.plot(data, filename = 'Test-bar.html', auto_open = False)

# Show HTML
from IPython.display import HTML
HTML(filename='Test-bar.html')
Ksam
  • 11
  • 1
0

If there's also not useful after install some packages, maybe you can try to restart the jupyter notebook, it works on to me:)

Josh P
  • 1
-3

My method is Jupyter notebook Default program (Exploere -> Chrome)

RainOver
  • 25
  • 1
  • 1
  • 2