# How to put axis in polar coordinates in matplotlib ?

Daidalos July 25, 2019

Examples of how to put axis in polar coordinates in matplotlib:

### Axis in polar coordinates

To put the axis in polar coordinates in matplotlib, a solution is to use the option projection='polar', example:

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()


### Formatting the axis

It is also possible to change the axis (ticks and lim) using set_xticks and set_yticks:

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()


### Plot a point

Note: The numpy function deg2rad() can be used to transform degrees to 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()


### Plot a function

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()


### Plot a surface

Example of how to plot a Fraunhofer diffraction pattern using the following dataset:FraunhoferHexagonalAperture_Data.txt :

#!/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()


### References

Coordonnées polaires wikipedia
Scatter plot on polar axis matplotlib doc
pylab_examples example code: polar_demo.py matplotlib doc
matplotlib.pyplot.polar matplotlib doc
How to set the axis limit in a matplotlib plt.polar plot stackoverflow
Transoffset matplotlib doc

Licence

Activity