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.

emilaz
  • 1,722
  • 1
  • 15
  • 31
  • 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. – ImportanceOfBeingErnest May 07 '19 at 00: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 (https://matplotlib.org/gallery/widgets/slider_demo.html, https://stackoverflow.com/questions/31001713/plotting-the-data-with-scrollable-x-time-horizontal-axis-on-linux). 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. – emilaz May 07 '19 at 01:22

1 Answers1

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 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. – emilaz May 07 '19 at 01:41
  • 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](https://stackoverflow.com/a/41251483/4124317). – ImportanceOfBeingErnest May 07 '19 at 01:48
  • 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. – emilaz May 07 '19 at 01:56
  • 1
    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). – ImportanceOfBeingErnest May 07 '19 at 02:00