Comment mettre les axes en coordonnées polaires avec matplotlib de python ?


Exemples de comment mettre les axes en coordonnées polaires avec matplotlib :

Axes en coordonnées polaires

Pour mettre les axes en coordonnées polaires, il suffit d'ajouter l'option projection='polar' comme dans cet exemple:

Comment mettre les axes en coordonnées polaires avec matplotlib de python ?
Comment mettre les axes en coordonnées polaires avec matplotlib de python ?

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111, projection='polar')

plt.savefig("polar_coordinates_01.png", bbox_inches='tight')

plt.show()

Mettre en forme

On peut aussi jouer avec la mise en forme avec set_xticks et set_yticks:

Comment mettre les axes en coordonnées polaires avec matplotlib de python ?
Comment mettre les axes en coordonnées polaires avec matplotlib de python ?

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

ax = fig.add_subplot(111, projection='polar')

ax.set_xticks(np.arange(0,2.0*np.pi,np.pi/6.0))

ax.set_ylim(0,4)
ax.set_yticks(np.arange(0,4,1.0))

plt.savefig("polar_coordinates_02.png", bbox_inches='tight')

plt.show()

Tracer un point

Comment mettre les axes en coordonnées polaires avec matplotlib de python ?
Comment mettre les axes en coordonnées polaires avec matplotlib de python ?

Note: On peut utiliser le fonction numpy deg2rad() pour convertir des degrés en radians

import matplotlib.pyplot as plt
import numpy as np

r = 2.0
theta = np.deg2rad(60.0)

fig = plt.figure()

ax = fig.add_subplot(111, projection='polar')

ax.scatter(theta,r)

ax.set_xticks(np.arange(0,2.0*np.pi,np.pi/6.0))

ax.set_ylim(0,4)
ax.set_yticks(np.arange(0,4,1.0))

plt.savefig("polar_coordinates_03.png", bbox_inches='tight')

plt.show()

Tracer une fonction

Comment mettre les axes en coordonnées polaires avec matplotlib de python ?
Comment mettre les axes en coordonnées polaires avec matplotlib de python ?

import matplotlib.pyplot as plt
import numpy as np

r = np.arange(0, 6, 0.01)
theta = 2 * np.pi * r

fig = plt.figure()

ax = fig.add_subplot(111, projection='polar')

ax.plot(theta,r)

ax.set_xticks(np.arange(0,2.0*np.pi,np.pi/6.0))

ax.set_ylim(0,4)
ax.set_yticks(np.arange(0,4,1.0))

plt.savefig("polar_coordinates_04.png", bbox_inches='tight')

plt.show()

Tracer une surface

Exemple de comment tracer une figure de diffraction (fichier de données: FraunhoferHexagonalAperture_Data.txt ) avec matplotlib:

Diffraction Fraunhofer ouverture hexagonale (Matplotlib)
Diffraction Fraunhofer ouverture hexagonale (Matplotlib)

#!/usr/bin/env python

from matplotlib import ticker

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# 108000 lines = 300 (r) * 360 (theta)

r, theta, refl = np.loadtxt("FraunhoferHexagonalAperture_Data.txt", unpack=True)

refl = np.reshape( refl, (360,-1) )

azimuths = np.radians(np.linspace(0, 360, 360))
zeniths = np.arange(0.00000000001, 30, 0.1)

r, theta = np.meshgrid(zeniths, azimuths)


fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))

contour_levels = [0.0000000001,0.000000001,0.00000001,0.0000001,
                  0.000001,0.00001,0.0001,0.001,0.01,0.1,1.0]

CS = ax.contourf(theta, r, refl, contour_levels, 
                 cmap=cm.gist_earth_r, locator=ticker.LogLocator())

cbar = plt.colorbar(CS)
cbar.set_label(r"Fraunhofer Diffracted Intensity $I/I_0$")

plt.savefig('FraunhoferHexagonalAperture.png')

plt.show()

Références

Image

of