I'm using geopandas' explore
and trying to change colors depending on value using cmap
.
For example:
0 < value <= 2
then return 'red'
2 < value <= 4
then return 'green'
etc.Is it possible to do this using cmap
?
def my_colormap(value):
if value < 2:
return 'red'
else:
return 'black'
geodata.explore("xxxx", cmap= lambda value: my_colormap(value), vmax = 8)
Result:
ValueError: ... is not a valid value for name; supported values are 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', ...
You can use :
import numpy as np
conditions = [
geodata["value"].between(0, 2, inclusive="right"),
geodata["value"].between(2, 4, inclusive="right"),
geodata["value"].gt(4)
# add here more conditions
]
colors = ["red", "green", "black"] # add here more colors
geodata["color"] = np.select(conditions, colors, default="black")
geodata.explore(column="color", cmap=sorted(colors), legend=True)
Output :
Input used :
np.random.seed(1)
geodata = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
geodata["value"] = np.random.randint(0, 10, len(geodata))