Challenge - 5 Problems
Polar Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediate2:00remaining
Output of a simple polar plot
What will be the output of the following code that uses matplotlib to plot on polar axes?
Matplotlib
import matplotlib.pyplot as plt import numpy as np theta = np.linspace(0, 2 * np.pi, 100) r = np.abs(np.sin(3 * theta)) fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) plt.show()
Attempts:
2 left
š” Hint
Think about how the sine function with frequency 3 affects the shape in polar coordinates.
ā Incorrect
The code plots r = |sin(3Īø)| on polar axes, which creates a shape with three symmetrical 'petals'. The plot is a continuous line, not scatter or bars.
ā data_output
intermediate1:30remaining
Number of ticks on polar theta axis
Given the code below, how many major ticks will appear on the theta (angular) axis by default?
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) plt.close(fig) len(ax.get_xticks())
Attempts:
2 left
š” Hint
Check the default number of angular ticks matplotlib uses for polar plots.
ā Incorrect
Matplotlib's polar axes default to 8 major ticks evenly spaced around the circle (every 45 degrees).
š§ Debug
advanced2:00remaining
Identify the error in polar plot code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt import numpy as np theta = np.linspace(0, 2 * np.pi, 100) r = np.sin(theta) fig, ax = plt.subplots() ax.plot(theta, r, projection='polar') plt.show()
Attempts:
2 left
š” Hint
Check how to specify polar projection when creating axes.
ā Incorrect
The 'projection' argument must be passed to subplots(), not to plot(). Passing it to plot() causes a TypeError.
ā visualization
advanced2:00remaining
Effect of changing theta zero location
What is the visual effect of running this code on the polar plot?
Matplotlib
import matplotlib.pyplot as plt import numpy as np theta = np.linspace(0, 2 * np.pi, 100) r = np.abs(np.cos(2 * theta)) fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) ax.set_theta_zero_location('N') plt.show()
Attempts:
2 left
š” Hint
Think about what 'set_theta_zero_location' controls in polar plots.
ā Incorrect
Setting theta zero location to 'N' moves the zero angle from the default east (right) to north (top) of the plot.
š Application
expert3:00remaining
Calculate area enclosed by polar curve
Given the polar curve r(Īø) = 2 + cos(Īø), what is the approximate area enclosed by one full rotation (0 to 2Ļ)? Use numerical integration with numpy and matplotlib.
Matplotlib
import numpy as np theta = np.linspace(0, 2 * np.pi, 1000) r = 2 + np.cos(theta) area = np.trapz(0.5 * r**2, theta) print(round(area, 3))
Attempts:
2 left
š” Hint
Area enclosed by polar curve = integral of 0.5 * r^2 dĪø over 0 to 2Ļ.
ā Incorrect
Numerical integration of 0.5 * (2 + cos(Īø))^2 from 0 to 2Ļ gives approximately 14.137.