Precompute Jupyter widget results and embed in HTML - python

I'm currently working on a Jupyter notebook that creates an energy level diagram using matplotlib. One parameter of the underlaying spectrum computation can be varied using an interact statement. Now I want to export this notebook to HTML while the widget functionality should stay intact. Is it possible to precompute all possible outcomes and include them into the HTML version? Users should be able to use the interact feature without having to connect to a kernel.
Thanks for advice.
Peter
EDIT: I hope to clarify my question with the following sample code:
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
def f(a):
x = np.linspace(0,2,100)
plt.plot(x,a*x**2)
plt.ylim([0,20])
plt.show()
return None
interact(f, a=(1,5,1))
In the final HTML file all possible images (in this case all parabolas with a=1...5) should be included (in this sense pre-computed) and then displayed when the user selects the appropiate value with the slider.

Related

Different result for x and y limits in 2 different code cells of Google Colab

Have a look at the code in the following image:
Question:
Since I am not resetting the plt variable, how did I get different limits of X and Y dimensions in the 2 different code cells of Google Colab?
matplotlib.pyplot is not a variable, it's a module, that encloses functions from inside matplotlib for ease of use.
the behavior depends on the backend used by matplotlib, when running in an ipy notebook, matplotlib uses an inline backend.
the inline backend automatically calls plt.show() on every code block, thus releasing the last figure, and a new figure is created in each code block.

anybody know how to save image output from cells in jupyter notebook?

unlike google colab i cant right click to save the image, i use vscode on a chromebook with linux beta, it would be helpful if you provide step-by-step instruction. I am also new to code and the notebook uses python
p.s this is for dall.e flow-jupyter
On the right we have two options, first one is to expand image (or zoom) and second option is to save image.
But best option is to use code to save image instead of saving images manually.
For example, if you want save pandas graph, then
import pandas as pd
import matplotlib.pyplot as plt # pandas uses matplotlib in the backend
df = pd.DataFrame({"a": list(range(50))})
df["a"].plot()
plt.savefig("test.png")

Jupyter and %matplotlib inline lost axis

I am having a really weird issue with using the %matplotlib inline code in my jupyter notebook for plotting graphs using both pyplot and the pandas plotting function.
The problem is they show up without any axes, and basically just show the graph area without anything aside from data points.
I found adding:
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
reverse it, but I find it odd that should do that every time as the effect disappears as soon as I run %matplotlib inlinecommand.
an example could be
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(A,A)
plt.tight_layout()
plt.xlabel('here')
plt.show()
This would generate the graph below:
Weird enough if I uses the savefig it get plotted with the axis, if I uses the right-click -> new output -> save as figure, I also get the graph with the figures !!
like this:
Can anyone help me understand what is wrong, which global setting did I mess up, and how do I revert it?
(I don't remember messing around with any settings aside from some settings for pandas, but don't think they should have had an impact)
as mentioned running mpl.rcParams.update(mpl.rcParamsDefault) command does bring it back to normal until I run %matplotlib inline` again !!
Any help would be much appreciated.
Okay I am sorry I think I can answer the question myself now.
With the helpfull #Mr. T asking for the imgur link made me realize what was going on. I had starting using the dark jupyter lab theme, and the graph would generate plots with transparent background, ie. the text and lines where there, but I just couldn't see them.
The trick is to change the background color preferably globally, but that will be a task for tomorrow.

plt.show() not showing data instead holding it for next plot (spyder)

I have been using the same setup for quite some time now but suddenly I am no longer allowed to plot more than one graph in a program.
Usually I can plot multiple plots after each other and let the program run through it. It executes the next lines of code after closing the first window. However, recently the first plot is not shown but instead the data is added to the last plot.
I have included a sample code which used to give me two plots but now only one.
import matplotlib.pyplot as plt
import numpy as np
random_num = np.random.randint(0,5,10)
random_num_2 = np.random.randint(0,100,10)
plt.plot(random_num, 'ko')
plt.show()
plt.plot(random_num_2, 'g*')
plt.show()
The first image shows the output from my program. But I would like to have them separated into two plots like Figure 2 and 3 show.
Maybe I should add that I am using Python 3.6 with Spyder 3.2.4. The graphics option is set to display it in Qt5 even though I tried all settings and only 'Inline' shows me the results the way I want it.
Sorry if this is a very simple question. I have tried googling but I only come up with questions about my topic where the way mine works would be the solution not the problem.
#TheresaOtt. I would suggest you create a new figure instance (plt.figure()) for each plot and use only once at the end the plt.show() command.

Use both matplotlib inline and qt in jupyter notebook

I am using Jupyter (with IPython) to analyze research data, as well as export figures. I really like the notebook approach offered by Jupyter: when I revisit an experiment after a long time, I can easily see how the figures correspond to the data. This is of course using the inline backend.
However, when I want to explore new data, I prefer to use the QT backend. It is faster than the inline one, and allows to easily scale, zoom in and out, and nicely displays the X and Y coordinates in the bottom left corner. Moreover, I can use the QT backend to determine good x and y limits to use in the inline backend.
I have tried using the %matplotlib notebook magic, but it is simply too slow. For some experiments I am plotting ~500 spectra (each consists of ~1000 data points), which is already slow in the inline backend. Even with less data points, the notebook backend is just too slow to use.
Therefore, I would like to use both the QT backend, and the inline backend whenever I plot something. (So, whenever I execute a cell which plots data, it should both display the inline image, and pop up a QT backend window). This way, I still have a nice overview of plots in my notebook, while also allowing me to easily explore my data. Is there a way to achieve this?
As far as I know it is not possible to use more than one backend at once.
What I do is to switch backend and replot. I have done it with matplotlib.use() or %matplotlib <backend>, according to which version or environment I was working in.
I had the impression that this sometimes doesn't work or maybe it is not responsive, so I need to restart the environment. I imagine it is not easy to maintain it, so I am even surprised it works so smoothly almost always.
Available backends can be listed with:
matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends
Hopefully here there are other useful info:
https://matplotlib.org/stable/tutorials/introductory/usage.html
This allows you to run QtConsole, plotting with the plotSin function, both inline and through the QtConsole.
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
..
def plotChirp(Type, Exp, Rand):
# Orignal Chirp Funciton From:
# http://stackoverflow.com/questions/19410042/how-to-make-ipython-notebook-matplotlib-plot-inline
x = np.linspace(0, 3*np.pi, Rand)
plt.plot(x, np.sin(x**int(Exp)))
plt.title('A simple chirp ' + Type)
plt.show()
..
plotChirp("A", 5, 200) # Plots inline if you choose
%connect_info # For your own connection
%qtconsole
QtConsole opens and now you can call your function to plot externally..
Using %matplotlib qt allows for printing in a loop, but unfortunately it seems to overlap the plots. Looking into subplots as a possible solution.
%matplotlib qt
for i in range(0,2):
if i == 0:
plotChirp("B",1, 400)
else:
plotChirp("c",6, 1000)

Categories

Resources