Plotly charts not showing when sharing jupyter notebook in IBM Cloud Pak for data - plotly-python

Here is the notebook: https://dataplatform.cloud.ibm.com/analytics/notebooks/v2/1c96e62e-553b-4f04-b851-95c5621a3483/view?access_token=d92c1c0fe2fab7ee9647499a5280a6faa8e77f4fe244bdf8ad10cc5be4efb361
I import the necessary libraries, namely:
!pip install plotly #==4.9.0 # map rendering for nicer looking maps
import plotly.express as px # map rendering for bubble plot
import plotly.offline as pyo # map rendering for offline viewing
from plotly.offline import init_notebook_mode, iplot
Then I define the plots ( at the bottom of the notebook ):
Then I attempt to render them for sharing purposes:
pyo.iplot(figa)
pyo.iplot(figb)
What am I missing? Can a shared notebook show the

The shared notebook will need to be marked as "Trusted" or the output will not be displayed.
Per Documentation here
"Trusting Notebooks
To prevent untrusted code from executing on users’ behalf when notebooks open, we store a signature of each trusted notebook. The notebook server verifies this signature when a notebook is opened. If no matching signature is found, Javascript and HTML output will not be displayed until they are regenerated by re-executing the cells."

Related

Jupyterlab kernel remains busy with matplotlib widget

I'm new to Jupyterlab and matplotlib so this may be a dumb question but here goes.
FYI: I'm using the standalone jupyterlab app on a mac.
When I make a simple interactive plot using the matplotlib widget magic, the kernel stays busy forever. I had found an earlier post on stack overflow about putting the magic in the 2nd cell (why this works?) and the plot works fine, I just want to know what's going on with the kernel.
The code, which works fine, is just:
import numpy as np
%matplotlib widget
import matplotlib.pyplot as plt
x = np.arange(10)
y = 2*x
plt.figure()
plt.scatter(x,y)
A screenshot to illustrate.
Kernel stuck running in top right of shot
UPDATE: To be clear, this is occurring on the Jupyterlab Standalone app. If I run this on jupyterlab launched via the anaconda navigator, it works fine. For whatever reason, it seems confined to the standalone app only.
As for the App, I installed it via the binary at GitHub. I'm running 3.3.2-2

Generating matplotlib graph which is refreshed real-time on web browser using mpld3

I have realized the dynamic graph drawing with matplotlib, and it can refresh graph real-time. Now, I want to generate matplot figure on Web Browers using mpld3. It can export to webpage, but can only run one time and graph won't be refreshed continuously. Wondering what this may be caused by? My script frame is as follow:
import mpld3[enter image description here][1]
from mpld3._server import serve
.
.
.
html1=mpld3.fig_to_html(fig1)
html2=mpld3.fig_to_html(fig2)
serve(html1+html2)
plt.pause(0.5)
enter image description here
There is no way to do that.
However, you can still export your figure as a html file – as you already do with the mpld3.fig_to_html() command – but always in the same html file, and run a auto-reload soft in the web browser on this page (see for example this plugin for Firefox: https://addons.mozilla.org/en-US/firefox/addon/tab-auto-reload/).
But for mpld3 figures, the loading time can be quite large depending on the amount of data you are showing. This solution is definitely not appropriate for real-time applications!

pop-up plots using Python Jupyter Notebook

Is there a way to have the plots created inside Jupyter Notebook using matplotlib to appear on a separate pop-up screen that would allow you to expand/shrink the image by hand? I've tried experimenting with (%matplotlib notebook) but that didn't really do the trick.
Just wondering if this is possible.
Just use an interactive backend. This works for me:
import matplotlib.pyplot as plt
%matplotlib tk
plt.plot([1, 2])
The notebook (nbagg) backend also allows for expand/shrink by hand. It has some rough edges though.
The tkinter backend is a bit buggy (windows 10, python 3).
I used %matplotlib qt for the matplotlib plot that we are all used to.

I don't know how to make a new matplotlib figure

I'm using Jupyter notebook with %matplotlib notebook in one of the first lines.
When making multiple plots, I have to physically press the 'stop interaction' button on each figure before running another plot, or else the newest plot will be overlaid onto the previous figure.
I think the problem is that I'm not specifying that a new figure needs to be made for each plot? But I'm stumped as to how best to do that!
DO I REALLY HAVE TO SAY PLT.FIGURE EVERY SINGLE TIME? THIS SEEMS UNLIKELY TO ME...?
Thanks in advance!
This is a bug with the notebook backend, but luckily the person who reported it also reported a workaround
Within the Notebook you will need to add the plt.ioff after you import pyplot.
Here is a snip from the top of a notebook, that makes it work for me. I was getting plots over written like you.
%matplotlib notebook # this is to allow the plotting in the notebook
import numpy as np
from scipy.linalg import hadamard
import matplotlib.pyplot as plt
plt.ioff() # this stops the graphs from overwriting each other

using interactive and non-interactive backends within one program

I am running code written with PyQt4 which uses matplotlib's Qt4Agg backend for showing live plots in windows. At the same time, I would like to use matplotlib in background thread to produce (different) figures which are only saved to file, not shown on the screen.
I can use Qt4Agg in the background thread, but I am getting a bunch of
QPixmap: It is not safe to use pixmaps outside the GUI thread
warnings, and also crashes in some cases.
As far as I see, matplotlib currently supports using only one backend at any given time (which can be changed via switch_backend, but that closes all existing figures). Is there some way to work around this limitation, and to assign per-figure backend?
To my knowledge, only if you don't use the pyplot interface.
For instance, using the full OO interface for a simple plot:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
canvas.print_figure('test.png')
HTH