Jupyterlab Inline Interactive plot - python

I am trying to make my inline plots in jupyterlab interactive. So far, I have tried a suggestion as pointed out here, among others:
jupyterlab interactive plot
# %matplotlib notebook - does not work : Javascript Error: IPython is not defined
# %matplotlib widget - works, but plots are overwritten
The widget magic works in making the plots interactive, but unfortunately, my plots are overwritten. Subsequent cells render plots on top of the output of cell 1 as below:
plt.scatter(trainData['x'], trainData['y'], color='Red', s=0.5)
plt.show()
plt.hist(trainData['x'])
plt.xlabel("Values of X")
plt.ylabel("Frequency")
plt.show()
plt.hist(trainData['y'])
plt.xlabel("Values of Y")
plt.ylabel("Frequency")
This does not look right, have I missed an option? I am looking for an IPython magic that can help with having several inline plots (after each cell)?
I am using jupyterlab v 0.35.4 on chrome Version 74.0.3729.169, run from Anaconda navigator on windows 10.

Related

No interactivity in plotly plots in Spyder 4

I have recently upgraded to Spyder 4.1.3 (up from Spyder 3.x) and have also consequently upgraded many packages along the way. Plots now show up in the dedicated plots pane (which is a nice addition to Spyder in general). However, now when I generate plots using plotly, there is no interactivity with the resultant plots whether inline or in the plots pane. This used to be there in Spyder 3.x. For example, running the sample code from plotly:
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(df, locations="iso_alpha",
color="continent", # which column to use to set the color of markers
hover_name="country", # column added to hover information
size="pop", # size of markers
projection="natural earth")
fig.show()
The plot shows up in the plot pane but when I hover over it with the mouse nothing pops up. When I run the same code in Jupyter I do get the interactivity. Any ideas about how to get this functionality going in Spyder 4?

Inline Interactive Plots with Julia in jupyter notebook

when I use
%matplotlib notebook
import matplotlib.pyplot as plt
I get interactive plots, i.e. I can also zoom into the figure.
For julia this command does not seem to exist. Any ideas?
You can get interactive plots by using Plotly, either directly with Plotly.jl or through its Plots backend.
Today I am thinking about how to enlarge the plot window interactively without referring to python or plotly or something else in Jupiter notebook. And I find a good solution. It is to use the package Interact. A simple example is as follows. You can get an interactive plot in 3 lines.
import Pkg; Pkg.add("Interact")
using LinearAlgebra, Plots, Interact
#manipulate for n in 10:100, w in 200:1000, h in 200:1000
plot(randn(n),randn(n),seriestype=:scatter,label="n=$(n)",size = (w, h))
end
The result looks like this

When is plt.show() required to show a plot and when is it not?

Since the following code will show a plot without plt.show(), what is the point of plt.show()?
Please tell me when plt.show() is required as this would allow me to appreciate the intricacy of matplotlib better.
N.B.: I'm using this in Spyder (Anaconda)
import matplotlib.pyplot as plt
plt.subplot(211) # the first subplot in the first figure
plt.plot([1, 2, 3])
To require or not required depending on where your script is.
There are 2 contexts.
Matplotlib is used in a terminal or scripts, plt.show() is a must.
Matplotlib is used in a IPython shell or a notebook (ex: Kaggle), plt.show() is unnecessary.
It seems either you are in an interactive mode or are using a JuPyter notebook, in both the cases plt.show() being rendered redundant (check the bold highlighted doc below)
From the official docs
Display a figure. When running in ipython with its pylab mode, display all figures and return to the ipython prompt.
In non-interactive mode, display all figures and block until the figures have been closed; in interactive mode it has no effect unless figures were created prior to a change from non-interactive to interactive mode (not recommended). In that case it displays the figures but does not block.
A single experimental keyword argument, block, may be set to True or False to override the blocking behavior described above.

matplotlib does not show plot()

I tried running plt.show() but no plot is shown. I had tried lots of solutions from stackoverflow including setting matplotlib backend to Qt4Agg, switching to other backends (i.e. TkAgg, Agg) and reinstalling matplotlib package but still not solving the issue. Some solutions that I had tried but not working are:
matplotlib does not show my drawings although I call pyplot.show()
Matplotlib.Pyplot does not show output; No Error
Why matplotlib does not plot?
Matplotlib does not show labels or numbers
Below is the code that I tried to run:
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel = "age"
plt.ylabel = "friends count"
plt.title = "Facebook Friends count by Age"
plt.show()
When plt.scatter(pf["age"], pf["friend_count"]) code was run, a scatter plot was shown but with no labels and title. Running plt.show() did not plot and no error was shown. Appreciate any help.
I installed Anaconda with Python 3 for Windows OS. My Mac laptop is running on Bootcamp.
The labels must use parentheses such as plt.xlabel("text"), not assigned to a string with = like you have in your example code. Make the changes in your code, save the changes, quit and reopen Spyder or whatever interpreter you are running, then run the code again.
plt.figure(1)
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel("age")
plt.ylabel("friends count")
plt.title("Facebook Friends count by Age")
plt.show()

Unable to specify matplotlib backend in Anaconda Spyder [duplicate]

I upgraded from Python(x,y) 2.7.2.3 to 2.7.6.0 in Windows 7 (and was happy to see that I can finally type function_name? and see the docstring in the Object Inspector again) but now the plotting doesn't work as it used to.
Previously (Spyder 2.1.9, IPython 0.10.2, matplotlib 1.2.1), when I plotted this script, for instance, it would plot the subplots side-by-side in an interactive window:
Now (Spyder 2.2.5, IPython 1.2.0, Matplotlib 1.3.1) when I try to plot things, it does the subplots as tiny inline PNGs, which is a change in IPython:
So I went into options and found this:
which seems to say that I can get the old interactive plots back, with the 4 subplots displayed side-by-side, but when I switch to "Automatic", and try to plot something, it does nothing. No plots at all.
If I switch this drop-down to Qt, or uncheck "Activate support", it only plots the first subplot, or part of it, and then stops:
How do I get the old behavior of 4 side-by-side subplots in a single figure that I can interact with?
Change the backend to automatic:
Tools > preferences > IPython console > Graphics > Graphics backend > Backend: Automatic
Then close and open Spyder.
You can quickly control this by typing built-in magic commands in Spyder's IPython console, which I find faster than picking these from the preferences menu. Changes take immediate effect, without needing to restart Spyder or the kernel.
To switch to "automatic" (i.e. interactive) plots, type:
%matplotlib auto
then if you want to switch back to "inline", type this:
%matplotlib inline
(Note: these commands don't work in non-IPython consoles)
See more background on this topic: Purpose of "%matplotlib inline"
After applying : Tools > preferences > Graphics > Backend > Automatic Just restart the kernel
And you will get Interactive Plot.
As said in the comments, the problem lies in your script. Actually, there are 2 problems:
There is a matplotlib error, I guess that you're passing an argument as None somewhere. Maybe due to the defaultdict ?
You call show() after each subplot. show() should be called once at the end of your script. The alternative is to use interactive mode, look for ion in matplotlib's documentation.
This is actually pretty easy to fix and doesn't take any coding:
1.Click on the Plots tab above the console.
2.Then at the top right corner of the plots screen click on the options button.
3.Lastly uncheck the "Mute inline plotting" button
Now re-run your script and your graphs should show up in the console.
For most mathematical coding, I use this website and their services as they offer examples for every subject and their support is super helpful:
https://labdeck.com/application-examples-screenshots/
If your want you graph to change by a variable amount then the code you want to use is
import matplotlib.pyplot as plt
import time
vec1=[1, 2, 3, 4, 5]
vec2py=[10, 12, 9, 11, 13]
plt.show()
axes = plt.gca()
axes.set_xlim(0, 6)
axes.set_ylim(5, 50)
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Example 1')
plt.grid()
line,= axes.plot(vec1,vec2py,color='red',lw=1)
for x in range(0,10):
vec2py = [x + 2 for x in vec2py]
line.set_ydata(vec2py)
plt.draw()
plt.pause(1e-17)
time.sleep(0.5)
plt.show()
You will have to change x for how many iterations of the graph you want and how long you want it to run for and also the +2 in the vec2py line for what variable amount you want to change it by. Naturally the code is a template and you can make any aesthetical changes. This code file is found under displaying dynamic graphs which is under python programming in the link above.
If you want to display a constant rely of information from a source, I'm not to sure how to do that but the website mention before does have an example, however it isn't in python but in a simplified form of C++.If you do want to see it then the link is https://labdeck.com/examples/dsp-ecg-processing/ecg-9-leads-graphs.pdf?01a96f&01a96f and its under ECG 9 Leads graphs in ECG on the link at the start.
The graph can be shown as in a document or independently from the document.
PS this is for people who have the same question but not necessarily the same scenario as I think this will help more.

Categories