import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2 * np.pi, 100)
r = 1 + 0.5 * np.sin(4 * theta)
plt.figure(figsize=(8, 6))
ax = plt.subplot(111, polar=True)
ax.plot(theta, r, color='blue', linewidth=2)
ax.set_title('Basic Polar Plot Pattern', va='bottom')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy → Used for numerical operations and generating arrays efficiently.
theta = np.linspace(0, 2 * np.pi, 100)
r = 1 + 0.5 * np.sin(4 * theta)
np.linspace(0, 2 * np.pi, 100) → Generates 100
evenly spaced values between 0 and 2π (360°).
theta → Represents the angular values in radians, making it perfect for a polar plot
np.sin(4 * theta) → Calculates the sine of the angular values. The 4 in the expression means the sine wave will complete 4 oscillations in one full rotation (2π).
r = 1 + 0.5 * sin(4 * theta) →
1 → Base radius (Constant Offset)
0.5 * sin(4 * theta) → Creates the sinusoidal
oscillations.
This makes the radius vary between 0.5 to 1.5 in a wave-like
pattern.
plt.figure(figsize=(8, 6))
plt.figure(figsize=(8, 6)) → Creates a figure of
size 8x6 inches. This helps in adjusting the dimensions for better visibility.
ax = plt.subplot(111, polar=True)
plt.subplot() → Creates a subplot inside the figure.
111 → Means 1 row, 1 column, and this is the 1st
subplot.
polar=True → Specifies that the plot will be in
polar coordinates instead of Cartesian.
ax.plot(theta, r, color='blue', linewidth=2)
ax.plot() → Plots the data on the polar plot using
the generated theta (angles) and r (radius values).
color='blue' → Makes the curve blue.
linewidth=2 → Sets the line thickness to 2 points
for clearer visibility.
ax.set_title('Basic Polar Plot with Sinusoidal
Pattern', va='bottom')
ax.set_title() → Adds a title to the polar plot.
va='bottom' → Aligns the title to the bottom of the
plot.
plt.show()
plt.show() → Displays the plot on the screen.
0 Comments:
Post a Comment