hvplot
hvplot copied to clipboard
Categorical coloring fails on mixed polygon geometry in the Matplotlib backend
ALL software version info
(this library, plus any other relevant software, e.g. bokeh, python, notebook, OS, browser, etc should be added within the dropdown below.)
Software Version Info
hvPlot = 0.11.4.dev59+gd027d7e.d20250731
HoloViews = 1.21.0
Bokeh = 3.7.3
Geopandas = 1.1.1
hvsampledata = 0.1.4a3
Description of expected behavior and the observed behavior
When plotting a GeoDataFrame using hvplot.polygons(..., color='bea_region') with the Matplotlib backend, the following problems occur:
- The polygon fills are miscolored, and do not match the expected categories.
- A colorbar is rendered instead of a categorical legend, even though the colorbar itself maps the correct labels to colors.
- The same plot works correctly with the Bokeh backend, including color mapping and legend.
- The issue is not reproducible with small, manually created GeoDataFrames using dummy Polygon objects – categorical coloring works fine in those cases.
Complete, minimal, self-contained example code that reproduces the issue
Non-working example (real dataset)
import hvplot.pandas # noqa
import hvsampledata
hvplot.extension('matplotlib')
gdf = hvsampledata.us_states("geopandas")
gdf.hvplot.polygons(
geo=True,
color='bea_region',
title='US states colored by BEA region (Matplotlib)',
)
Working example (dummy GeoDataFrame) but with colorbar instead of legend
import geopandas as gpd
import hvplot.pandas # noqa
from shapely.geometry import Polygon
hvplot.extension('matplotlib')
polys = [
Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]),
Polygon([(1.2, 0), (2.2, 0), (2.2, 1), (1.2, 1)]),
Polygon([(2.4, 0), (3.4, 0), (3.4, 1), (2.4, 1)]),
Polygon([(0, 1.5), (1, 1.5), (1, 2.5), (0, 2.5)]),
Polygon([(1.2, 1.5), (2.2, 1.5), (2.2, 2.5), (1.2, 2.5)]),
Polygon([(2.4, 1.5), (3.4, 1.5), (3.4, 2.5), (2.4, 2.5)]),
]
regions = ['East', 'West', 'North', 'East', 'West', 'South']
gdf = gpd.GeoDataFrame({'geometry': polys, 'region': regions})
gdf['region'] = gdf['region'].astype('category')
gdf.hvplot.polygons(
color='region',
title='Dummy Polygon MRE (Matplotlib)',
)
Bokeh plot (Everything correct)
import hvplot.pandas # noqa
import hvsampledata
gdf = hvsampledata.us_states("geopandas")
gdf.hvplot.polygons(
geo=True,
color='bea_region',
title='US states colored by BEA region (Bokeh)',
)
Stack traceback and/or browser JavaScript console output
None
Screenshots or screencasts of the bug in action
See above images.