0

I have one python file Vis.py with the following two functions:

import matplotlib.pyplot as plt from matplotlib.widgets import Slider def update(val): #needed for slider function of plot_test pos = spos.val ax.axis([pos,pos+10,-1,1]) fig.canvas.draw_idle() def plot_test(data): fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) plt.plot(data) plt.axis([0, 10, -1, 1]) axcolor = 'lightgoldenrodyellow' axpos = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor=axcolor) spos = Slider(axpos, 'Pos', 0.1, 90.0) spos.on_changed(update) plt.show(); 

and I am trying to use the plot_test function in a separate ipynb file:

%matplotlib notebook from Vis import * import numpy as np t = np.arange(0.0, 200.0, 0.1) s = np.sin(2*np.pi*t) plot_test(s) 

However, the plot doesn't show up, not even an empty white space. I tried running %matplotlib inline before plot_test(s). That makes the plot show up, but it also gets rid of the interactiveness of the plot.

2
  • It works fine for me; though to get an interactive slider you would need to define ax in the updating function. Best make update part of plot_test(), such that ax is accessible in the namespace. Commented May 7, 2019 at 0:42
  • Huh strange. So you can use the slider on the bottom, using my code as-is? Regarding the update function, I'd agree with you, however the update function is being used as in my example all over the internet (matplotlib.org/gallery/widgets/slider_demo.html, stackoverflow.com/questions/31001713/…). If you can provide an explicit example with the update function being part of the plot_test() function and working interactively, I'll gladly accept it as an answer. Commented May 7, 2019 at 1:22

1 Answer 1

1

The updating function references ax, which is out of scope. A solution is to put the updating function inside the plot_test function.

import matplotlib.pyplot as plt from matplotlib.widgets import Slider def plot_test(data): fig, ax = plt.subplots() plt.subplots_adjust(bottom=0.25) plt.plot(data) plt.axis([0, 10, -1, 1]) axcolor = 'lightgoldenrodyellow' axpos = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor=axcolor) spos = Slider(axpos, 'Pos', 0.1, 90.0) def update(val): #needed for slider function of plot_test pos = spos.val ax.axis([pos,pos+10,-1,1]) fig.canvas.draw_idle() spos.on_changed(update) plt.show() 

Then, keeping the notebook part unchanged,

%matplotlib notebook from Vis import * import numpy as np t = np.arange(0.0, 200.0, 0.1) s = np.sin(2*np.pi*t) plot_test(s) 

results in the desired interactive figure for me.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Perfect, thanks! A small thing to add: For me, the plot still didn't show up, but adding %matplotlib notebook a second time after the from Vis import * line weirdly solved it for me. Might help someone in the future.
That's when you don't start with a fresh kernel, i.e. if have used the inline backend before, changed the line to notebook, but did not restart the kernel. See this answer.
No that wasn't it. I got rid of the inline statement and restarted the kernel, but it's not showing without the second %matplotlib notebook statement. Also, in the question you referenced, a blank white space appears, which for me wasn't the case.
Your notebook may be configured in a way to automatically load the inline backend. This seems to often be the case (though I don't know why that is).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.