pysal / pointpats

Planar Point Pattern Analysis in PySAL
https://pysal.org/pointpats/
BSD 3-Clause "New" or "Revised" License
80 stars 26 forks source link

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

Closed martinfleis closed 10 months ago

martinfleis commented 10 months ago

Resolves https://github.com/gdsbook/book/issues/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[bot] commented 10 months ago

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 commented 10 months ago

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

martinfleis commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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?