mpl_interactions.interactive_hist

interactive_hist(f, density=False, bins='auto', weights=None, figsize=None, ax=None, slider_format_string=None, display=True, force_ipywidgets=False, play_buttons=False, play_button_pos='right', **kwargs)[source]

Control the contents of a histogram using widgets.

See https://github.com/ianhi/mpl-interactions/pull/73#issue-470638134 for a discussion of the limitations of this function. These limitations will be improved once https://github.com/matplotlib/matplotlib/pull/18275 has been merged.

Parameters
  • f (function) – A function that will return a 1d array of which to take the histogram

  • density (bool, optional) – whether to plot as a probability density. Passed to np.histogram

  • bins (int or sequence of scalars or str, optional) – bins argument to np.histogram

  • weights (array_like, optional) – passed to np.histogram

  • figsize (tuple or scalar) – If tuple it will be used as the matplotlib figsize. If a number then it will be used to scale the current rcParams figsize

  • ax (matplotlib axis, optional) – If None a new figure and axis will be created

  • slider_format_string (None, string, or dict) – If None a default value of decimal points will be used. For ipywidgets this uses the new f-string formatting For matplotlib widgets you need to use % style formatting. A string will be used as the default format for all values. A dictionary will allow assigning different formats to different sliders. note: For matplotlib >= 3.3 a value of None for slider_format_string will use the matplotlib ScalarFormatter object for matplotlib slider values.

  • display (boolean) – If True then the output and controls will be automatically displayed

  • force_ipywidgets (boolean) – If True ipywidgets will always be used, even if not using the ipympl backend. If False the function will try to detect if it is ok to use ipywidgets If ipywidgets are not used the function will fall back on matplotlib widgets

  • play_buttons (bool or dict, optional) – Whether to attach an ipywidgets.Play widget to any sliders that get created. If a boolean it will apply to all kwargs, if a dictionary you choose which sliders you want to attach play buttons too.

  • play_button_pos (str, or dict, or list(str)) – ‘left’ or ‘right’. Whether to position the play widget(s) to the left or right of the slider(s)

Returns

  • fig (matplotlib figure)

  • ax (matplotlib axis)

  • controls (list of widgets)

Examples

With numpy arrays:

loc = np.linspace(-5, 5, 500)
scale = np.linspace(1, 10, 100)
def f(loc, scale):
    return np.random.randn(1000)*scale + loc
interactive_hist(f, loc=loc, scale=scale)

with tuples:

def f(loc, scale):
    return np.random.randn(1000)*scale + loc
interactive_hist(f, loc=(-5, 5, 500), scale=(1, 10, 100))