Mapclassify

2.10.0 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to generate sample data, apply two common classification schemes (Quantiles and Fisher-Jenks), and inspect their bin edges and class assignments. It then visualizes the results using the `plot_legendgram` method, which requires `matplotlib` for execution. Ensure `mapclassify[plotting]` is installed for the visualization part.

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()

view raw JSON →