Yes, correct. In the first case, you extract a 2D array from the image and imshow
by default applies a standard colormap, viridis
afaik, which in fact is greenish. This is appropriate for data arrays which are supposed to be displayed as a heatmap, but not for pictures.
On the other hand, a 3D array, i.e. an array with RGB values is automatically interpreted as an image by imshow
. So if in this case only the first component is different to 0
, you'll get a red picture.
However, you can force imshow
to use a colormap of your choice for 2D arrays by the cmap kwarg:
plt.imshow(img_name, cmap=cm_name)
with cm_name being one of e.g. 'Greys'
or 'Greys_r'
to get a grayscale representation of the image (or the R-channel in your case). And of course there are also 'Reds'
and 'Reds_r'
available - guess what...
For a complete list just put a typo in the cm_name - the error message is quite verbose and informative:
Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r
To show the differences, here a sample plot:
created with the following code:
import imageio
import matplotlib.pyplot as plt
import numpy as np
im = imageio.imread('imageio:chelsea.png')
im_r = np.zeros(np.shape(im))
im_r[:, :, 0] = im[:, :, 0]
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].imshow(im[:, :, 0])
axs[1, 0].imshow(im[:, :, 0], cmap='Greys_r')
axs[0, 1].imshow(im[:, :, 0], cmap='Reds_r')
axs[1, 1].imshow(im_r.astype(int))
axs[0, 0].set_title('pure imshow of 2D-array (R-channel)')
axs[1, 0].set_title('imshow of 2D-array with cmap="Grey_r"')
axs[0, 1].set_title('imshow of 2D-array with cmap="Reds_r"')
axs[1, 1].set_title('imshow of 3D-array with coordinates 1 and 2 \n(i.e.: channels G and B) set to 0')
plt.tight_layout()