Skip to main content

Matplotlib aware interact functions

Project description

mpl_interactions

All Contributors

Documentation StatusBinder (Warning: The interactions will be laggy when on binder)

Welcome!

mpl_interactions' library provides helpful ways to interact with Matplotlib plots. A summary of key components can be found below. Fuller narrative, further examples, and more information can be found on ReadtheDocs.

There are three submodules:

pyplot

Control Matplotlib plots using sliders and other widgets to adjust the parameters of the functions you are plotting. If working in a notebook then ipywidgets will be used to make the sliders, otherwise Matplotlib widgets will be used.

This is a different approach to controlling plots with sliders than ipywidgets.interact as when using interact you are responsible for:

  1. Defining the function to plot f(x,...) => y
  2. Handling the plotting logic (plt.plot, fig.cla, ax.set_ylim, etc)

In contrast, with mpl-interactions you only need to provide f(x, ...) => y and the plotting and updating boilerplate are handled for you.

x = np.linspace(0,6,100)
beta = np.linspace(0,5*np.pi)
def f(x, beta):
    return np.sin(x*4+beta)
interactive_plot(f, x=x, beta=beta)

These functions are designed to be used with ipympl, the backend that is designed for use in Jupyter Notebooks. So for optimal performance, make sure you set the backend with %matplotlib ipympl. That said, these functions will also work with any interactive backend (e.g. %matplotlib qt5).

generic

Provides ways to interact with Matplotlib that will work outside of a Jupyter Notebook; this should work equally well with any backend.

  1. A very niche (but very cool) way to compare 2D heatmaps
  2. Scroll to zoom
  3. Middle click to pan

utils

This module includes utility functions to make things just that little bit easier.

  1. ioff as a context manager
from mpl_interactions.utils import ioff
with ioff:
    # interactive mode will be off
    fig = plt.figure()
    # other stuff
# interactive mode will be on
  1. figure that accepts a scalar for figsize (this will scale the default dimensions)
from mpl_interactions.utils import figure
fig = figure(3)
# the default figsize is [6.4, 4.8], this figure will have figsize = [6.4*3, 4.8*3]
  1. nearest_idx -- avoid ever having to write np.argmin(np.abs(arr - value)) again

Installation

pip install mpl_interactions

# if using jupyterlab
conda install -c conda-forge nodejs=13
jupyter labextension install @jupyter-widgets/jupyterlab-manager

If you use JupyterLab, make sure you follow the full instructions in the ipympl readme in particular installing jupyterlab-manager.

Contributing / feature requests / roadmap

I use the GitHub issues to keep track of ideas I have, so looking through those should serve as a roadmap of sorts. For the most part I add to the library when I create a function that is useful for the science I am doing. If you create something that seems useful a PR would be most welcome so we can share it easily with more people. I'm also open to feature requests if you have an idea.

Documentation

The fuller narrative documentation can be found on ReadTheDocs. You may also find it helpful to check out the examples directory.

Examples with GIFs!

Tragically, neither GitHub nor the sphinx documentation render the actual moving plots so instead, here are gifs of the functions. The code for these can be found in the notebooks in the examples directory.

interactive_plot

Easily make a line plot interactive:

heatmap_slicer

Compare vertical and horizontal slices across multiple heatmaps:

scrolling zoom + middle click pan

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Ian Hunt-Isaak

💻

Sam

📖

Jenny Coulter

📓

Sabina Haque

📖 📓 💻

John Russell

💻 📓 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpl_interactions-0.12.0.tar.gz (31.5 MB view details)

Uploaded Source

Built Distribution

mpl_interactions-0.12.0-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file mpl_interactions-0.12.0.tar.gz.

File metadata

  • Download URL: mpl_interactions-0.12.0.tar.gz
  • Upload date:
  • Size: 31.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for mpl_interactions-0.12.0.tar.gz
Algorithm Hash digest
SHA256 c3d4255b4a9678096d7a71e78007d094dbe8a5c9ce8a8d1fb9c4a4ab1b1be358
MD5 ba09c42d822b73d9b25360c454e19a2d
BLAKE2b-256 dc3e4ab2a9dd18e90e8ac77f4341d25d73df02a2e9cc5b201a703da4e7fe66f6

See more details on using hashes here.

File details

Details for the file mpl_interactions-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: mpl_interactions-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for mpl_interactions-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b5627dbff6bfb1d539a71f41f6b531c7c78e4f3ae361c3a1ae4edd6d9c11ad9
MD5 94435c456214c9a5aa09281368c2ca38
BLAKE2b-256 b7d45898ea7e1d10842bfe74d71c402a9d21e461b893f55facb85ec84fbdbcc9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page