matplotlib/matplotlib · error · RuntimeError

You must first set the image array or the image attribute

Error message

You must first set the image array or the image attribute

What it means

_make_image is the render-path helper that converts the stored array into an RGBA buffer. It raises RuntimeError when the array it must rasterize is None, meaning a draw reached an image artist that has no data. This is an internal invariant: the normal draw pipeline expects data to be set before rendering.

Source

Thrown at lib/matplotlib/image.py:407

        round_to_pixel_border : bool, default: True
            If True, the output image size will be rounded to the nearest pixel
            boundary.  This makes the images align correctly with the Axes.
            It should not be used if exact scaling is needed, such as for
            `.FigureImage`.

        Returns
        -------
        image : (M, N, 4) `numpy.uint8` array
            The RGBA image, resampled unless *unsampled* is True.
        x, y : float
            The upper left corner where the image should be drawn, in pixel
            space.
        trans : `~matplotlib.transforms.Affine2D`
            The affine transformation from image to pixel space.
        """
        if A is None:
            raise RuntimeError('You must first set the image '
                               'array or the image attribute')
        if A.size == 0:
            raise RuntimeError("_make_image must get a non-empty image. "
                               "Your Artist's draw method must filter before "
                               "this method is called.")

        clipped_bbox = Bbox.intersection(out_bbox, clip_bbox)

        if clipped_bbox is None:
            return None, 0, 0, None

        # Define the magnified bbox after clipping
        magnified_extents = clipped_bbox.extents * magnification
        if ((not unsampled) and round_to_pixel_border):
            # Round to the nearest output pixel
            # Add a tiny fudge amount to account for numerical precision loss
            # on the two sides away from the Agg anchor point (x0, y1)
            x0 = np.floor(magnified_extents[0] + 0.5)  # round half up

View on GitHub (pinned to b379c1b69e)

Solutions

  1. Set the array before any draw: im.set_array(A) then fig.canvas.draw()
  2. Create the artist through ax.imshow(A) so data is attached at construction
  3. If you used set_array(None), restore an array before the next draw or remove the artist from the Axes

Example fix

# before
im = ax.imshow(np.zeros((4, 4)))
im.set_array(None)
fig.savefig('out.png')  # RuntimeError
# after
im.set_array(new_data)
fig.savefig('out.png')
Defensive patterns

Strategy: validation

Validate before calling

im = AxesImage(ax)
im.set_data(data)  # attach data before any draw can happen
ax.add_image(im)
fig.canvas.draw_idle()

Prevention

When it happens

Trigger: im = AxesImage(ax); ax.add_image(im); fig.canvas.draw() or fig.savefig(...) with no set_array call; also after im.set_array(None) frees the array and a redraw follows.

Common situations: Custom _ImageBase subclasses or external code calling make_image directly; interactive tools that add a placeholder artist and draw before the data stream fills it; memory-saving code that clears arrays after export.

Related errors


AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21). Data as JSON: /api/errors/663982b3e1c4a2b0. Report an issue: GitHub.