Closed Gustacro closed 1 year ago
@Gustacro can you place here all the output error? I think that what is creating your error is the Choropleth default value of fill_color=blue.
From Choropleth docstring:
fill_color: string, default 'blue'
Area fill color. Can pass a hex code, color name, or if you are
binding data, one of the following color brewer palettes:
'BuGn', 'BuPu', 'GnBu', 'OrRd', 'PuBu', 'PuBuGn', 'PuRd', 'RdPu',
'YlGn', 'YlGnBu', 'YlOrBr', and 'YlOrRd'.
But in fact, currently, only the brewer palettes codes are working. Passing a color hex code gives the same error.
the full error message is this: `--------------------------------------------------------------------------- ValueError Traceback (most recent call last)
If you use one of the color brew palettes does it work?
'BuGn', 'BuPu', 'GnBu', 'OrRd', 'PuBu', 'PuBuGn', 'PuRd', 'RdPu', 'YlGn', 'YlGnBu', 'YlOrBr', and 'YlOrRd'
Choropleth(fill_color='BuGn')
I'm thinking maybe we should recover automatically, with a warning, if somebody provides data but doesn't use a color brew palette instead of raising an exception. Or the error message should contain some hint on how to resolve.
@Conengmo it works. Looks like that Choropleth only accepts brew pallets codes. I went back to see how it was done in early versions, and the line of code that breaks now, is the same as it was back on versions like 2 or 3.
if data is not None and not color_brewer(fill_color):
raise ValueError('Please pass a valid color brewer code to '
'fill_local. See docstring for valid codes.')
Also, "fill_local" in the error message should be "fill_color", right?
@Gustacro, you are using the style_function parameter when in reality looks like that is not supported by Choropleth.
Looks like that Choropleth only accepts brew pallets codes.
If you provide a dataframe it only accepts those. Otherwise a 'normal' color works.
Also, "fill_local" in the error message should be "fill_color", right?
Indeed. But, that error message doesn't trigger because an error is already raised in color_brewer()
. Maybe use a try/except here and include the list of valid codes in the error message? What do you think?
Ahh okay, now I tried without passing data and it works with color "blue". So we should alert on the error message what codes are available and why it doesn't work with a color name or hex code.
Here would you include the try/except block? If we do it here: https://github.com/python-visualization/folium/blob/f05f3ce9b3d71f87a0738f3c8c1236b933e497a9/folium/features.py#L1046-L1048
it will break again here: https://github.com/python-visualization/folium/blob/f05f3ce9b3d71f87a0738f3c8c1236b933e497a9/folium/features.py#L1088
What do you think that is better, break the code and give an error message with more information, or do a try/except block, alert the user with a warning and generate a fill_color with a brew pallet code? Something like this:
try:
if data is not None and not color_brewer(fill_color):
pass
except Exception as e:
print(e)
fill_color = "PuBu"
warnings.warn(
[error message and why]
Please choose one from the list below:\n'
'["Spectral", "RdYlGn", "PuBu", (...)]\n'
'"PuBu" brew color pallet was setup automatically for you.')
Another question, do you think that would be harder import the style_function from GeoJson to Choropleth?
@Conengmo it works. Looks like that Choropleth only accepts brew pallets codes. I went back to see how it was done in early versions, and the line of code that breaks now, is the same as it was back on versions like 2 or 3.
if data is not None and not color_brewer(fill_color): raise ValueError('Please pass a valid color brewer code to ' 'fill_local. See docstring for valid codes.')
Also, "fill_local" in the error message should be "fill_color", right?
@Gustacro, you are using the style_function parameter when in reality looks like that is not supported by Choropleth.
@Conengmo, I did at first, using a custom color ramp function (my_colors), didn't know that it wasn't supported by Choropleth, then I used threshold scale and colorbrewer palette. It would be useful if Choropleth support style_function to be able to use custom color ramp.
Pretty sure the original issue has been solved by https://github.com/python-visualization/folium/pull/1288
Please add a code sample or a nbviewer link, copy-pastable if possible
Problem description
I'm trying to display a thematic map with folium using values from a column and a function (color ramp) but I'm getting ValueError: blue is not a valid ColorBrewer code as result.
[this should explain why the current behavior is a problem and what is the expected output.]
Expected Output
I'm expecting to get a map with specific color based on a column ['drv_tm'] values and function that contain the color ramp
Output of
folium.__version__
0.8.3