Hexbin Plot

Soil Nutrient Analysis

Agricultural hexbin showing nitrogen vs phosphorus levels in soil samples

Output
Soil Nutrient Analysis
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(555)
nitrogen = np.random.gamma(3, 10, 5500)
phosphorus = nitrogen * 0.3 + np.random.gamma(2, 5, 5500)

fig, ax = plt.subplots(figsize=(10, 8), facecolor='#f0fdf4')
ax.set_facecolor('#dcfce7')

colors = ['#dcfce7', '#bbf7d0', '#86efac', '#4ade80', '#22c55e', '#16a34a', '#15803d', '#166534']
cmap = LinearSegmentedColormap.from_list('green', colors, N=256)

hb = ax.hexbin(nitrogen, phosphorus, gridsize=28, cmap=cmap, mincnt=1, edgecolors='white', linewidths=0.3)

cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.8)
cbar.outline.set_edgecolor('#bbf7d0')
cbar.ax.tick_params(colors='#15803d', labelsize=9)
cbar.set_label('Sample Count', color='#15803d', fontsize=10)

ax.set_xlabel('Nitrogen (mg/kg)', color='#15803d', fontsize=11)
ax.set_ylabel('Phosphorus (mg/kg)', color='#15803d', fontsize=11)
ax.tick_params(colors='#15803d', labelsize=10)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#bbf7d0')
ax.spines['bottom'].set_color('#bbf7d0')

plt.tight_layout()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support