|
| 1 | +# Using `folium.colormap` |
| 2 | + |
| 3 | +**A few examples of how to use `folium.colormap` in choropleths.** |
| 4 | + |
| 5 | +Let's load a GeoJSON file, and try to choropleth it. |
| 6 | + |
| 7 | +```{code-cell} ipython3 |
| 8 | +import json |
| 9 | +
|
| 10 | +import folium |
| 11 | +import pandas as pd |
| 12 | +import requests |
| 13 | +
|
| 14 | +
|
| 15 | +url = ( |
| 16 | + "https://raw.githubusercontent.com/python-visualization/folium/main/examples/data" |
| 17 | +) |
| 18 | +us_states = f"{url}/us-states.json" |
| 19 | +US_Unemployment_Oct2012 = f"{url}/US_Unemployment_Oct2012.csv" |
| 20 | +
|
| 21 | +geo_json_data = json.loads(requests.get(us_states).text) |
| 22 | +unemployment = pd.read_csv(US_Unemployment_Oct2012) |
| 23 | +
|
| 24 | +unemployment_dict = unemployment.set_index("State")["Unemployment"] |
| 25 | +``` |
| 26 | + |
| 27 | +## Self-defined |
| 28 | + |
| 29 | +You can build a choropleth in using a self-defined function. |
| 30 | +It has to output an hexadecimal color string of the form `#RRGGBB` or `#RRGGBBAA`. |
| 31 | + |
| 32 | +```{code-cell} ipython3 |
| 33 | +def my_color_function(feature): |
| 34 | + """Maps low values to green and high values to red.""" |
| 35 | + if unemployment_dict[feature["id"]] > 6.5: |
| 36 | + return "#ff0000" |
| 37 | + else: |
| 38 | + return "#008000" |
| 39 | +``` |
| 40 | + |
| 41 | +```{code-cell} ipython3 |
| 42 | +m = folium.Map([43, -100], tiles="cartodbpositron", zoom_start=4) |
| 43 | +
|
| 44 | +folium.GeoJson( |
| 45 | + geo_json_data, |
| 46 | + style_function=lambda feature: { |
| 47 | + "fillColor": my_color_function(feature), |
| 48 | + "color": "black", |
| 49 | + "weight": 2, |
| 50 | + "dashArray": "5, 5", |
| 51 | + }, |
| 52 | +).add_to(m) |
| 53 | +
|
| 54 | +m |
| 55 | +``` |
| 56 | + |
| 57 | +## StepColormap |
| 58 | + |
| 59 | +But to help you define your colormap, we've embedded `StepColormap` in `folium.colormap`. |
| 60 | + |
| 61 | +You can simply define the colors you want, and the `index` (*thresholds*) that correspond. |
| 62 | + |
| 63 | +```{code-cell} ipython3 |
| 64 | +import branca.colormap as cm |
| 65 | +
|
| 66 | +step = cm.StepColormap( |
| 67 | + ["green", "yellow", "red"], vmin=3, vmax=10, index=[3, 4, 8, 10], caption="step" |
| 68 | +) |
| 69 | +
|
| 70 | +step |
| 71 | +``` |
| 72 | + |
| 73 | +```{code-cell} ipython3 |
| 74 | +m = folium.Map([43, -100], tiles="cartodbpositron", zoom_start=4) |
| 75 | +
|
| 76 | +folium.GeoJson( |
| 77 | + geo_json_data, |
| 78 | + style_function=lambda feature: { |
| 79 | + "fillColor": step(unemployment_dict[feature["id"]]), |
| 80 | + "color": "black", |
| 81 | + "weight": 2, |
| 82 | + "dashArray": "5, 5", |
| 83 | + }, |
| 84 | +).add_to(m) |
| 85 | +
|
| 86 | +m |
| 87 | +``` |
| 88 | + |
| 89 | +If you specify no index, colors will be set uniformly. |
| 90 | + |
| 91 | +```{code-cell} ipython3 |
| 92 | +cm.StepColormap(["r", "y", "g", "c", "b", "m"]) |
| 93 | +``` |
| 94 | + |
| 95 | +## LinearColormap |
| 96 | + |
| 97 | +But sometimes, you would prefer to have a *continuous* set of colors. This can be done by `LinearColormap`. |
| 98 | + |
| 99 | +```{code-cell} ipython3 |
| 100 | +linear = cm.LinearColormap(["green", "yellow", "red"], vmin=3, vmax=10) |
| 101 | +
|
| 102 | +linear |
| 103 | +``` |
| 104 | + |
| 105 | +```{code-cell} ipython3 |
| 106 | +m = folium.Map([43, -100], tiles="cartodbpositron", zoom_start=4) |
| 107 | +
|
| 108 | +folium.GeoJson( |
| 109 | + geo_json_data, |
| 110 | + style_function=lambda feature: { |
| 111 | + "fillColor": linear(unemployment_dict[feature["id"]]), |
| 112 | + "color": "black", |
| 113 | + "weight": 2, |
| 114 | + "dashArray": "5, 5", |
| 115 | + }, |
| 116 | +).add_to(m) |
| 117 | +
|
| 118 | +m |
| 119 | +``` |
| 120 | + |
| 121 | +Again, you can set the `index` if you want something irregular. |
| 122 | + |
| 123 | +```{code-cell} ipython3 |
| 124 | +cm.LinearColormap(["red", "orange", "yellow", "green"], index=[0, 0.1, 0.9, 1.0]) |
| 125 | +``` |
| 126 | + |
| 127 | +If you want to transform a linear map into a *step* one, you can use the method `to_step`. |
| 128 | + |
| 129 | +```{code-cell} ipython3 |
| 130 | +linear.to_step(6) |
| 131 | +``` |
| 132 | + |
| 133 | +You can also use more sophisticated rules to create the thresholds. |
| 134 | + |
| 135 | +```{code-cell} ipython3 |
| 136 | +linear.to_step( |
| 137 | + n=6, |
| 138 | + data=[30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100], |
| 139 | + method="quantiles", |
| 140 | + round_method="int", |
| 141 | +) |
| 142 | +``` |
| 143 | + |
| 144 | +And the opposite is also possible with `to_linear`. |
| 145 | + |
| 146 | +```{code-cell} ipython3 |
| 147 | +step.to_linear() |
| 148 | +``` |
| 149 | + |
| 150 | +## Built-in |
| 151 | + |
| 152 | +For convenience, we provide a (small) set of built-in linear colormaps, in `folium.colormap.linear`. |
| 153 | + |
| 154 | +```{code-cell} ipython3 |
| 155 | +cm.linear.OrRd_09 |
| 156 | +``` |
| 157 | + |
| 158 | +You can also use them to generate regular `StepColormap`. |
| 159 | + |
| 160 | +```{code-cell} ipython3 |
| 161 | +cm.linear.PuBuGn_09.to_step(12) |
| 162 | +``` |
| 163 | + |
| 164 | +Of course, you may need to scale the colormaps to your bounds. This is doable with `.scale`. |
| 165 | + |
| 166 | +```{code-cell} ipython3 |
| 167 | +cm.linear.YlGnBu_09.scale(3, 12) |
| 168 | +``` |
| 169 | + |
| 170 | +```{code-cell} ipython3 |
| 171 | +cm.linear.RdGy_11.to_step(10).scale(5, 100) |
| 172 | +``` |
| 173 | + |
| 174 | +At last, if you want to check them all, simply ask for `linear` in the notebook. |
| 175 | + |
| 176 | +```{code-cell} ipython3 |
| 177 | +cm.linear |
| 178 | +``` |
| 179 | + |
| 180 | +## Draw a `ColorMap` on a map |
| 181 | + |
| 182 | +By the way, a ColorMap is also a Folium `Element` that you can draw on a map. |
| 183 | + |
| 184 | +```{code-cell} ipython3 |
| 185 | +m = folium.Map(tiles="cartodbpositron") |
| 186 | +
|
| 187 | +colormap = cm.linear.Set1_09.scale(0, 35).to_step(10) |
| 188 | +colormap.caption = "A colormap caption" |
| 189 | +m.add_child(colormap) |
| 190 | +
|
| 191 | +m |
| 192 | +``` |
0 commit comments