{"record":{"id":"663982b3e1c4a2b0","repo":"matplotlib/matplotlib","slug":"you-must-first-set-the-image-array-or-the-image-at","errorCode":null,"errorMessage":"You must first set the image array or the image attribute","messagePattern":"You must first set the image array or the image attribute","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/image.py","lineNumber":407,"sourceCode":"\n        round_to_pixel_border : bool, default: True\n            If True, the output image size will be rounded to the nearest pixel\n            boundary.  This makes the images align correctly with the Axes.\n            It should not be used if exact scaling is needed, such as for\n            `.FigureImage`.\n\n        Returns\n        -------\n        image : (M, N, 4) `numpy.uint8` array\n            The RGBA image, resampled unless *unsampled* is True.\n        x, y : float\n            The upper left corner where the image should be drawn, in pixel\n            space.\n        trans : `~matplotlib.transforms.Affine2D`\n            The affine transformation from image to pixel space.\n        \"\"\"\n        if A is None:\n            raise RuntimeError('You must first set the image '\n                               'array or the image attribute')\n        if A.size == 0:\n            raise RuntimeError(\"_make_image must get a non-empty image. \"\n                               \"Your Artist's draw method must filter before \"\n                               \"this method is called.\")\n\n        clipped_bbox = Bbox.intersection(out_bbox, clip_bbox)\n\n        if clipped_bbox is None:\n            return None, 0, 0, None\n\n        # Define the magnified bbox after clipping\n        magnified_extents = clipped_bbox.extents * magnification\n        if ((not unsampled) and round_to_pixel_border):\n            # Round to the nearest output pixel\n            # Add a tiny fudge amount to account for numerical precision loss\n            # on the two sides away from the Agg anchor point (x0, y1)\n            x0 = np.floor(magnified_extents[0] + 0.5)  # round half up","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/image.py#L389-L425","documentation":"_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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set the array before any draw: im.set_array(A) then fig.canvas.draw()","Create the artist through ax.imshow(A) so data is attached at construction","If you used set_array(None), restore an array before the next draw or remove the artist from the Axes"],"exampleFix":"# before\nim = ax.imshow(np.zeros((4, 4)))\nim.set_array(None)\nfig.savefig('out.png')  # RuntimeError\n# after\nim.set_array(new_data)\nfig.savefig('out.png')","handlingStrategy":"validation","validationCode":"im = AxesImage(ax)\nim.set_data(data)  # attach data before any draw can happen\nax.add_image(im)\nfig.canvas.draw_idle()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never let an image artist without data reach a draw","After set_array(None), always restore an array or remove the artist before rendering"],"tags":["matplotlib","image","draw","state-not-set"],"backgroundTag":"state-not-initialized","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}