YouTip LogoYouTip

Matplotlib Ref Imshow

* * * [![Image 1: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html) `imshow()` is used to display images on Axes or render a 2D array as a heatmap. It is the most commonly used 2D data visualization function, supporting multiple interpolation, colormap, and scaling methods. ## Function Definition ### pyplot Interface matplotlib.pyplot.imshow(X, cmap=None, norm=None, *, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, filternorm=True, filterrad=4.0, resample=None, url=None, **kwargs) ### Axes Interface Axes.imshow(X, cmap=None, norm=None, *, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, filternorm=True, filterrad=4.0, resample=None, url=None, **kwargs) ## Parameter Description | Parameter | Type | Description | | --- | --- | --- | | X | array-like or PIL Image | Image data to display. 2D array displayed as grayscale/pseudo-color, 3D array (MxNx3 or MxNx4) displayed as RGB/RGBA image | | cmap | str or Colormap | Colormap, only valid for 2D data. Default 'viridis' | | norm | Normalize or str | Data normalization method, such as 'linear', 'log', 'symlog' | | aspect | str or float | Pixel aspect ratio: 'equal' (default, pixels are square), 'auto' (automatically fill Axes), number (custom ratio) | | interpolation | str | Interpolation method: 'none'/'nearest' (no interpolation), 'bilinear', 'bicubic', 'antialiased' (default), etc. | | alpha | float or array-like | Transparency, 0-1 | | vmin, vmax | float | Data range for colormap | | origin | str | Origin location: 'upper' (default, [0,0] at top-left) or 'lower' ([0,0] at bottom-left) | | extent | tuple (left,right,bottom,top) | Data boundary coordinates, changes axis labels but not data | > `origin='upper'` (default) places the first row of the array at the top, following image display conventions. `origin='lower'` places the first row at the bottom, following mathematical coordinate system conventions. * * * ## Usage Examples ### Example 1: Display 2D Array (Heatmap) ## Instance import matplotlib.pyplot as plt import numpy as np # Create a 10x10 2D array np.random.seed(42) data = np.random.rand(10,10) fig, ax = plt.subplots(figsize=(6,5), layout='constrained') # Display as heatmap im = ax.imshow(data, cmap='viridis', aspect='auto') # Display value in each cell for i in range(10): for j in range(10): color ='white'if data[i, j]>0.5 else'black' ax.text(j, i, f'{data[i,j]:.2f}', ha='center', va='center', color=color, fontsize=8) # Add colorbar fig.colorbar(im, ax=ax, label='Value', shrink=0.8) ax.set_title('2D Array as Heatmap') ax.set_xlabel('Column') ax.set_ylabel('Row') plt.show() ### Example 2: extent Parameter Controls Axis Coordinates ## Instance import matplotlib.pyplot as plt import numpy as np # Create 50x50 mathematical function data x = np.linspace(-3,3,50) y = np.linspace(-3,3,50) X, Y = np.meshgrid(x, y) Z = np.sin(X
← Matplotlib Ref Layout ConfigMatplotlib Ref Fill_Between β†’