color_bins_layer breaks argument should take in iterables
See original GitHub issueI tried passing a numpy array for the breaks argument of a color_bins_layer helper:
from cartoframes.viz.helpers import color_bins_layer
breaks = np.arange(
0,
np.log(counties_w_counts.properties_per_household.max()),
np.log(counties_w_counts.properties_per_household.max()) / 7,
)
Map(
[
color_bins_layer(
counties_w_counts[["geometry", "properties_per_household"]],
"properties_per_household",
stroke_color="transparent",
palette="sunset",
breaks=breaks,
),
Layer("SELECT * FROM mamataakella.state_bounds", "color: opacity(black, 0.5)"),
],
show_info=True,
viewport={"zoom": 3.15, "lat": 39.051997, "lng": -99.333419},
)
And received the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-308-147ad3fa75fa> in <module>
14 stroke_color="transparent",
15 palette="sunset",
---> 16 breaks=breaks,
17 ),
18 Layer("SELECT * FROM mamataakella.state_bounds", "color: opacity(black, 0.5)"),
~/.local/share/virtualenvs/property_data-_sgn6fy1/lib/python3.7/site-packages/cartoframes/viz/helpers/color_bins_layer.py in color_bins_layer(source, value, title, method, bins, breaks, palette, size, opacity, stroke_color, stroke_width, description, footer, legend, popup, widget, animate)
47 raise ValueError('Available methods are: "quantiles", "equal", "stdev".')
48
---> 49 func = 'buckets' if breaks else {
50 'quantiles': 'globalQuantiles',
51 'equal': 'globalEqIntervals',
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
My expectation is that breaks can be an ordered iterable (list, tuple, numpy array). It works fine with list and tuple, but the error above leads me to think that it’s testing for whether it’s a list or tuple (and not a string). Since numpy has such a common use in peoples workflows for calculating custom arrays, we should support it too. A the very least we need to improve the error message that it should be a list/tuple or one of the named strings.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
itertools — Functions creating iterators for efficient looping ...
It generates a break or new group every time the value of the key function changes (which is why it is usually necessary...
Read more >Iterables - Python Like You Mean It
Here are some useful built-in functions that accept iterables as arguments: list , tuple , dict , set : construct a list ...
Read more >When should I accept a parameter of Iterable<T> vs ...
You say it would be a "breaking change." What would break if (say) the ArrayList<T> constructor parameter was changed from Collection<T> to ...
Read more >21. Iterables and iterators - Exploring JS
A value doesn't have to be an object in order to be iterable. That's because all values are coerced to objects before the...
Read more >API Reference — more-itertools 9.0.0 documentation
By the default, the last yielded list will have fewer than n elements if the ... Break iterable into sub-iterables with n elements...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the response, @makella! Yeah, looks like she has the same issue of needing to convert it to a python list first.
I used it because I wanted clear, evenly spaced values for the percentages. I could’ve tried
method='equal'
andbins=10
I guess but it felt easier to generate the bins I know I want ahead of time.Just tried the way you suggest:
More or less the same except for the
<
and>
for the lower/upper values.