geoplot icon indicating copy to clipboard operation
geoplot copied to clipboard

kdeplot not working when fill=True

Open jose-vines-toteat opened this issue 1 year ago • 3 comments

Hi, I tried reproducing your example in the documentation, and I get this error when using kdeplot with fill=True

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[104], line 3
      1 # ax = gplt.polyplot(gdf_cl[gdf_cl.Region == 'Región Metropolitana de Santiago'], projection=gcrs.WebMercator(), zorder=1)
      2 ax = gplt.webmap(gdf_cl[gdf_cl.Region == 'Región Metropolitana de Santiago'], projection=gcrs.WebMercator(), zorder=1)
----> 3 gplt.kdeplot(view[['geometry', 'sale']], cmap='viridis', ax=ax, thresh=.05, legend=True, fill=True)

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/geoplot/geoplot.py:1320, in kdeplot(df, projection, extent, figsize, ax, clip, **kwargs)
   1315         return ax
   1317 plot = KDEPlot(
   1318     df, projection=projection, extent=extent, figsize=figsize, ax=ax, clip=clip, **kwargs
   1319 )
-> 1320 return plot.draw()

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/geoplot/geoplot.py:1304, in kdeplot.<locals>.KDEPlot.draw(self)
   1301     return ax
   1303 if self.projection:
-> 1304     sns.kdeplot(
   1305         x=pd.Series([p.x for p in self.df.geometry]),
   1306         y=pd.Series([p.y for p in self.df.geometry]),
   1307         transform=ccrs.PlateCarree(), ax=ax, **self.kwargs
   1308     )
   1309 else:
   1310     sns.kdeplot(
   1311         x=pd.Series([p.x for p in self.df.geometry]),
   1312         y=pd.Series([p.y for p in self.df.geometry]),
   1313         ax=ax, **self.kwargs
   1314     )

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/seaborn/distributions.py:1682, in kdeplot(data, x, y, hue, weights, palette, hue_order, hue_norm, color, fill, multiple, common_norm, common_grid, cumulative, bw_method, bw_adjust, warn_singular, log_scale, levels, thresh, gridsize, cut, clip, legend, cbar, cbar_ax, cbar_kws, ax, **kwargs)
   1679 p._attach(ax, allowed_types=["numeric", "datetime"], log_scale=log_scale)
   1681 method = ax.fill_between if fill else ax.plot
-> 1682 color = _default_color(method, hue, color, kwargs)
   1684 if not p.has_xy_data:
   1685     return ax

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/seaborn/utils.py:159, in _default_color(method, hue, color, kws, saturation)
    156 elif method.__name__ == "fill_between":
    158     kws = _normalize_kwargs(kws, mpl.collections.PolyCollection)
--> 159     scout = method([], [], **kws)
    160     facecolor = scout.get_facecolor()
    161     color = to_rgb(facecolor[0])

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/matplotlib/__init__.py:1465, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1462 @functools.wraps(func)
   1463 def inner(ax, *args, data=None, **kwargs):
   1464     if data is None:
-> 1465         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1467     bound = new_sig.bind(ax, *args, **kwargs)
   1468     auto_label = (bound.arguments.get(label_namer)
   1469                   or bound.kwargs.get(label_namer))

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/matplotlib/axes/_axes.py:5509, in Axes.fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
   5507 def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
   5508                  step=None, **kwargs):
-> 5509     return self._fill_between_x_or_y(
   5510         "x", x, y1, y2,
   5511         where=where, interpolate=interpolate, step=step, **kwargs)

File ~/mambaforge/envs/ds/lib/python3.9/site-packages/matplotlib/axes/_axes.py:5500, in Axes._fill_between_x_or_y(self, ind_dir, ind, dep1, dep2, where, interpolate, step, **kwargs)
   5498 up_x = up_y = True
   5499 if "transform" in kwargs:
-> 5500     up_x, up_y = kwargs["transform"].contains_branch_seperately(self.transData)
   5501 self.update_datalim(pts, updatex=up_x, updatey=up_y)
   5503 self.add_collection(collection, autolim=False)

AttributeError: 'PlateCarree' object has no attribute 'contains_branch_seperately'

jose-vines-toteat avatar Jan 02 '24 16:01 jose-vines-toteat

I can confirm this issue, it is an incompatibility with matplotlib 3.8. Until it is fixed, you can install matplotlib 3.7 (pip install "matplotlib<3.8") to be able to plot with fill=True

tfardet avatar Jan 22 '24 15:01 tfardet

I too confirm this issue but could not resolve it by installing matplotlib<3.8 neither by installing matplotlib==3.7

LFK01 avatar May 28 '24 23:05 LFK01

@ResidentMario , are you still accepting PRs for this repository?

I've investigated this issue and have a fix that can work. It is due to updates in seaborn as they pass in the kwargs through another function first which can't process a cartopy transform, and only accepts matplotlib.transform.

I've detailed my findings here: https://github.com/mwaskom/seaborn/issues/3694#issuecomment-2155162194

The work around I proposed to OP in that issue would also work here.

I noted this repository hasn't had any commits in a long time, may I be able to assist?

nicholas-ys-tan avatar Jun 07 '24 16:06 nicholas-ys-tan