Giter Site home page Giter Site logo

Geographic Filled KDE Plot about seaborn HOT 5 OPEN

bschweigert avatar bschweigert commented on September 27, 2024
Geographic Filled KDE Plot

from seaborn.

Comments (5)

mwaskom avatar mwaskom commented on September 27, 2024

hm this smells like something between matplotlib and cartopy, that's not a function seaborn is calling directly. can you share the full traceback?

from seaborn.

bschweigert avatar bschweigert commented on September 27, 2024

Here is the full traceback:

File ~\Miniconda3\envs\figs\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)

File c:\users\bschweigert2\desktop\lm clim\error_ex.py:28
kde = sns.kdeplot(x = lons, y = lats, fill = True, transform = ccrs.PlateCarree(), zorder = 1)

File ~\Miniconda3\envs\figs\Lib\site-packages\seaborn\distributions.py:1682 in kdeplot
color = _default_color(method, hue, color, kwargs)

File ~\Miniconda3\envs\figs\Lib\site-packages\seaborn\utils.py:136 in _default_color
scout = method([], [], **kws)

File ~\Miniconda3\envs\figs\Lib\site-packages\matplotlib_init_.py:1478 in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)

File ~\Miniconda3\envs\figs\Lib\site-packages\matplotlib\axes_axes.py:5509 in fill_between
return self._fill_between_x_or_y(

File ~\Miniconda3\envs\figs\Lib\site-packages\matplotlib\axes_axes.py:5500 in _fill_between_x_or_y
up_x, up_y = kwargs["transform"].contains_branch_seperately(self.transData)

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

That would be intriguing, because I have done plenty of plotting recently with matplotlib and cartopy with no issues thus far. Thanks for the help!

from seaborn.

mwaskom avatar mwaskom commented on September 27, 2024

Based on that it seems like you could reproduce this with something like

f, ax = plt.subplots()
ax.fill_between([], [], transform=ccrs.PlateCarree())

(untested as I don't have the relevant geographic dependencies)

from seaborn.

mwaskom avatar mwaskom commented on September 27, 2024

You may need to set up the subplot with the same projection too, I am not sure.

from seaborn.

nicholas-ys-tan avatar nicholas-ys-tan commented on September 27, 2024

@bschweigert , I've been looking into this one too.

I compared to code from 0.9 branch (I saw it was working from someone on stack exchange in 2020 so picked the branch of that year).

Back then the kwargs being fed into seaborn.distributions.kdeplot went straight to _bivariate_kdeplot_ or _univariate_kdeplot, which later feeds the kwargs into ax.contourf or ax.contour which I believe is managed by matplotlib.

However, somewhere along the way (about 3 years ago), this was added before the kwargs can get to plot_univariate_density or plot_bivariate_density

color = _default_color(method, hue, color, kwargs)

It consequently tries to pass in the transform kwarg into the fill_between method in matplotlib - the tricky bit is that fill_between does accept the transform kwarg, except it must be a matplotlib.transform type!

I am not sure if you might still encounter the error with a univariate density plot with fill=True because it does look like it can calll fill_between. I haven't tested/looked further into this.

But it seems to work fine on a bivariate density maybe because contourf supports the cartopy projection? transform is an argument for matplotlib.contour.ContourSet and seems to accept a wide range of transformation types based on the get_transform function. This function converts the transform variable to a matplotlib.Transform type.

A workaround for you to continue forward is to do this:

 kde = sns.kdeplot(x = lons, y = lats, fill = True, transform = ccrs.PlateCarree()._as_mpl_transform(ax), zorder = 1)

as cartopy projections have a (private) method to convert to matplotlib transform types.

Whether this is a bug by seaborn, or by matplotlib - I don't know. Maybe matplotlib should convert all transforms to a matplotlib type first in fill_between, or maybe seaborn should be doing the conversion - or maybe transform type should be first checked to prevent passing into _default_color() as I don't think it's doing much with that information anyway.

@mwaskom , let me know if you would like to me to raise a PR to address this if one of the suggestions is something you would want to implement in seaborn.

from seaborn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.