Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting just a single rgb color in matplotlib

I am running matplotlib and I want to plot a single color, like:

import matplotlib.pyplot as plt
plt.imshow([(100, 100, 200)])

but this is showing a gradient. What is the issue?

like image 215
Rob Avatar asked Oct 28 '25 17:10

Rob


1 Answers

This gives only one color:

import matplotlib.pyplot as plt
plt.imshow([[(0, 0, 1)]])

enter image description here

plt.imshow([[(0.5, 0.5, 0.5)]])

enter image description here

You need a shape of MxNx3:

>>> import numpy as np
>>> np.array([[(0.5, 0.5, 0.5)]]).shape
(1, 1, 3)

Here M and N are 1.

plt.imshow(X, ...)

X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4) Display the image in X to current axes. X may be a float array, a uint8 array or a PIL image. If X is an array, it can have the following shapes:

  • MxN -- luminance (grayscale, float array only)
  • MxNx3 -- RGB (float or uint8 array)
  • MxNx4 -- RGBA (float or uint8 array)

    The value for each component of MxNx3 and MxNx4 float arrays should be in the range 0.0 to 1.0; MxN float arrays may be normalized.

You need to convert int to float between 0 and 1:

from __future__ import division  # needed for Python 2 only

plt.imshow([[(100 / 255, 100 / 255, 200 / 255)]])

enter image description here

like image 75
Mike Müller Avatar answered Oct 31 '25 08:10

Mike Müller