0
0
Matplotlibdata~20 mins

Polar axes in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
šŸŽ–ļø
Polar Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2: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()
AA line plot with theta on x-axis and r on y-axis
BA scatter plot with points randomly distributed in polar coordinates
CA polar plot showing three 'petals' formed by the sine function's absolute value
DA bar chart with bars at angles theta and heights r
Attempts:
2 left
šŸ’” Hint
Think about how the sine function with frequency 3 affects the shape in polar coordinates.
ā“ data_output
intermediate
1: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())
A4
B6
C12
D8
Attempts:
2 left
šŸ’” Hint
Check the default number of angular ticks matplotlib uses for polar plots.
šŸ”§ Debug
advanced
2: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()
ATypeError: plot() got an unexpected keyword argument 'projection'
BValueError: x and y must be the same size
CNo error, plots a polar plot correctly
DAttributeError: 'AxesSubplot' object has no attribute 'plot'
Attempts:
2 left
šŸ’” Hint
Check how to specify polar projection when creating axes.
ā“ visualization
advanced
2: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()
AThe zero angle is at the top (north) of the plot instead of the right (east)
BThe plot is flipped upside down
CThe radius labels move to the top of the plot
DThe plot changes to a Cartesian coordinate system
Attempts:
2 left
šŸ’” Hint
Think about what 'set_theta_zero_location' controls in polar plots.
šŸš€ Application
expert
3: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))
A9.424
B14.137
C12.566
D4.712
Attempts:
2 left
šŸ’” Hint
Area enclosed by polar curve = integral of 0.5 * r^2 dĪø over 0 to 2Ļ€.