An image with pixels on a rectilinear grid. In contrast to `.AxesImage`, where pixels are on a regular grid, NonUniformImage allows rows and columns with individual heights / widths. See also :doc:`/gallery/images_contours_and_fields/image_nonuniform`.
| 1029 | |
| 1030 | |
| 1031 | class NonUniformImage(AxesImage): |
| 1032 | """ |
| 1033 | An image with pixels on a rectilinear grid. |
| 1034 | |
| 1035 | In contrast to `.AxesImage`, where pixels are on a regular grid, |
| 1036 | NonUniformImage allows rows and columns with individual heights / widths. |
| 1037 | |
| 1038 | See also :doc:`/gallery/images_contours_and_fields/image_nonuniform`. |
| 1039 | """ |
| 1040 | |
| 1041 | def __init__(self, ax, *, interpolation='nearest', **kwargs): |
| 1042 | """ |
| 1043 | Parameters |
| 1044 | ---------- |
| 1045 | ax : `~matplotlib.axes.Axes` |
| 1046 | The Axes the image will belong to. |
| 1047 | interpolation : {'nearest', 'bilinear'}, default: 'nearest' |
| 1048 | The interpolation scheme used in the resampling. |
| 1049 | **kwargs |
| 1050 | All other keyword arguments are identical to those of `.AxesImage`. |
| 1051 | """ |
| 1052 | super().__init__(ax, **kwargs) |
| 1053 | self.set_interpolation(interpolation) |
| 1054 | |
| 1055 | def _check_unsampled_image(self): |
| 1056 | """Return False. Do not use unsampled image.""" |
| 1057 | return False |
| 1058 | |
| 1059 | def make_image(self, renderer, magnification=1.0, unsampled=False): |
| 1060 | # docstring inherited |
| 1061 | if self._A is None: |
| 1062 | raise RuntimeError('You must first set the image array') |
| 1063 | if unsampled: |
| 1064 | raise ValueError('unsampled not supported on NonUniformImage') |
| 1065 | A = self._A |
| 1066 | if A.ndim == 2: |
| 1067 | if A.dtype != np.uint8: |
| 1068 | A = self.to_rgba(A, bytes=True) |
| 1069 | else: |
| 1070 | A = np.repeat(A[:, :, np.newaxis], 4, 2) |
| 1071 | A[:, :, 3] = 255 |
| 1072 | else: |
| 1073 | if A.dtype != np.uint8: |
| 1074 | A = (255*A).astype(np.uint8) |
| 1075 | if A.shape[2] == 3: |
| 1076 | B = np.zeros(tuple([*A.shape[0:2], 4]), np.uint8) |
| 1077 | B[:, :, 0:3] = A |
| 1078 | B[:, :, 3] = 255 |
| 1079 | A = B |
| 1080 | magnified_extents = (self.axes.bbox.extents * magnification + 0.5).astype(int) |
| 1081 | l, b, r, t = magnified_extents / magnification |
| 1082 | width = int((r - l) * magnification) |
| 1083 | height = int((t - b) * magnification) |
| 1084 | |
| 1085 | invertedTransform = self.axes.transData.inverted() |
| 1086 | x_pix_edges = invertedTransform.transform( |
| 1087 | [(x, b) for x in np.linspace(l, r, width + 1)])[:, 0] |
| 1088 | y_pix_edges = invertedTransform.transform( |
no outgoing calls
searching dependent graphs…