Extract Matplotlib colormap in hex-format

MatplotlibServer Side ProgrammingProgramming

To extract matplotlib colormap in hex-format, we can take the following steps −

  • Get the rainbow color map.

  • Iterate in the range of rainbow colormap length.

  • Using rgb2hex method, convert rgba tuple to a hexadecimal representation of a color.

Example

from matplotlib import cm
import matplotlib

cmap = cm.rainbow
for i in range(cmap.N):
   rgba = cmap(i)
   print("Hexadecimal representation of rgba:{} is {}".format(rgba, matplotlib.colors.rgb2hex(rgba)))

Output

...............
........................
....................................
Hexadecimal representation of rgba:(1.0, 0.3954512068705424, 0.2018824091570102,
1.0) is #ff6533
Hexadecimal representation of rgba:(1.0, 0.38410574917192575, 0.1958454670071669,
1.0) is #ff6232
Hexadecimal representation of rgba:(1.0, 0.37270199199091436,
0.18980109344182594, 1.0) is #ff5f30
.........................................................
raja
Published on 06-May-2021 13:59:00
Advertisements