Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of using mpl-interactions as an engine for updating without sliders #234

Open
ianhi opened this issue Feb 4, 2022 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@ianhi
Copy link
Collaborator

ianhi commented Feb 4, 2022

Problem

This package has put a lot of work into figuring out how to update various matplotlib artists. However sometimes people want to update a plot in a loop instead of wiht sliders, for example see matplotlib/ipympl#425. They should still be able to do this using mpl-interactions, but it's not obvious how to do this.

attn: @henrypinkard for inspiring this

Suggested Improvement

Include an example of something like this:

%matplotlib ipympl
import matplotlib.pyplot as plt
import mpl_interactions.ipyplot as iplt
import numpy as np
from matplotlib.widgets import AxesWidget
from matplotlib.cbook import CallbackRegistry
import time

def display_immediately(fig):
    """
    Force immediate display of a plot in ipympl. Not necessary in desktop backends
    """
    canvas = fig.canvas
    display(canvas)
    canvas._handle_message(canvas, {'type': 'send_image_mode'}, [])
    canvas._handle_message(canvas, {'type':'refresh'}, [])
    canvas._handle_message(canvas,{'type': 'initialized'},[])
    canvas._handle_message(canvas,{'type': 'draw'},[])

# currently needs to inherit form axeswidget rather than
# Widget due to limitations in mpl-interactions
class progress(AxesWidget):
    """A small widget to pass as a kwarg to mpl-interactions"""
    def __init__(self):
        self._val = 0
        self._observers = CallbackRegistry()
    @property
    def val(self):
        return self._val
    @val.setter
    def val(self, value):
        # could add validation here
        self._val = value
        self._observers.process("changed", value)
    def on_changed(self, func):
        return self._observers.connect("changed", lambda val: func(val))

def f(t):
    if t==0:
        # otherwise line doesn't get made and we will
        # get weird errors later on
        return [0,0]
    # make some fake data
    steps = np.arange(t)
    return [steps, np.sin(steps)]

# make our widget for updating the plot
prog = progress()

with plt.ioff():
    fig, ax = plt.subplots()
display_immediately(fig) # only necessary with ipympl backend

iplt.plot(f, t=prog, parametric=True)

   


for i in range(1, 10):
    # do some work that will update our data sources
    # make the updates plot
    prog.val = i
    # mpl-interactions should be handling this draw for us
    # but it only ever does draw_idle
    # should add an option to draw immediately.
    fig.canvas.draw() 
    time.sleep(.5)
@ianhi ianhi added bug Something isn't working documentation Improvements or additions to documentation labels Feb 4, 2022
@ianhi ianhi removed the bug Something isn't working label Feb 4, 2022
@ianhi
Copy link
Collaborator Author

ianhi commented Feb 4, 2022

Should probably come up with a better name for that widget and then incorporate it into the library. Would also be sweet if this worked with ipywidgets progressbars

@ianhi
Copy link
Collaborator Author

ianhi commented Feb 4, 2022

oop turns out it just does work immediately - pretty sweet:

prog = IntProgress()
with plt.ioff():
    fig, ax = plt.subplots()
display_immediately(fig) # only necessary with ipympl backend

iplt.plot(f, t=prog, parametric=True)

for i in range(1, 10):
    # do some work that will update our data sources
    # make the updates plot
    prog.value = i
    # mpl-interactions should be handling this draw for us
    # but it only ever does draw_idle
    # should add an option to draw immediately.
    fig.canvas.draw() 
    time.sleep(.5)
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant