How to extract colors from branca colormap using Python?
Question:
Answers:
The source code on GitHub shows that to_step
returns a StepColormap
object. This object (line 500 in the linked code) shows that StepColormap has a colors
attribute. You should be able to index through the colors that way:
import branca
color_map = branca.colormap.linear.PuRd_09.scale(0, 250)
colormap = color_map.to_step(index=[0, 10, 20, 50, 70, 90, 120, 200])
for color in colormap.colors:
print(color)
You can use matplotlib.colors.to_hex
on colormap.colors
:
from matplotlib.colors import to_hex
out = [to_hex(c) for c in colormap.colors]
# or
out = list(map(to_hex, colormap.colors))
Output:
['#f7f4f9', '#f0ebf4', '#e3d9eb', '#d0aad2', '#d084bf', '#e44199', '#67001f']
The source code on GitHub shows that to_step
returns a StepColormap
object. This object (line 500 in the linked code) shows that StepColormap has a colors
attribute. You should be able to index through the colors that way:
import branca
color_map = branca.colormap.linear.PuRd_09.scale(0, 250)
colormap = color_map.to_step(index=[0, 10, 20, 50, 70, 90, 120, 200])
for color in colormap.colors:
print(color)
You can use matplotlib.colors.to_hex
on colormap.colors
:
from matplotlib.colors import to_hex
out = [to_hex(c) for c in colormap.colors]
# or
out = list(map(to_hex, colormap.colors))
Output:
['#f7f4f9', '#f0ebf4', '#e3d9eb', '#d0aad2', '#d084bf', '#e44199', '#67001f']