Matplotlib Imshow
imshow() is a function in the Matplotlib library used to display images.
The imshow() function is commonly used to plot two-dimensional grayscale or color images.
The imshow() function can be used to plot matrices, heatmaps, maps, etc.
The syntax of the imshow() method is as follows:
imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, *, data=None, **kwargs)
**Parameter Description:**
* `X`: Input data. Can be a 2D array, 3D array, PIL image object, matplotlib path object, etc.
* `cmap`: Colormap. Used to control the colors corresponding to different values in the image. Can choose built-in colormaps such as `gray`, `hot`, `jet`, etc., or customize colormaps.
* `norm`: Used to control the normalization method of values. Can choose normalization methods such as `Normalize`, `LogNorm`, etc.
* `aspect`: Controls the aspect ratio of the image. Can be set to `auto` or a number.
* `interpolation`: Interpolation method. Used to control the smoothness and detail level of the image. Can choose interpolation methods such as `nearest`, `bilinear`, `bicubic`, etc.
* `alpha`: Image transparency. Range is 0~1.
* `origin`: Position of the coordinate axis origin. Can be set to `upper` or `lower`.
* `extent`: Controls the data range displayed. Can be set to `[xmin, xmax, ymin, ymax]`.
* `vmin`, `vmax`: Control the value range of the colormap.
* `filternorm and filterrad`: Used for image filtering objects. Can be set to `None`, `antigrain`, `freetype`, etc.
* `imlim`: Used to specify the image display range.
* `resample`: Used to specify the image resampling method.
* `url`: Used to specify the image link.
Here are some examples of using the imshow() function.
### Display Grayscale Image
## Example
import matplotlib.pyplot as plt
import numpy as np
# Generate a 2D random array
img = np.random.rand(10,10)
# Plot grayscale image
plt.imshow(img, cmap='gray')
# Display image
plt.show()
In the above example, we generated a 10x10 random array and used the imshow() function to display it as a grayscale image.
We set the cmap parameter to gray, which means the image will be displayed using the grayscale colormap.
The display result is as follows:
!(#)
### Display Color Image
## Example
import matplotlib.pyplot as plt
import numpy as np
# Generate a random color image
img = np.random.rand(10,10,3)
# Plot color image
plt.imshow(img)
# Display image
plt.show()
In the above example, we generated a 10x10 random color image and used the imshow() function to display it.
Since color images are 3D arrays, the cmap parameter does not need to be set.
The display result is as follows:
!(#)
### Display Heatmap
## Example
import matplotlib.pyplot as plt
import numpy as np
# Generate a 2D random array
data = np.random.rand(10,10)
# Plot heatmap
plt.imshow(data, cmap='hot')
# Display image
plt.colorbar()
plt.show()
In the above example, we generated a 10x10 random array and used the imshow() function to display it as a heatmap.
We set the cmap parameter to hot, which means the image will be displayed using the hot colormap.
In addition, we added a colorbar to view the relationship between data values and colors.
The display result is as follows:
!(#)
### Display Map
## Example
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
# Load map image, download address: https://static.jyshare.com/images/demo/map.jpeg
img = Image.open('map.jpg')
# Convert to array
data = np.array(img)
# Plot map
plt.imshow(data)
# Hide axes
plt.axis('off')
# Display image
plt.show()
In the above example, we loaded a map image and converted it to an array.
Then, we used the imshow() function to display it and used the axis('off') function to hide the axes for better map viewing.
The display result is as follows:
!(https://static.jyshare.com/images/demo/map.jpeg)
### Display Matrix
## Example
import matplotlib.pyplot as plt
import numpy as np
# Generate a random matrix
data = np.random.rand(10,10)
# Plot matrix
plt.imshow(data)
# Display image
plt.show()
In the above example, we generated a random matrix and used the imshow() function to display it as an image.
Since matrices are also 2D arrays, the imshow() function can be used to display them.
The display result is as follows:
!(#)
* * *
## More Examples
The following creates a 4x4 2D numpy array and displays it using three different imshow image presentations.
* The first shows the grayscale colormap without color blending.
* The second shows the image using the viridis colormap, also without color blending.
* The third shows the image using the viridis colormap and uses bicubic interpolation for color blending.
## Example
import matplotlib.pyplot as plt
import numpy as np
n =4
# Create an n x n 2D numpy array
a = np.reshape(np.linspace(0,1,n**2),(n,n))
plt.figure(figsize=(12,4.5))
# First image shows grayscale colormap without color blending
plt.subplot(131)
plt.imshow(a, cmap='gray', interpolation='nearest')
plt.xticks(range(n))
plt.yticks(range(n))
# Grayscale map, no blending
plt.title('Gray color map, no blending', y=1.02, fontsize=12)
# Second image shows viridis colormap without color blending
plt.subplot(132)
plt.imshow(a, cmap='viridis', interpolation='nearest')
plt.yticks([])
plt.xticks(range(n))
# Viridis map, no blending
plt.title('Viridis color map, no blending', y=1.02, fontsize=12)
# Third image shows viridis colormap with bicubic interpolation for color blending
plt.subplot(133)
plt.imshow(a, cmap='viridis', interpolation='bicubic')
plt.yticks([])
plt.xticks(range(n))
# Viridis map, bicubic blending
plt.title('Viridis color map, bicubic blending', y=1.02, fontsize=12)
plt.show()
The display result is as follows:
!(#)
YouTip