Introduction
This guide demonstrates how to configure and manipulate axes in polar coordinates using Matplotlib, a popular Python plotting library. We’ll cover the basics of setting up a polar plot, adjusting axis ticks, plotting points and functions, and even visualizing a Fraunhofer diffraction pattern.
Setting Up Axis in Polar Coordinates
To configure a plot in polar coordinates, you need to set the projection='polar'
when creating the axis. Here's a simple example:
1 2 3 4 5 6 7 | 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() |
In this example, we create a figure and define an axis with a polar projection. The plot is then saved and displayed.
Formatting Polar Axis Ticks
You can modify the ticks on both the radial (r) and angular (theta) axes using set_xticks
and set_yticks
. This is particularly useful if you need customized labeling for your plot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='polar') # Set custom theta ticks (in radians) ax.set_xticks(np.arange(0, 2.0 * np.pi, np.pi / 6)) # Set radial limits and custom ticks ax.set_ylim(0, 4) ax.set_yticks(np.arange(0, 4, 1)) plt.savefig("polar_coordinates_02.png", bbox_inches='tight') plt.show() |
In this case, the angular ticks are set to divide the full circle (2π radians) into 12 parts, and the radial axis is limited to a range of 0 to 4.
Plotting a Point in Polar Coordinates
To plot a single point, you need to specify its radius r
and angle theta
(in radians). You can use numpy.deg2rad()
to convert degrees to radians if needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import matplotlib.pyplot as plt import numpy as np r = 2.0 theta = np.deg2rad(60.0) # Convert 60 degrees to radians fig = plt.figure() ax = fig.add_subplot(111, projection='polar') # Plot a point at (r, theta) ax.scatter(theta, r) # Format the axis ax.set_xticks(np.arange(0, 2.0 * np.pi, np.pi / 6)) ax.set_ylim(0, 4) ax.set_yticks(np.arange(0, 4, 1)) plt.savefig("polar_coordinates_03.png", bbox_inches='tight') plt.show() |
Here, a single point at a radius of 2.0 units and an angle of 60 degrees is plotted.
Plotting a Function in Polar Coordinates
You can also plot functions in polar coordinates, where theta
represents the angular coordinate and r
the radial coordinate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import matplotlib.pyplot as plt import numpy as np r = np.arange(0, 6, 0.01) theta = 2 * np.pi * r # A spiral function fig = plt.figure() ax = fig.add_subplot(111, projection='polar') # Plot the function ax.plot(theta, r) # Format the axis ax.set_xticks(np.arange(0, 2.0 * np.pi, np.pi / 6)) ax.set_ylim(0, 4) ax.set_yticks(np.arange(0, 4, 1)) plt.savefig("polar_coordinates_04.png", bbox_inches='tight') plt.show() |
In this example, we plot a spiral function where the radial distance increases as theta
increases.
Plotting a Surface: Fraunhofer Diffraction Pattern
Here’s an advanced example of plotting a Fraunhofer diffraction pattern from a dataset, visualizing the intensity distribution in polar coordinates. (FraunhoferHexagonalAperture_Data.txt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #!/usr/bin/env python import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm from matplotlib import ticker # Load the dataset r, theta, refl = np.loadtxt("FraunhoferHexagonalAperture_Data.txt", unpack=True) # Reshape the reflection data for contouring refl = np.reshape(refl, (360, -1)) # Generate grid for polar plot azimuths = np.radians(np.linspace(0, 360, 360)) zeniths = np.arange(0.00000000001, 30, 0.1) r, theta = np.meshgrid(zeniths, azimuths) # Create the polar plot fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) # Define contour levels for diffraction intensity contour_levels = [0.0000000001, 0.000000001, 0.00000001, 0.0000001, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0] # Plot the filled contours CS = ax.contourf(theta, r, refl, contour_levels, cmap=cm.gist_earth_r, locator=ticker.LogLocator()) # Add colorbar cbar = plt.colorbar(CS) cbar.set_label(r"Fraunhofer Diffracted Intensity $I/I_0$") plt.savefig('FraunhoferHexagonalAperture.png') plt.show() |
In this plot, we visualize the intensity of a Fraunhofer diffraction pattern, with contours representing different intensity levels.
References
Link | Site |
---|---|
Wikipedia: Polar Coordinates | wikipedia |
Matplotlib Documentation: Scatter Plot on Polar Axis | matplotlib.org |
Matplotlib Example: Polar Demo | matplotlib.org/ |
Matplotlib API: Polar Plots | matplotlib.org/ |
Stack Overflow: Set Axis Limit in Polar Plot | stackoverflow.com |
Matplotlib: Transoffset Example | matplotlib.org |