python-visualization / branca

This library is a spinoff from folium, that would host the non-map-specific features.
https://python-visualization.github.io/branca/
MIT License
111 stars 63 forks source link

allow colorbar ticks at arbitrary position #113

Closed kota7 closed 1 year ago

kota7 commented 1 year ago

This extend the ColorMap and its successors LinearColormap and StepColormap so that one may specify the positions of ticks to display independently of the index parameter. Currently the index parameter is used both to determine the color scale and as the location of the legend labels. It is possible that the two roles are better to be separated. For example, one decided to split colors by the percentile values of the data, which would result in any numeric values. However, for the visualization purpose, numbers shown in the color bar are better to be "good" numbers such as 0, 50, 100. For example, this discussion is related to this feature: https://github.com/python-visualization/folium/issues/1374.

This PR enables this feature by an adding additional argument tick_labels to LinearColormap and StepColormap, which is used as the tick positions. The default value is None, for which the current behavior is maintained. This logic is implemented in the ColorMap.render method.

In addition the LinearColormap.to_step method is updated so that the tick_labels attribute is maintained to the result.

Examples:

import folium
from branca.colormap import LinearColormap

colormap = LinearColormap(["blue", "yellow", "red"], index=[1, 10, 100], vmin=1, vmax=100,
                          tick_labels=[30, 80, 150])
m = folium.Map(location=[48, -102], zoom_start=3)
colormap.add_to(m)
m

Screenshot_2022-08-16_01-44-14

import folium
from branca.colormap import StepColormap

colormap = StepColormap(["blue", "yellow", "red"], index=[1, 10, 100], vmin=1, vmax=100,
                        tick_labels=[30, 80, 150])
m = folium.Map(location=[48, -102], zoom_start=3)
colormap.add_to(m)
m

Screenshot_2022-08-16_01-43-55

Conengmo commented 1 year ago

Perfect! Thank you so much.