- Scatter plot on polar axis#
- Scatter plot on polar axis, with offset origin#
- Scatter plot on polar axis confined to a sector#
- How to Plot Polar Axes in Matplotlib?
- Introduction
- When to use Polar Coordinates
- Angular Data
- How to Plot Polar Axes in Matplotlib
- How does it Work?
- Syntax and Parameters
- How to Plot Data on a Polar Coordinate System
- Common Examples
- Polar Projections
- Polygons
- Conclusion
Scatter plot on polar axis#
Size increases radially in this example and color increases with angle (just to verify the symbols are being scattered correctly).
import numpy as np import matplotlib.pyplot as plt # Fixing random state for reproducibility np.random.seed(19680801) # Compute areas and colors N = 150 r = 2 * np.random.rand(N) theta = 2 * np.pi * np.random.rand(N) area = 200 * r**2 colors = theta fig = plt.figure() ax = fig.add_subplot(projection='polar') c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
Scatter plot on polar axis, with offset origin#
The main difference with the previous plot is the configuration of the origin radius, producing an annulus. Additionally, the theta zero location is set to rotate the plot.
fig = plt.figure() ax = fig.add_subplot(projection='polar') c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75) ax.set_rorigin(-2.5) ax.set_theta_zero_location('W', offset=10)
Scatter plot on polar axis confined to a sector#
The main difference with the previous plots is the configuration of the theta start and end limits, producing a sector instead of a full circle.
fig = plt.figure() ax = fig.add_subplot(projection='polar') c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75) ax.set_thetamin(45) ax.set_thetamax(135) plt.show()
The use of the following functions, methods, classes and modules is shown in this example:
Total running time of the script: ( 0 minutes 1.794 seconds)
How to Plot Polar Axes in Matplotlib?
Matplotlib provides the module and functions to plot the coordinates on polar axes. A point in polar coordinates is represented as (r, θ \theta θ ). The pair has a distance and angle – r r r for the radius or distance from the origin and θ \theta θ for the angle at which r has to be measured from the origin. The pyplot module of matplotlib provides the function polar to draw a polar plot.
Introduction
The polar coordinate system plots angular data on the graph, i.e., periodic functions. A point in the polar coordinate system is represented as ( r , θ r,\theta r , θ ), where r is the distance from the origin and theta is the angle from the axis. matplotlib.pyplot.polar() is used to plotting curve on polar axes.
When to use Polar Coordinates
The two most popular methods for plotting the points on a graph are the rectangular and polar coordinate systems. Polar coordinates are best used when periodic functions are considered. The polar coordinates of points describe their position in terms of distance from the origin and a fixed direction (angle).
Using a rectangular coordinate system when dealing with latitude and longitude would be impractical. Suppose we point out the location of a spherical object. In that case, we use polar coordinates because it will pinpoint the location with more accuracy, distance from the center of the spherical object, and angle from the axes.
Similarly, in a GPS, the concept of polar coordinates is used to find the real-time location of a person. Moreover, many physical systems—such as those concerned with bodies moving around a central point or phenomena originating from a central point—are simpler and more intuitive to model using polar coordinates.
Angular Data
For plotting the polar coordinates on the polar coordinate system, we need two things, r r r and θ \theta θ , where r r r is the distance from the origin to the point, and the θ \theta θ is the angle of projection of the point to the x-axis.
Example: Plotting angular data on the polar coordinate system
In the above plot, this is how a polar coordinate system looks: angles are the x-axis and the distance from the origin to the circle’s circumference is the y-axis. As we can see, there is a marker on the coordinate system. It is a polar coordinate where r=8 and theta=45°.
How to Plot Polar Axes in Matplotlib
How does it Work?
Before plotting polar coordinates on the graph, we have to change the coordinate system from rectangular to polar, which can be done by using the function matplotlib.pyplot.axes(projection =) where we must pass the value ‘polar’ in the parameter projection. The matplotlib.pyplot.polar() function pyplot modules are used to plot polar coordinates. This function plots various figures like circles, ellipses, cardioid, and the archimedean spiral. It takes at least two parameters to plot the polar coordinates on the graph, i.e., r and theta.
Syntax and Parameters
Syntax of the function to plot polar coordinates is
Parameters of the function,
- theta: Angle by which we plot the point.
- r: Distance between the origin and point.
- kwargs: Keyword argument to change the visual properties of the points like a marker, color, size, width, etc.
How to Plot Data on a Polar Coordinate System
Below is the implementation of plotting points on polar axes in matplotlib.
Code explanation:
- Importing the required modules.
- Creating the new figure and setting up the resolution using the function matplotlib.pyplot.figure(dpi=) .
- Adding the subplots to the created figure and set the coordinate system to polar by setting the value of the parameter projection to ‘polar’.
- Then, generate the r and theta and store them in the list.
- Plot the point on the polar coordinate system using the function matplotlib.pyplot.polar() .
- Then, set the y-axis tick range using the .add_subplot().set_ylim function.
- Displaying the plot using the matplotlib.pyplot.show() function.
Common Examples
Polar Projections
Polar projections are some plots that can be plotted on a polar coordinate system example, a circle, an ellipse, or an archimedean spiral.
Example 1: Drawing a circle on a polar coordinate system
For Drawing a circle on the polar coordinate system, we need a radius and a list of angles from 0 to 2π in a very short interval.
Output:
Example 2: Drawing an ellipse on polar coordinates
An ellipse is a locus of a point moving in a plane such that the sum of its distances from two other points (the foci) is constant. Here, r is defined as:
r = a ∗ b ( a s i n θ ) 2 + ( b c o s θ ) 2 ) r= \frac > r = ( a s i n θ ) 2 + ( b c o s θ ) 2 )
a = length of the semi-major axis b = length of the semi-minor axis
Polygons
Example: Implementation of the polygon in matplotlib
Conclusion
- A point in polar coordinates is represented as ( r , θ r,\theta r , θ ).
- r is the distance from the origin, and theta ( θ \theta θ ) is the angle from the axis.
- First, we must set the coordinate system from rectangular to polar.
- In matplotlib, we can plot points on polar axes by using the function matplotlib.pyplot.Polar .