Creating Polar Plots Using Matplotlib and NumPy
In this blog, we will explore how to create polar plots using Matplotlib and NumPy. This marks my 24th blog on my journey with Matplotlib, showcasing its powerful plotting capabilities.
import matplotlib.pyplot as plt
import numpy as np
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = np.linspace(0, 5, 10)
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(x, y)
plt.savefig("Plots/fig_with_polar_plot.png")
plt.clf()
theta = np.linspace(0, 2 * np.pi, 100)
radius = np.abs(np.sin(2 * theta))
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(theta, radius, color='b', linestyle='--', linewidth=2)
plt.savefig("Plots/fig_with_polar_plot_custom.png")
plt.clf()We will walk through the code step-by-step to understand how to create and customize polar plots using these libraries. Let’s dive in!
Importing Required Libraries
To begin, we need to import the necessary libraries: matplotlib.pyplot and numpy. These libraries are essential for plotting and generating data points.
import matplotlib.pyplot as plt
import numpy as npGenerating Data Points for Polar Plot
We create two sets of data points: x and y. The x values range from 1 to 10, and the y values are generated using the numpy.linspace() function to create a range from 0 to 5 with 10 equally spaced values.
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = np.linspace(0, 5, 10)Creating a Figure Object
Next, we create a figure object using the plt.figure() method. This figure will serve as the canvas on which we will plot our polar graph.
fig = plt.figure()Adding a Polar Subplot
We add a subplot to the figure object with polar coordinates by using the add_subplot() method. Setting the parameter polar=True ensures that the plot is created in polar form.
ax = fig.add_subplot(111, polar=True)Plotting Data Points on the Polar Plot
Using the plot() method, we plot the x and y data points on the polar subplot.
ax.plot(x, y)Saving the Polar Plot
We save the plot as an image using plt.savefig(). This will store the figure as a file named fig_with_polar_plot.png. After saving, we clear the figure using plt.clf() to prepare for the next plot.
plt.savefig("Plots/fig_with_polar_plot.png")
plt.clf()Visualizing Polar Plot
Customizing the Polar Plot with New Data
In this section, we generate a new set of data points, theta and radius, to create a more intricate polar plot. The theta values range from 0 to 2*pi with 100 elements, and the radius is calculated using the absolute value of the sine function.
theta = np.linspace(0, 2 * np.pi, 100)
radius = np.abs(np.sin(2 * theta))Creating a New Figure Object for the Custom Plot
Similar to the previous example, we create a new figure object to plot our customized polar graph.
fig = plt.figure()Adding a Customized Polar Subplot
We again add a polar subplot to the new figure object.
ax = fig.add_subplot(111, polar=True)Plotting the Custom Polar Data
This time, we plot the theta and radius data points with specific customizations. We use a blue color, dashed linestyle, and set the linewidth to 2.
ax.plot(theta, radius, color='b', linestyle=' - ', linewidth=2)Saving the Customized Polar Plot
We save the customized plot as a new image file named fig_with_polar_plot_custom.png and clear the plot afterward.
plt.savefig("Plots/fig_with_polar_plot_custom.png")
plt.clf()Visualizing Custom Polar Plot
Conclusion
In this blog, we successfully explored the steps to create and customize polar plots using Matplotlib and NumPy. We started by plotting simple data points and then moved to a more customized polar plot with specific styling.
Get Someone’s stories in your inbox
Join Medium for free to get updates from this writer.
Understanding these concepts allows you to create visually appealing plots that can be used for various analytical purposes.
If you’d like to explore my code further or see more of my work with Matplotlib, please check out my Github account. To stay updated with my progress and latest blogs, make sure to follow me on Twitter.
Looking forward to seeing you in the next blog as we continue our journey with data visualization!









