Skip to content

[Doc]: Frame grabbing docs outdated? #28794

Closed

Description

@v0lta

Documentation Link

https://matplotlib.org/stable/gallery/animation/frame_grabbing_sgskip.html

Problem

An API part that used to accept scalars now expects a sequence.
Running:

import numpy as np

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt

from matplotlib.animation import FFMpegWriter

# Fixing random state for reproducibility
np.random.seed(19680801)


metadata = dict(title='Movie Test', artist='Matplotlib',
                comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata)

fig = plt.figure()
l, = plt.plot([], [], 'k-o')

plt.xlim(-5, 5)
plt.ylim(-5, 5)

x0, y0 = 0, 0

with writer.saving(fig, "writer_test.mp4", 100):
    for i in range(100):
        x0 += 0.1 * np.random.randn()
        y0 += 0.1 * np.random.randn()
        l.set_data(x0, y0)
        writer.grab_frame()

from https://matplotlib.org/stable/gallery/animation/frame_grabbing_sgskip.html

Used to work, but now it produces:

RuntimeError                              Traceback (most recent call last)
Cell In[1], line 30
     28 x0 += 0.1 * np.random.randn()
     29 y0 += 0.1 * np.random.randn()
---> 30 l.set_data(x0, y0)
     31 writer.grab_frame()

File ~/miniconda3/envs/pytorch/lib/python3.12/site-packages/matplotlib/lines.py:665, in Line2D.set_data(self, *args)
    662 else:
    663     x, y = args
--> 665 self.set_xdata(x)
    666 self.set_ydata(y)

File ~/miniconda3/envs/pytorch/lib/python3.12/site-packages/matplotlib/lines.py:1289, in Line2D.set_xdata(self, x)
   1276 """
   1277 Set the data array for x.
   1278 
   (...)
   1286 set_ydata
   1287 """
   1288 if not np.iterable(x):
-> 1289     raise RuntimeError('x must be a sequence')
   1290 self._xorig = copy.copy(x)
   1291 self._invalidx = True

RuntimeError: x must be a sequence

Which was not a problem in some previous versions.

Suggested improvement

  • We should update the docs with an example illustrating how people should use the new implementation of FFMpegWriter .

Activity

  1. v0lta commented on Sep 9, 2024

    @v0lta
    ContributorAuthor

    The fix appears to be quite straightforward:

    import numpy as np
    
    import matplotlib
    
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt
    
    from matplotlib.animation import FFMpegWriter
    
    # Fixing random state for reproducibility
    np.random.seed(19680801)
    
    
    metadata = dict(title='Movie Test', artist='Matplotlib',
                    comment='Movie support!')
    writer = FFMpegWriter(fps=15, metadata=metadata)
    
    fig = plt.figure()
    l, = plt.plot([], [], 'k-o')
    
    plt.xlim(-5, 5)
    plt.ylim(-5, 5)
    
    x0, y0 = 0, 0
    
    with writer.saving(fig, "writer_test.mp4", 100):
        for i in range(100):
            x0 += 0.1 * np.random.randn()
            y0 += 0.1 * np.random.randn()
            l.set_data([x0], [y0])
            writer.grab_frame()

    works as expected.

  2. QuLogic commented on Sep 9, 2024

    @QuLogic
    Member

    That looks correct; would you like to open a PR fixing it?

  3. v0lta commented on Sep 10, 2024

    @v0lta
    ContributorAuthor

    Yes, i will put one together.

  4. rcomer commented on Sep 10, 2024

    @rcomer
    Member

    I'm curious why this didn't show up as a doc build failure. Is it something to do with the context manager?

  5. tacaswell commented on Sep 10, 2024

    @tacaswell
    Member

    This example is not run as part of the build (due to the _sgskip in the name).

  6. rcomer commented on Sep 10, 2024

    @rcomer
    Member

    Should we then also remove this line?

    Output generated via `matplotlib.animation.Animation.to_jshtml`.

  7. rcomer commented on Sep 10, 2024

    @rcomer
    Member

    Sorry - finger slipped!

  8. reopened this on Sep 10, 2024
  9. v0lta commented on Sep 12, 2024

    @v0lta
    ContributorAuthor

    Should we then also remove this line?

    Output generated via `matplotlib.animation.Animation.to_jshtml`.

    I did that, too.

  10. v0lta commented on Sep 12, 2024

    @v0lta
    ContributorAuthor

    PR is out: #28805

  11. added this to the v3.9-doc milestone on Sep 12, 2024
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