Matplotlib - Polar Charts



A polar chart is a type of graph that displays data in a circular format. Instead of using traditional x and y axes like in a regular chart, a polar chart uses a circular grid. The center of the circle represents the starting point, and data points are plotted at various distances from the center and at different angles.

Imagine a compass where the center is north, and as you move outward, you are going in different directions. Each direction corresponds to an angle on the polar chart. The distance from the center to a point represents a value −

Table Charts

Polar Charts in Matplotlib

We can create a polar chart in Matplotlib using the polar() function. This function allows us to plot data in a polar coordinate system. The radial axis represents the distance from the center, while the angular axis represents the angle around the circle.

This type of chart is useful for applications such as radar systems, astronomy, and any scenario where cyclical information needs to be visually shown.

The polar() Function

The polar() function in Matplotlib is used to create polar plots, which represent data in a polar coordinate system. Instead of the typical x and y axes, polar plots use radial and angular axes to visualize data points in a circular manner.

Following is the syntax of the polar() function in Matplotlib −

plt.polar(theta, r, marker='o', linestyle='-', label='Data')

Where,

  • theta is an array representing the angles in radians for data points.
  • r is an array representing the radial distance from the center for each data point.
  • marker (optional) specifies the marker style for each data point like 's' for squares, 'o' for circles or 'x' for crosses.
  • linestyle (optional) specifies the style of connecting lines between data points like '-' for solid line, ':' for dotted lines or '--' for dashed lines.
  • label (optional) provides a label for the plotted data, which is useful when adding a legend to the plot.

Basic Polar Plot with Sine Function

Creating a polar plot with a sine function in Matplotlib is like displaying data in a circular coordinate system, where the angle around the circle corresponds to the independent variable, and the radial distance from the center represents the value of the sine function at that angle.

Example

In the following example, we are creating a basic polar plot using the plt.polar() function with polar coordinates derived from a sine function. The resulting plot shows a sine wave pattern around the circle −

import matplotlib.pyplot as plt
import numpy as np

# Generating polar coordinates using a sine function
theta = np.linspace(0, 2*np.pi, 360)
r = np.sin(3 * theta)

# Plotting the polar data and adding a legend
plt.polar(theta, r, label='Sine Function')
plt.legend()
plt.show()

Output

After executing the above code, we get the following output −

Polar Plot with Sine Function

Polar Scatter Plot

A Polar Scatter Plot in Matplotlib is a graphical representation that displays individual data points in a circular coordinate system. Unlike traditional scatter plots that use Cartesian coordinates, polar scatter plots use radial distance and angular position to represent data. Each point is plotted based on its angle and distance from the center, creating a scatter of points around the circle.

Example

In here, we are creating a polar plot with "20" custom data points (r) at evenly spaced angles (theta) around a circle −

import matplotlib.pyplot as plt
import numpy as np

# Specifying polar coordinates
theta = np.linspace(0, 2*np.pi, 20)
r = np.array([0.2, 0.5, 0.8, 1.2, 1.5, 1.8, 2.1, 2.5, 2.8, 3.0, 2.8, 2.5, 2.1, 1.8, 1.5, 1.2, 0.8, 0.5, 0.2, 0.0])

# Creating a polar scatter plot with data points
plt.polar(theta, r, marker='o', linestyle='None', label='Data Points')
plt.legend()
plt.show()

Output

Following is the output of the above code −

Polar Scatter Plot

Multiple Data Series in Polar Plot

A Multiple Data Series in a Polar Plot in Matplotlib is a visual representation that includes plotting more than one set of data in a circular coordinate system. In this type of plot, each data series is represented by its own set of angles and radial distances, creating distinct patterns around the center of the plot. We can distinguish each series by using different colors, markers, or linestyles.

Example

Now, we are creating a polar plot with two data series. To achieve this, we are using sine and cosine functions to generate two distinct radial patterns (r1 and r2) at various angles (theta) −

import matplotlib.pyplot as plt
import numpy as np

# Generating polar coordinates using both sine and cosine functions
theta = np.linspace(0, 2*np.pi, 360)
r1 = np.sin(3 * theta)
r2 = np.cos(2 * theta)

# Plotting multiple data series in a polar plot 
plt.polar(theta, r1, label='Sine Function')
plt.polar(theta, r2, label='Cosine Function')
plt.legend()
plt.show()

Output

Output of the above code is as follows −

Multiple Data Series in Polar Plot

Rose Plot

A Rose Plot in Matplotlib is a specialized type of polar plot that combines circular coordinates with a filled area under the curve. It represents data using a periodic function, such as the sine function, to create petal-like patterns like a rose flower. Each "petal" corresponds to a certain range of angles, and the radial distance from the center indicates the magnitude of the function at those angles.

Example

In the example below, we are creating a polar plot by taking the absolute value of a sine function. The plot displays a petal-like pattern at various angles (theta). Additionally, the fill() function is used to fill the area under the curve −

import matplotlib.pyplot as plt
import numpy as np

# Generating polar coordinates using the absolute value of a sine function
theta = np.linspace(0, 2*np.pi, 360)
r = np.abs(np.sin(5 * theta))

# Plotting a rose plot with a filled area under the curve 
plt.polar(theta, r, label='Rose Plot')
# Filled area under the curve
plt.fill(theta, r, alpha=0.5)  
plt.legend()
plt.show()

Output

The output obtained is as shown below −

Rose Plot
Advertisements