Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Clarify how to use keyword arguments in FuncAnimation #24139

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,8 @@ class FuncAnimation(TimedAnimation):
func : callable
The function to call at each frame. The first argument will
be the next value in *frames*. Any additional positional
arguments can be supplied via the *fargs* parameter.
arguments can be supplied via the *fargs* parameter. See *fargs* for
how to use `functools.partial` to supply keyword arguments.
Comment on lines +1521 to +1522
Copy link
Member

@story645 story645 Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
arguments can be supplied via the *fargs* parameter. See *fargs* for
how to use `functools.partial` to supply keyword arguments.
arguments can be supplied using `functools.partial` or via the *fargs* parameter.

Flipped because I think the functools version is preferred. I think it's gonna be clearer to add the information about partial in this section since it's passed in as the func keyword, Maybe after the required signature, something like:

To pass in a function with positional keywords, set func to the partial function::
      
     func = partial(func, frame, **kwargs)


The required signature is::

Expand Down Expand Up @@ -1564,6 +1565,14 @@ def init_func() -> iterable_of_artists
fargs : tuple or None, optional
Additional arguments to pass to each call to *func*.

An option to *fargs* is to use `functools.partial`, with the added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An option to *fargs* is to use `functools.partial`, with the added
An alternative to *fargs* is to use `functools.partial`, with the added

benefit of also supporting keyword arguments. An example is to pass
something like::

partial(func, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't you normally have the case that you want to set some kwargs explicitly? Then I suggest to make the example more concrete:

Suggested change
partial(func, **kwargs)
partial(func, my_kwarg='something')

Maye even show a more complete FuncAnimation call to show this fits in.


as *func*.

Comment on lines +1568 to +1575
Copy link
Member

@story645 story645 Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you move the intro of partial up to func, this can be simplified to

Suggested change
An option to *fargs* is to use `functools.partial`, with the added
benefit of also supporting keyword arguments. An example is to pass
something like::
partial(func, **kwargs)
as *func*.
Passing in `functools.partial` as *func* is preferred to passing in *fargs* and supports keyword arguments. See *func* for an example.

save_count : int, default: 100
Fallback for the number of values from *frames* to cache. This is
only used if the number of frames cannot be inferred from *frames*,
Expand Down