{"record":{"id":"5632c47567f56848","repo":"matplotlib/matplotlib","slug":"you-must-first-set-the-image-array","errorCode":null,"errorMessage":"You must first set the image array","messagePattern":"You must first set the image array","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/image.py","lineNumber":319,"sourceCode":"            shape = self.get_shape()\n            return f\"{type(self).__name__}(shape={shape!r})\"\n        except RuntimeError:\n            return type(self).__name__\n\n    def __getstate__(self):\n        # Save some space on the pickle by not saving the cache.\n        return {**super().__getstate__(), \"_imcache\": None}\n\n    def get_size(self):\n        \"\"\"Return the size of the image as tuple (numrows, numcols).\"\"\"\n        return self.get_shape()[:2]\n\n    def get_shape(self):\n        \"\"\"\n        Return the shape of the image as tuple (numrows, numcols, channels).\n        \"\"\"\n        if self._A is None:\n            raise RuntimeError('You must first set the image array')\n\n        return self._A.shape\n\n    def set_alpha(self, alpha):\n        \"\"\"\n        Set the alpha value used for blending - not supported on all backends.\n\n        Parameters\n        ----------\n        alpha : float or 2D array-like or None\n        \"\"\"\n        martist.Artist._set_alpha_for_array(self, alpha)\n        if np.ndim(alpha) not in (0, 2):\n            raise TypeError('alpha must be a float, two-dimensional '\n                            'array, or None')\n        self._imcache = None\n\n    def _get_scalar_alpha(self):","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/image.py#L301-L337","documentation":"_ImageBase.get_shape() and get_size() read self._A, the array installed by set_data/set_array. If the array was never set — typical for an AxesImage constructed directly rather than through imshow — _A is None and the method raises RuntimeError.","triggerScenarios":"im = AxesImage(ax) followed by im.get_size() or im.get_shape() before any im.set_array(A) / set_data(A) call.","commonSituations":"Wrappers or subclasses that build the artist first and attach streamed data later; querying the shape to configure an Axes before the data pipeline has produced the array.","solutions":["Call set_array(A) (or set_data(A)) before get_size()/get_shape()","Prefer creating the artist through ax.imshow(A), which sets data at construction","Gate shape queries on im.get_array() is not None"],"exampleFix":"im = AxesImage(ax)\n# before\nh, w = im.get_size()  # RuntimeError\n# after\nim.set_array(arr)\nh, w = im.get_size()","handlingStrategy":"validation","validationCode":"im = AxesImage(ax)\nif im.get_array() is None:\n    im.set_array(arr)\nh, w = im.get_size()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set data immediately after constructing image artists by hand","Gate shape queries on get_array() is not None"],"tags":["matplotlib","image","state-not-set","runtimeerror"],"backgroundTag":"state-not-initialized","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}