hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

Categorical coloring fails on mixed polygon geometry in the Matplotlib backend

Open Azaya89 opened this issue 4 months ago • 4 comments

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:

  1. The polygon fills are miscolored, and do not match the expected categories.
  2. A colorbar is rendered instead of a categorical legend, even though the colorbar itself maps the correct labels to colors.
  3. The same plot works correctly with the Bokeh backend, including color mapping and legend.
  4. 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)',
)
Image

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)',
)
Image

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)',
)
Image

Stack traceback and/or browser JavaScript console output

None

Screenshots or screencasts of the bug in action

See above images.

Azaya89 avatar Jul 31 '25 18:07 Azaya89