FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FuncAnimation Named Arguments · Issue #20326 · matplotlib/matplotlib · GitHub

Repository navigation

FuncAnimation Named Arguments #20326

Closed

Description

Problem

I would like to use named arguments in FuncAnimation.

Currently:

ani = matplotlib.animation.FuncAnimation(
  fig, update, frames=len(raw_history),
  interval=500, repeat=True, repeat_delay=1,
  fargs=(raw_history, action_history, style)
)

This would be slightly cumbersome when I change the order / type of arguments my function accepts. I could use functools to attach named parameters to the function update, but it would be nice if FuncAnimation can support this feature.

Proposed Solution

ani = matplotlib.animation.FuncAnimation(
  fig, update, frames=len(raw_history),
  interval=500, repeat=True, repeat_delay=1,
  fkwargs={'raw_history': raw_history, 'action_history': action_history, 'style': style}
)

We keep the fargs parameter, but we also add an fkwargs for keyword parameters.

Activity

  1. anntzer commented on May 29, 2021

    Contributor

    I don't particularly like fargs to begin with (I prefer functools.partial), but given that it's here, adding fkwargs seems reasonable.

  2. timhoffm commented on May 29, 2021

    Member

    So why don‘t we discourage fargs and advertise partial instead, including the note that you can use kwargs there?

  3. anntzer commented on May 29, 2021

    Contributor

    That works for me too.

  4. jeffreypaul15 commented on May 31, 2021

    Contributor

    This would just require adding fkwargs as keyword arguments right? I also noticed that fargs doesn't really have any sort of test written for it, can I give it a go?

  5. timhoffm commented on May 31, 2021

    Member

    Um, no. functools.partial lets you create new functions with some parameters bound.

    This works for both positional

    my_update = partial(update, raw_history, action_history, style)
    

    and keyword argumetns

    my_update = partial(update, raw_history=raw_history, action_history=action_history, style=style)
    

    or any combination. This is a builtin capability of python and does not require any changes within matplotlib (except maybe that we should encourage this use in the FuncAnimation docs).

    You can pass my_update to FuncAnimation, or use it inlined:

    from functools import partial
    
    ani = matplotlib.animation.FuncAnimation(
      fig,
      partial(update, raw_history=raw_history, action_history=action_history, style=style),
      frames=len(raw_history), interval=500, repeat=True, repeat_delay=1,
    )
    
  6. tacaswell commented on Jun 2, 2021

    Member

    In defense of saying "use partial" rather than adding support for fkwargs is cite https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.call_later

    The optional positional args will be passed to the callback when it is called. If you want the callback to be called with keyword arguments use functools.partial().

  7. tacaswell commented on Jun 2, 2021

    Member

    @jeffreypaul15 What would be super helpful would be a note in the FuncAnitamation docstring pointing users at functools.partial and maybe updating / tweaking an existing example or adding a new example that make use of functools.partial.

  8. added this to the v3.6.2 milestone on Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL