Sliders and numeric input fields

Hi there!

I’m using mpl_interactions with Sliders (from the matplotlib.widget) to set the starting values of a fitting procedure and it works very well!

The next step, would be to have the possibility to have instead of a sliders a simple numeric input (a field where I can write a number). This is because for a few parameters, I prefer to have a finer control.

Is something like this possible? I think it should be… Can you give me some hints / examples?


Hi Antonio,

great question! I agree that it feels like it *should* be possible but looking into it it’s not automatically built in like it is with a slider. This is because the matplotlib textbox widget is a bit more general than sliders and doesn’t share the same methods. It would be great to a have a numerical input textbox which would be very easy.

That said, this is still possible! You have to manually attach a callback to the textbox and then use a private method of the controls object, but I don’t expect to change that method anytime soon so it should be safe for a while. If I do change it, it will likely be in an update that also introduces the ability to directly use textboxes. (but as ever caveat that using a private method is not garunteed to be stable)

I think the below example is what you want, please let me know if it’s not. One hard limitation is that if you were relying on the figure mpl-interactions creates to hold the sliders there is no easy way to place the textbox in that figure. It will either need to be in the figure with the plot or in a new figure that you create.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import TextBox
import mpl_interactions.ipyplot as iplt
from mpl_interactions.controller import Controls

# new utility method for attaching an mpl textbox to a controller
def make_submit(controls, name):
    ctrls : mpl_interactions.controller.Controls
        The relevant controls object
    name : str
        the name of the parameter that will follow this text box

    submit : Callable
        A function to be used with the textbox *on_submit* method.
    def submit(text):
        # could add better error checking here if you want
        # but realisitcally this belongs in a new mpl numerical input box
        # widget
        new_val = float(text)

        # use a private method to update the internal value
        # will need 
        controls._slider_updated({'new':float(text)}, name, None)
    return submit

x = np.linspace(0, 2 * np.pi, 1000)
def f1(x, tau, beta):
    return np.sin(x * tau) * x * beta

fig, ax = plt.subplots()

# manually create a controls object with tau as "fixed" parameters
# but secretly it's not fixed and we'll change in the textbox callback
tau = np.linspace(0.5, 10, 100)
ctrls = Controls(beta=2, tau=tau)

with ctrls:
    iplt.plot(x, f1)

# manually add the textbox widget
# this doesn't have to be in the same figure
# you can make another figure or put this whereever you want

txt_box_param_name = 'beta'
axbox = fig.add_axes([0.1, 0.05, 0.2, 0.075])
text_box = TextBox(axbox, txt_box_param_name, initial=str(ctrls.params[txt_box_param_name]), textalignment="left")
text_box.on_submit(make_submit(ctrls, name=txt_box_param_name));

# make sure to the display the controls

Oh also, fwiw if you are in a jupyter environment then you can just directly use the ipywidgets FloatText widget as shown here: Usage Guide — mpl-interactions

wow! Thanks a lot!

I will give it a try for sure tomorrow!

No problem for the figure, I’m always creating my own figure.

I will let you know in case of ‘additional’ problems!



On Wed, Feb 14, 2024 at 4:42 PM Ian Hunt-Isaak via Matplotlib <> wrote:

| ianhi
February 14 |

  • | - |

Oh also, fwiw if you are in a jupyter environment then you can just directly use the ipywidgets FloatText widget as shown here: Usage Guide — mpl-interactions

Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.