← retour aux snippets

matplotlib: hist2d et hexbin

Densités 2D par histogramme carte/hexbin.

objectif

Densités 2D par histogramme carte/hexbin.

code minimal

import matplotlib.pyplot as plt, numpy as np
x = np.random.default_rng(0).normal(size=1000)
y = x + np.random.default_rng(1).normal(scale=0.5, size=1000)
plt.hist2d(x, y, bins=20)
print("ok")

utilisation

import matplotlib.pyplot as plt, numpy as np
x = np.random.default_rng(0).normal(size=500)
y = np.random.default_rng(1).normal(size=500)
plt.hexbin(x, y, gridsize=15)
print("ok")

variante(s) utile(s)

import matplotlib.pyplot as plt
plt.colorbar() if plt.gcf() else None
print("ok")

notes

  • Préférer hexbin pour grands nuages de points.