1

I'm trying to create a simple GUI that works embedded in a jupyter notebook. The idea is to create a slider widget that updates a plot. Once a user finds the preferred slider position, then he just clicks a button widget, and the program saves the slider position (user feedback).

The following code works when at the beginning of the jupyter notebook I set:

%matplotlib qt

Code (shortened) that creates plots, sliders, and updating mechanism:

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

fig, ax = plt.subplots(nrows=1, ncols=2)
... CODE THAT DEFINES THE PLOTS
fig.text(0.1, 0.035, "$\it{Please\ select\ the\ most\ realistic\ hyperparameter\ configuration. }$")
plt.draw()

paramfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor='lightgoldenrodyellow')
slider = Slider(paramfreq, 'Hyperparam. conf.', 1, self.user_feedback_grid_size, valinit=1, valstep=1)
def update(val):
    param_ind = int(slider.val-1)
    update_errorbar(l1, x_axis_points, np.mean(self.irf(param_ind,'y'),axis=0), xerr=None, yerr=np.std(self.irf(0,'y'),axis=0)) 
    update_errorbar(l2, x_axis_points, np.mean(self.irf(param_ind,'r'),axis=0), xerr=None, yerr=np.std(self.irf(0,'r'),axis=0)) 
    fig.canvas.draw_idle() 
slider.on_changed(update) 

confirm_ = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(confirm_, 'Confirm', color='lightgoldenrodyellow', hovercolor='0.975')
def confirm(event):
    typed_value = int(slider.val)
    print('Your choice was: ' + str(typed_value))
    self.user_feedback = self.current_xi_grid[(int(typed_value)-1),:]
    self.user_feedback_was_given = True
    plt.close('all')

button.on_clicked(confirm)
plt.show()

Event loop:

while True:
    if self.user_feedback_was_given:
        break
    plt.pause(0.5)

The problem is that I would like the GUI to be embedded in the jupyter notebook. When I try to achieve this by replacing

%matplotlib qt

by

%matplotlib widget

I run to a new problem: "plt.pause()" does not work with %matplotlib widget (ipympl). I have tried numerous different approaches but I cannot find a solution that works. Thanks in advance for any help.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.