← retour aux snippets

matplotlib: légende, grille, savefig

Gérer légendes, grille et exporter la figure.

objectif

Gérer légendes, grille et exporter la figure.

code minimal

import matplotlib.pyplot as plt

plt.plot([1,2,3], label="serie")
plt.legend(); plt.grid(True)
plt.savefig("fig.png", dpi=120, bbox_inches="tight")
print("saved")

utilisation

import matplotlib.pyplot as plt

plt.figure(); plt.plot([1,2,3], [3,2,1], label="b"); plt.legend(); print("legend ok")

variante(s) utile(s)

import matplotlib.pyplot as plt

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

notes

  • SVG pour vectoriel; PNG pour bitmap.