'In skimage, how to get cmap from one generated image, and use it in another image?

If I plot two images with cmap="gray":

  • on Im1 (left), the tile with value 0.1 is light grey
  • On Im2 (right), the tiles are all defined with value 0.1, but there are all black

So how do I obtain the same light grey on Im2 ?

import matplotlib.pyplot as plt
import numpy as np

Im1 = np.array([[0.1,0.2],[0.02,0.002]])
plt.subplot(1, 2, 1)
plt.imshow(Im1, cmap="gray")

Im2 = np.array([[0.1,0.1],[0.1,0.1]])
plt.subplot(1, 2, 2)
plt.imshow(Im2, cmap="gray")

plt.show()

enter image description here

Thank you



Solution 1:[1]

You probably want to use the same Normalize object on both subplots, like this:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize

Im1 = np.array([[0.1,0.2],[0.02,0.002]])
Im2 = np.array([[0.1,0.1],[0.1,0.1]])

_min = min(t.min() for t in [Im1, Im2])
_max = max(t.max() for t in [Im1, Im2])
norm = Normalize(vmin=_min, vmax=_max)

plt.subplot(1, 2, 1)
plt.imshow(Im1, cmap="gray", norm=norm)

plt.subplot(1, 2, 2)
plt.imshow(Im2, cmap="gray", norm=norm)

plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Davide_sd