Matplotlib aware interact functions
Project description
mpl-interactions
(Warning: The interactions will be laggy when on binder)
This library provides helpful ways to interact with Matplotlib plots. There are three submodules:
jupyter
Provides a different approach than ipywidgets.interact
for making sliders that affect a Matplotlib plot. When using interact
you are responsible for:
- Defining the function to plot
f(x,...) => y
- 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.
- A very niche (but very cool) way to compare 2D heatmaps
- Scroll to zoom
- Middle click to pan
utils
This module includes utility functions to make things just that little bit easier
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
figure
that accepts a scalar forfigsize
(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]
nearest_idx
-- avoid ever having to writenp.argmin(np.abs(arr - value))
again
Installation
pip install mpl_interactions
# if using jupyterlab
conda install -c conda-forge nodejs>10
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
Definitely a work in progress--I would recommend checking out the examples directory. The minimal narrative documentation can be found on ReadTheDocs
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 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mpl_interactions-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6657d3316c61b30d60a3efbc39745fd11b40b53a69fe31d0b4ce7f02ccc6466 |
|
MD5 | 4ca292ee5ebf1efa47954dd1880aada8 |
|
BLAKE2b-256 | ad3d4cbdb33b99c3b832a427887bd8e626545fb7f3807a6db1c27fbc3ce370f8 |