Handling Color Overflow in Brewer Palettes#
ColorBrewer palettes have a limited number of colors (typically 8–12).
When your data has more categories than the palette provides, a larger palette is created either by interpolating the original colors, or by cycling, or by generating additional colors.
By default, qualitative palettes use generate (creating new colors with varied luminance),
while sequential and diverging palettes use interpolate.
The overflow parameter controls the way extended palette is generated: ‘interpolate’, ‘cycle’, or ‘generate’.
import string
from lets_plot import *
LetsPlot.setup_html()
letters = list(string.ascii_uppercase)
data = {'letter': letters, 'x': list(range(len(letters)))}
# Default (generate for qualitative)
p = ggplot(data, aes(x='x', color='letter')) + geom_point(size=7) + guides(color=guide_legend(nrow=9))
Default: ‘generated’#
Additional colors are created based on the original palette colors.
p + scale_color_brewer(palette='Set1')
Cycle Palette Colors#
p + scale_color_brewer(palette='Set1', overflow='cycle')
Interpolate Palette Colors#
p + scale_color_brewer(palette='Set1', overflow='i')
Choosing the Right Palette for Many Categories#
When displaying a large number of categories (like countries on a world map), not all Brewer palettes work equally well with the generate overflow.
Palettes with high contrast colors like Set1 can produce jarring results when extended.
Softer palettes like Pastel1 work better, while Paired and Set3 tend to produce the most visually balanced results for large categorical datasets.
from lets_plot.geo_data import *
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).
countries = geocode_countries().get_boundaries()
display(countries)
| country | found name | geometry | |
|---|---|---|---|
| 0 | Andorra | Andorra | MULTIPOLYGON (((1.72589 42.5027, 1.41358 42.53... |
| 1 | Slovakia | Slovakia | MULTIPOLYGON (((16.9402 48.61654, 18.85091 49.... |
| 2 | Austria | Austria | MULTIPOLYGON (((9.60705 47.06077, 9.53075 47.2... |
| 3 | Hungary | Hungary | MULTIPOLYGON (((16.11389 46.86906, 17.16078 48... |
| 4 | Georgia | Georgia | MULTIPOLYGON (((41.54705 41.52069, 41.43971 42... |
| ... | ... | ... | ... |
| 213 | Chad | Chad | MULTIPOLYGON (((14.08333 13.08333, 13.63338 13... |
| 214 | Bosnia and Herzegovina | Bosnia and Herzegovina | MULTIPOLYGON (((17.58097 42.93833, 15.77656 45... |
| 215 | Norway | Norway | MULTIPOLYGON (((-7.92853 71.14587, -8.43795 70... |
| 216 | Morocco | Morocco | MULTIPOLYGON (((-5.31132 35.89622, -5.31163 35... |
| 217 | Sahrawi Arab Democratic Republic | Sahrawi Arab Democratic Republic | MULTIPOLYGON (((-17.04976 20.77011, -17.06318 ... |
218 rows × 3 columns
world = (ggplot()
+ geom_map(aes(fill='country'), data=countries, color='paper')
+ ggsize(800, 600)
+ ggtb()
)
The Default Qualitative Palette (Set1)#
Works, but not ideal for many categories.
world + scale_fill_brewer()
Pastel1#
A softer palette that works better with many categories.
world + scale_fill_brewer(palette='Pastel1')
Paired and Set3#
These palettes produce the most balanced results for large categorical datasets.
world + scale_fill_brewer(palette='Paired')
world + scale_fill_brewer(palette='Set3')