Skip to content
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

2kde use hdi_probs by default #2383

Merged
merged 2 commits into from
Sep 25, 2024
Merged

2kde use hdi_probs by default #2383

merged 2 commits into from
Sep 25, 2024

Conversation

aloctavodia
Copy link
Contributor

@aloctavodia aloctavodia commented Sep 16, 2024

Closes #2382

An "extra" contour is plotted by default, when passing hdi_probs or when passing an integer to levels. This little change "fix" the issue. Not entirely happy with the solution


📚 Documentation preview 📚: https://arviz--2383.org.readthedocs.build/en/2383/

@OriolAbril
Copy link
Member

I agree with changing the behaviour with hdi_probs, I'd keep the same behaviour with int levels though to follow the "path of less surprise". I think users expect contour_kwargs and contourf_kwargs to be passed as is to matplotlib after adding some defaults, this would change that.

I remembered hdi_probs were converted into levels so it means we can change the hdi_probs->levels translation to fix the issue (if we restrict ourselves to that). Here https://github.com/arviz-devs/arviz/blob/main/arviz/plots/kdeplot.py#L282 both 0 and the maximum are added to the levels which with the new behaviour I think we only want for contourf but not for contour, so changing this to put a slice of the variable instead should fix the issue: https://github.com/arviz-devs/arviz/blob/main/arviz/plots/kdeplot.py#L292

@aloctavodia
Copy link
Contributor Author

aloctavodia commented Sep 16, 2024

What you suggest will fix the issue when passing hdi_probs, actually that was my first thought too. But to my surprise if you specify countour_kwargs={"levels":x}, countourf_kwargs={"levels":x}, where x is an integer, or if you just do the default plot, you will still get the "extra" contour.

In other words, this is not something exclusive of hdi_probs.

@OriolAbril
Copy link
Member

if you specify countour_kwargs={"levels":x}, countourf_kwargs={"levels":x}, where x is an integer, or if you just do the default plot, you will still get the "extra" contour.

The thing is this new "extra" contour is matplotlib new default behaviour. It might be a bit surprising at first because it has been added recently, but I think this is a temporal effect which will soon be reversed. People will have done/seen contourplots with matplotlib which show this outer contour delimiting where there is data but using the same parameters they won't get it if using ArviZ, in fact, depending on what we do it might even be impossible to get this behaviour at all if using ArviZ.

With hdi_probs the expected behaviour is clearer and unless someone asks for 0 as hdi_prob to be plotted that outer contour should not be there. But with integer value passed to levels the matplotlib docs say "If an int n, use MaxNLocator, which tries to automatically choose no more than n+1 "nice" contour levels", plus the fact that it is/was expected contour(levels=x) and contourf(levels=x) basically produce the same plot also makes things quite challenging.

I have gone quite a bit off-topic, but I think it mostly boils down to "do we actively dislike this outer contour and want to diverge from matplotlib behaviour?"

@aloctavodia
Copy link
Contributor Author

I see your point, but my visceral dislike for this outer contour is greater, haha. What if we default to some values of hdi_probs other than None like [0.5, 0.8, 0.94]?

@OriolAbril
Copy link
Member

What if we default to some values of hdi_probs other than None like [0.5, 0.8, 0.94]?

Sounds good

@aloctavodia aloctavodia changed the title fix 2kde contour 2kde use hdi_probs by default Sep 20, 2024
Copy link

codecov bot commented Sep 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.95%. Comparing base (8e14fc1) to head (5fba0bd).
Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2383      +/-   ##
==========================================
- Coverage   87.00%   86.95%   -0.05%     
==========================================
  Files         124      124              
  Lines       12877    12882       +5     
==========================================
- Hits        11203    11202       -1     
- Misses       1674     1680       +6     
Flag Coverage Δ
86.95% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aloctavodia
Copy link
Contributor Author

@OriolAbril I think this is ready to merge.

@OriolAbril OriolAbril merged commit f09ffd3 into main Sep 25, 2024
12 checks passed
@OriolAbril OriolAbril deleted the 2kde_cont branch September 25, 2024 16:32
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

Successfully merging this pull request may close these issues.

2D Kde showing "extra" contour
2 participants