Skip to content

plot_kde is not applying contour_kwargs and contourf_kwargs as expected #2450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bersavosh opened this issue Apr 22, 2025 · 1 comment
Open

Comments

@bersavosh
Copy link

Describe the bug
I noticed two possible issues with plot_kde when plotting 2D data:
1- it seems levels (hdi_probs) are only applied to filled contours (contourf) and not line contours (contour). However I am not sure if this is an expected behavior and my misunderstanding, as it seems prevalent in examples.
2- filled contour seems not to apply the passed transparency keyword alpha, while contour lines do.,

To Reproduce
Both of these issues are visible in the examples on the plot_kde documentation page and reproducible by the example provided there. Particularly:

# Example from the documentation page
import arviz as az
non_centered = az.load_arviz_data('non_centered_eight')
mu_posterior = np.concatenate(non_centered.posterior["mu"].values)
tau_posterior = np.concatenate(non_centered.posterior["tau"].values)

# Not applying `hdi_probs`:
az.plot_kde(mu_posterior, values2=tau_posterior, hdi_probs=[0.393, 0.865, 0.989])

# Ignoring alpha for contourf
az.plot_kde(mu_posterior, values2=tau_posterior, hdi_probs=[0.393, 0.865, 0.989], contourf_kwargs={'alpha':0.0})

Image

Expected behavior
Regarding issue 1: I expected the contour lines to match the edge of filled contours. I wonder if I'm misunderstanding how it should behave.
Regarding issue 2: As indicated in the documentation, I expected the resulting plot with contourf_kwargs={'alpha':0.0} to only have contour lines and no contour filling.

Additional context
Encountered with Arviz 0.21.0 and matplotlib 3.10.0. Presence of this issue in the online examples may suggest I have misunderstood expected behavior or that the issues are perhaps unlikely to be caused by my specific system setup.

Thanks!

@OriolAbril
Copy link
Member

Thanks for reporting. IIRC, hdi_probs gets converted to contour values and added to the contour and contourf kwarg dicts. From the behaviour I am guessing this update step is somehow broken and only has an effect on contourf kwarg dict while at the same time instead of being added they overwrite the existing dict for contourf kwargs so it ends up containing only the requested hdi_probs as contour levels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants