Exemples de comment tracer un simple cercle avec matplotlib de python:
Tracer un cercle avec plot()
Pour tracer un cercle en python avec matplotlib on peut utiliser la fonction plot():

import numpy as npimport matplotlib.pyplot as plttheta = np.linspace(0, 2*np.pi, 100)r = np.sqrt(1.0)x1 = r*np.cos(theta)x2 = r*np.sin(theta)fig, ax = plt.subplots(1)ax.plot(x1, x2)ax.set_aspect(1)plt.xlim(-1.25,1.25)plt.ylim(-1.25,1.25)plt.grid(linestyle='--')plt.title('How to plot a circle with matplotlib ?', fontsize=8)plt.savefig("plot_circle_matplotlib_01.png", bbox_inches='tight')plt.show()
Avec Matplotlib patches circle
On peut aussi utiliser matplotlib.patches.Circle:

import matplotlib.pyplot as pltcircle1 = plt.Circle((0, 0), 0.5, color='r')fig, ax = plt.subplots()plt.xlim(-1.25,1.25)plt.ylim(-1.25,1.25)plt.grid(linestyle='--')ax.set_aspect(1)ax.add_artist(circle1)plt.title('How to plot a circle with matplotlib ?', fontsize=8)plt.savefig("plot_circle_matplotlib_02.png", bbox_inches='tight')plt.show()
En utilisant l'équation du cercle
Autre option si on veut partir de l'équation du cercle:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(-1.0, 1.0, 100)y = np.linspace(-1.0, 1.0, 100)X, Y = np.meshgrid(x,y)F = X**2 + Y**2 - 1.0fig, ax = plt.subplots()ax.contour(X,Y,F,[0])ax.set_aspect(1)plt.title('How to plot a circle with matplotlib ?', fontsize=8)plt.xlim(-1.25,1.25)plt.ylim(-1.25,1.25)plt.grid(linestyle='--')plt.savefig("plot_circle_matplotlib_03.png", bbox_inches='tight')plt.show()
Références
| Liens | Site |
|---|---|
| matplotlib.patches.Circle | matplotlib doc |
| plot a circle with pyplot | stackoverflow |
| Plot equation showing a circle | stackoverflow |
