← retour aux snippets

matplotlib: fill_between, errorbar, twinx

Remplissages, barres d'erreur et axe secondaire.

python viz #matplotlib#twinx

objectif

Remplissages, barres d’erreur et axe secondaire.

code minimal

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 50)
y = np.sin(x)
plt.fill_between(x, y, 0)
print("fill ok")

utilisation

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5); y = [1,2,3,2,1]; e = [0.1]*5
plt.errorbar(x, y, yerr=e)
ax1 = plt.gca(); ax2 = ax1.twinx(); ax2.plot(x, y)
print("twin ok")

variante(s) utile(s)

import matplotlib.pyplot as plt

plt.figure(); plt.plot([1,2,3]); plt.tight_layout(); print("ok")

notes

  • Ne pas surcharger la lecture avec deux axes sans nécessité.