From 96d4358667ae53ec57b4d80ee05f54d9593c67fb Mon Sep 17 00:00:00 2001 From: jeffreypaul15 Date: Thu, 3 Jun 2021 23:53:07 +0530 Subject: [PATCH 1/5] Updated example and doctring --- doc/api/animation_api.rst | 31 ++++++++++++++++++++++++++++++- lib/matplotlib/animation.py | 3 ++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/doc/api/animation_api.rst b/doc/api/animation_api.rst index 2e052b53a5ce..9cdca88c59b0 100644 --- a/doc/api/animation_api.rst +++ b/doc/api/animation_api.rst @@ -103,6 +103,7 @@ artist at a global scope and let Python sort things out. For example :: import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation + from functools import partial fig, ax = plt.subplots() xdata, ydata = [], [] @@ -124,7 +125,35 @@ artist at a global scope and let Python sort things out. For example :: plt.show() The second method is to use `functools.partial` to 'bind' artists to -function. A third method is to use closures to build up the required +function. :: + + import numpy as np + import matplotlib.pyplot as plt + from matplotlib.animation import FuncAnimation + + fig, ax = plt.subplots() + xdata, ydata = [], [] + ln, = plt.plot([], [], 'ro') + + def init(): + ax.set_xlim(0, 2*np.pi) + ax.set_ylim(-1, 1) + return ln, + + def update(frame): + xdata.append(frame) + ydata.append(np.sin(frame)) + ln.set_data(xdata, ydata) + return ln, + + ani = FuncAnimation( + fig, partial(update, offset=-0.5), + frames=np.linspace(0, 2 * np.pi, 128), + init_func=init, blit=True) + + plt.show() + +A third method is to use closures to build up the required artists and functions. A fourth method is to create a class. Examples diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index c15fa086eb15..f6ae108d5eea 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1622,7 +1622,8 @@ def init_func() -> iterable_of_artists value is unused if ``blit == False`` and may be omitted in that case. fargs : tuple or None, optional - Additional arguments to pass to each call to *func*. + Additional arguments to pass to each call to *func*. Note: the use of + `functools.partial` is preferred over fargs. save_count : int, default: 100 Fallback for the number of values from *frames* to cache. This is From e1b3df7feecd87699eec4faf0ad11d24608db178 Mon Sep 17 00:00:00 2001 From: jeffreypaul15 Date: Thu, 3 Jun 2021 23:57:26 +0530 Subject: [PATCH 2/5] update example --- doc/api/animation_api.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/animation_api.rst b/doc/api/animation_api.rst index 9cdca88c59b0..519c92532dcb 100644 --- a/doc/api/animation_api.rst +++ b/doc/api/animation_api.rst @@ -130,6 +130,7 @@ function. :: import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation + from functools import partial fig, ax = plt.subplots() xdata, ydata = [], [] From b739a44a59fe324e708fac623c2ab90c7ef03960 Mon Sep 17 00:00:00 2001 From: jeffreypaul15 Date: Sun, 6 Jun 2021 20:42:09 +0530 Subject: [PATCH 3/5] updated rst and example --- doc/api/animation_api.rst | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/api/animation_api.rst b/doc/api/animation_api.rst index 519c92532dcb..d69dca41d98a 100644 --- a/doc/api/animation_api.rst +++ b/doc/api/animation_api.rst @@ -124,7 +124,7 @@ artist at a global scope and let Python sort things out. For example :: init_func=init, blit=True) plt.show() -The second method is to use `functools.partial` to 'bind' artists to +The second method is to use `functools.partial` to pass arguments to the function. :: import numpy as np @@ -133,24 +133,24 @@ function. :: from functools import partial fig, ax = plt.subplots() - xdata, ydata = [], [] ln, = plt.plot([], [], 'ro') def init(): - ax.set_xlim(0, 2*np.pi) - ax.set_ylim(-1, 1) - return ln, + ax.set_xlim(0, 2*np.pi) + ax.set_ylim(-1, 1) + return ln, - def update(frame): - xdata.append(frame) - ydata.append(np.sin(frame)) - ln.set_data(xdata, ydata) - return ln, + def update(frame, x, y): + x.append(frame) + y.append(np.sin(frame)) + ln.set_data(xdata, ydata) + return ln, + xdata, ydata = [], [] ani = FuncAnimation( - fig, partial(update, offset=-0.5), - frames=np.linspace(0, 2 * np.pi, 128), - init_func=init, blit=True) + fig, partial(update, x=xdata, y=ydata), + frames=np.linspace(0, 2 * np.pi, 128), + init_func=init, blit=True) plt.show() From 7561a72a90022b668eabe02b57caf63cb577cefc Mon Sep 17 00:00:00 2001 From: Jeffrey Aaron Paul <50923538+jeffreypaul15@users.noreply.github.com> Date: Sun, 6 Jun 2021 21:05:21 +0530 Subject: [PATCH 4/5] Update lib/matplotlib/animation.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/animation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index f6ae108d5eea..c96923bb86b3 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1623,7 +1623,7 @@ def init_func() -> iterable_of_artists fargs : tuple or None, optional Additional arguments to pass to each call to *func*. Note: the use of - `functools.partial` is preferred over fargs. + `functools.partial` is preferred over *fargs*. save_count : int, default: 100 Fallback for the number of values from *frames* to cache. This is From dff7c58e98feb17145d1f79de92b335d7ee6ce71 Mon Sep 17 00:00:00 2001 From: Jeffrey Aaron Paul <50923538+jeffreypaul15@users.noreply.github.com> Date: Tue, 8 Jun 2021 07:06:12 +0530 Subject: [PATCH 5/5] Update doc/api/animation_api.rst Co-authored-by: Elliott Sales de Andrade --- doc/api/animation_api.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/animation_api.rst b/doc/api/animation_api.rst index d69dca41d98a..1d4bb34bb5f7 100644 --- a/doc/api/animation_api.rst +++ b/doc/api/animation_api.rst @@ -148,9 +148,9 @@ function. :: xdata, ydata = [], [] ani = FuncAnimation( - fig, partial(update, x=xdata, y=ydata), - frames=np.linspace(0, 2 * np.pi, 128), - init_func=init, blit=True) + fig, partial(update, x=xdata, y=ydata), + frames=np.linspace(0, 2 * np.pi, 128), + init_func=init, blit=True) plt.show()