Let’s back up. What is the goal here? You already know you can display them both when they aren’t interactive. What are your actual needs?
As for what is going on currently when you run code cell 2 as interactive and then try to run code cell 3, I have a guess without running your non-reproducible code:
You are getting to the first plot in code cell 2 ( I suspect it is a blue set of points on the line?) and making it the active interactive plot. It plots the time and speed from data data = pd.read_csv(r"PathToData")
. It sits there being interactive because you set it to be.
Alright, next you try running code cell 3. You have made the same variables and tell matplotlib to plot them. It plots them in the currently active interactive plot and you see them as the points making the orange line on the same plot above in code cell 2, right?
There’s a simpler model spelled out here. (One of the big reasons I point this out is because what you provide isn’t reproducible. We don’t have the files for data = pd.read_csv(r"PathToData")
and data = pd.read_csv(r"PathToData2")
. To get back information most pertinent to your needs, when posting at forums like this one you’d need to provide toy data or randomly generated versions that substitute in for that each data
is. One popular option folks use is to have numpy to randomly generate an array something similar to what you have.)
You may want to work through what the original person posting wrote and the long first reply to get a better idea of how matplotlib and ipympl are expected to work in conjunction with the Python kernel in Jupyter. There’s several other pertinent exchanges in that thread, too. Because it is complicated (plus see the four approaches shown here, this is sort of why I’m asking your goal here.
My feeling is that you may probably be good with the use of subplots with ipympl, see here? That issues thread may provide other options to test. (Though in my hands, using plt.plot()
subsequent times kept adding lines, and because I wasn’t defining axes I could not clear of lines using ax.cla()
or [l.remove() for l in ax.lines]
or ax.lines = []
as I usually do.)
Alternatively, maybe you just need to put %matplotlib ipympl
at the top of each cell? (That option was mentioned there, too.) And then run your first code cell and then in turn your secondd. Importantly, you cannot do Run All Cells
/ Kernel
> Restart Kernel and Run All Cells
or it doesn’t fully display the first plot, only the second. Running in each in turn didn’t exhibit that quirk where it shows empty space in place of where the first plot should show.
These two cells in the same notebook running in JupyterLab Version 3.4.8 worked for me:
Code Cell 1
%matplotlib ipympl
# based on https://mpl-interactions.readthedocs.io/en/stable/examples/plot.html , run `%pip install mpl_interactions` first!
import numpy as np
import matplotlib.pyplot as plt
import mpl_interactions.ipyplot as iplt
x = np.linspace(0, np.pi, 100)
tau = np.linspace(0.5, 10, 100)
beta = np.linspace(1, 10, 100)
def f1(x, tau, beta):
return np.sin(x * tau) * x * beta
def f2(x, tau, beta):
return np.sin(x * beta) * x * tau
fig, ax = plt.subplots()
controls = iplt.plot(x, f1, tau=tau, beta=beta, label="f1")
iplt.plot(x, f2, label="f2", controls=controls)
_ = plt.legend()
Code cell 2
%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt
import mpl_interactions.ipyplot as iplt
x2 = np.linspace(0, np.pi, 100)
tau2 = np.linspace(0.5, 10, 100)
beta2 = np.linspace(1, 10, 100)
def f3(x2, tau2, beta2):
return np.sin(x2 * tau2) * x2 * beta2
def f4(x2, tau2, beta2):
return np.sin(x2 * beta2) * x2 * tau2
fig, ax = plt.subplots()
controls = iplt.plot(x2, f3, tau2=tau2, beta2=beta2, label="f3")
iplt.plot(x2, f4, label="f4", controls=controls)
_ = plt.legend()
Each will be interactive in turn as they are run separately.
Note that I could also just paste Code cell 1 twice in the running .ipynb
file and step through using them, too, and it seemed to work. This is more in line with your provided code because you use the same variables data
, speed
and time
over in each, while reassigning them in between. I specifically made the variables and functions different in the code blocks to emphasize the plots can also be unrelated and in the same notebook file in separate cells.