1

When I try to plot a gray-scale image, sometimes the image is normalized by the min and max pixels in the image. Is there anyway to specify an absolute scale from 0 to 255.

We can see from the example below the pixel 240 is plotted very differently.

import numpy as np
from matplotlib import pyplot as plt

x = np.array([
        [255,255,255],
        [255,255, 255],
        [255, 240, 0]
        ])    
plt.imshow(x, cmap = "gray")

x = np.array([
        [255,255,255],
        [255,255, 255],
        [255, 240, 240]
        ])    
plt.imshow(x, cmap = "gray")
John
  • 1,779
  • 3
  • 25
  • 53

1 Answers1

2

Yes, use vmin and vmax kwargs to set the borders of your scaling

plt.imshow(x, cmap = "gray", vmin=0, vmax=255)

example:

enter image description here

import numpy as np
from matplotlib import pyplot as plt

fig, axs = plt.subplots(1, 2, figsize=(8, 3))

x = np.array([
        [255,255,255],
        [255,255, 255],
        [255, 240, 240]
        ])    
a = axs[0].imshow(x, cmap = "gray")
plt.colorbar(a, ax=axs[0])

x = np.array([
        [255,255,255],
        [255,255, 255],
        [255, 240, 240]
        ])    
a = axs[1].imshow(x, cmap = "gray", vmin=0, vmax=255)
plt.colorbar(a, ax=axs[1])
SpghttCd
  • 10,510
  • 2
  • 20
  • 25