Mapclassify
Mapclassify provides a collection of classification schemes for choropleth maps, including standard methods like Quantiles, Equal Interval, and Natural Breaks (Jenks). It helps transform raw data into discrete classes suitable for thematic mapping. The library is actively maintained as part of the PySAL ecosystem, currently at version 2.10.0, with regular updates introducing new features and improvements.
Warnings
- breaking Starting with version 2.9.0, mapclassify requires Python 3.11 or newer. Projects on older Python versions will need to upgrade Python or stick to `mapclassify<2.9.0`.
- gotcha When working with `FisherJenks` classification, `numba` is highly recommended for performance, especially with large datasets. Without it, the algorithm can be significantly slower.
- gotcha Versions prior to 2.8.1 had a bug in NaN handling for color arrays. While fixed, users on older versions should be aware of potential issues with missing data in visualization.
- deprecated Mentions of `geoplot` were removed from the README and documentation around v2.8.0. This suggests a shift away from recommending `geoplot` for visualization within the PySAL ecosystem, possibly towards `splot` or direct `matplotlib` / `seaborn` integration.
- gotcha Version 2.10.0 introduced enhanced support for `Colormap` objects and `cmap` detection. If you're using custom color mapping with `plot_legendgram` or related functions, this release provides more flexible and idiomatic ways to integrate `matplotlib` colormaps.
Install
-
pip install mapclassify -
pip install mapclassify[plotting] -
pip install mapclassify[all]
Imports
- mapclassify
import mapclassify as mc
- Quantiles
mc.Quantiles
- NaturalBreaks
mc.NaturalBreaks
- FisherJenks
mc.FisherJenks
Quickstart
import numpy as np
import mapclassify as mc
import matplotlib.pyplot as plt
# Generate some sample data
np.random.seed(42)
data = np.random.rand(100) * 100
# Apply a classification scheme (e.g., Quantiles)
classifier_q = mc.Quantiles(data, k=5)
print(f"Quantiles Classifier (k={classifier_q.k}):")
print(f"Bin edges: {classifier_q.bins}")
print(f"Class assignments for first 5 values: {classifier_q.yb[:5]}\n")
# Apply another scheme (e.g., Natural Breaks / Fisher-Jenks)
classifier_fj = mc.FisherJenks(data, k=5)
print(f"Fisher-Jenks Classifier (k={classifier_fj.k}):")
print(f"Bin edges: {classifier_fj.bins}")
print(f"Class assignments for first 5 values: {classifier_fj.yb[:5]}\n")
# Visualize the classification with a legendgram
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
classifier_q.plot_legendgram(ax=ax1, cmap='viridis', title='Quantiles Legendgram')
classifier_fj.plot_legendgram(ax=ax2, cmap='plasma', title='Fisher-Jenks Legendgram')
plt.tight_layout()
plt.show()