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

ENH: plot_density for KDE plotting of point patterns based on statsmodels #118

Merged
merged 4 commits into from
Sep 18, 2023

Conversation

martinfleis
Copy link
Member

Resolves gdsbook/book#305. Essentially replaces seaborn.kdeplot which does not work as expected with our own solution.

Tests will come once I'll figure out how to do them.

import geopandas as gpd
from pointpats.kde import plot_density

gdf = gpd.read_file('https://martinfleischmann.net/sds/chapter_06/data/brno_pedestrian_accidents.gpkg')


gdf.plot(markersize=0.5, figsize=(9,9))
plot_density(gdf, 500)

4d2bc015-5f66-441c-8b69-c488ecd58d2c

gdf.plot(markersize=0.5, figsize=(9,9))
plot_density(gdf, 500, levels=25, cmap="magma_r", linewidths=1)

60e0adc4-0c3b-4f59-a7f8-1a7d07c824a8

gdf.plot(markersize=0.1, figsize=(9,9), zorder=2, color='k')
plot_density(gdf, 500, levels=25, cmap="magma_r", fill=True)

d68543e7-cbcb-458d-872e-ce62ea3f3da1

gdf.plot(markersize=0.5, figsize=(9,9))
plot_density(gdf, 1000, cmap="magma_r", linewidths=1)

ceb74c5e-0c3c-4f63-ad6b-b4d2f127b72a

I would love to have this in the next release alongside #111 so I can use both during teaching starting in two weeks.

@codecov
Copy link

codecov bot commented Sep 17, 2023

Codecov Report

Merging #118 (26b45b3) into main (8fd02a6) will increase coverage by 0.7%.
The diff coverage is 86.4%.

Impacted file tree graph

@@           Coverage Diff           @@
##            main    #118     +/-   ##
=======================================
+ Coverage   63.5%   64.2%   +0.7%     
=======================================
  Files         11      12      +1     
  Lines       1404    1448     +44     
=======================================
+ Hits         892     930     +38     
- Misses       512     518      +6     
Files Changed Coverage Δ
pointpats/kde.py 86.0% <86.0%> (ø)
pointpats/__init__.py 100.0% <100.0%> (ø)

@knaaptime
Copy link
Member

funny. I'm also right in the middle of using this for some bike/ped accident work

pointpats/kde.py Outdated Show resolved Hide resolved
@martinfleis
Copy link
Member Author

I was struggling to make it work decently fast on large data so I included also an implementation on top of KDEpy. That is fast, offers a selection of kernels but can behave funny in some edge cases. While statsmodels seems to be robust default.

@martinfleis
Copy link
Member Author

The tests are not perfect but testing matplotlib is just a pain in the ass, so this at least ensures that the stuff works and args are used. It should be ready for review.

@knaaptime knaaptime merged commit 0725890 into pysal:main Sep 18, 2023
@martinfleis martinfleis deleted the kde branch September 18, 2023 05:27
@knaaptime
Copy link
Member

right now this gives back a matplotlib.contour.QuadContourSet. Is there any way to have it return an axes object, e.g. to play nicely with contextily?

@martinfleis martinfleis mentioned this pull request Sep 26, 2023
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.

seaborn.kdeplot is not suitable for point patterns
4 participants