Bird
Raised Fist0
Matplotlibdata~20 mins

Polar axes in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 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π.

Practice

(1/5)
1. What does setting projection='polar' do in a matplotlib plot?
easy
A. It creates a 3D plot with depth perspective.
B. It creates a plot with circular axes using angles and radius.
C. It changes the plot background to a polar color scheme.
D. It rotates the plot by 90 degrees.

Solution

  1. Step 1: Understand the projection parameter

    The projection argument in matplotlib defines the coordinate system for the plot.
  2. Step 2: Identify what 'polar' projection means

    Setting projection='polar' changes the axes to polar coordinates, which use angles and radius instead of x and y.
  3. Final Answer:

    It creates a plot with circular axes using angles and radius. -> Option B
  4. Quick Check:

    projection='polar' = circular axes [OK]
Hint: Remember: 'polar' means angle + radius axes [OK]
Common Mistakes:
  • Confusing polar projection with 3D plotting
  • Thinking it changes colors or rotates plot
  • Assuming it creates Cartesian axes
2. Which of the following is the correct way to create a polar plot in matplotlib?
easy
A. plt.plot(projection='polar')
B. plt.axis(polar=True)
C. plt.polar()
D. plt.subplot(projection='polar')

Solution

  1. Step 1: Recall how to create polar axes

    In matplotlib, plt.subplot(projection='polar') creates a polar subplot.
  2. Step 2: Check other options for correctness

    plt.plot() does not accept projection, plt.polar() is deprecated, and plt.axis(polar=True) does not create new axes.
  3. Final Answer:

    plt.subplot(projection='polar') -> Option D
  4. Quick Check:

    Use subplot with projection='polar' [OK]
Hint: Use plt.subplot(projection='polar') to start polar plots [OK]
Common Mistakes:
  • Using plt.plot with projection argument
  • Trying deprecated plt.polar() function
  • Passing polar=True instead of projection='polar'
3. What will the following code display?
import matplotlib.pyplot as plt
import numpy as np

ax = plt.subplot(projection='polar')
theta = np.linspace(0, 2*np.pi, 100)
r = np.abs(np.sin(3*theta))
ax.plot(theta, r)
plt.show()
medium
A. A polar plot with three 'petals' shaped by sine function.
B. A line plot with sine wave on Cartesian axes.
C. A scatter plot of random points on polar axes.
D. An error because np.abs cannot be used with polar plots.

Solution

  1. Step 1: Analyze the data arrays

    theta is angles from 0 to 2π, r is absolute sine of 3 times theta, creating 3 cycles.
  2. Step 2: Understand the plot type

    Plotting r vs theta on polar axes draws a 3-petal shape because sine(3θ) repeats 3 times around the circle.
  3. Final Answer:

    A polar plot with three 'petals' shaped by sine function. -> Option A
  4. Quick Check:

    Polar plot of |sin(3θ)| = 3 petals [OK]
Hint: sin(3θ) on polar plot makes 3 petals [OK]
Common Mistakes:
  • Thinking it plots Cartesian sine wave
  • Confusing plot type with scatter
  • Assuming np.abs causes error
4. Identify the error in this code snippet for a polar plot:
import matplotlib.pyplot as plt
ax = plt.subplot()
ax.plot([0, 1, 2], [0, 1, 0.5])
plt.show()
medium
A. Missing projection='polar' in subplot creation.
B. Plot data lists have different lengths.
C. plt.show() is called before plotting.
D. Using plt.subplot() instead of plt.subplots() causes error.

Solution

  1. Step 1: Check subplot creation

    The code uses plt.subplot() without projection='polar', so axes are Cartesian by default.
  2. Step 2: Understand impact on polar plotting

    Without polar projection, the plot is not polar. To create polar plot, projection='polar' must be specified.
  3. Final Answer:

    Missing projection='polar' in subplot creation. -> Option A
  4. Quick Check:

    Polar plot needs projection='polar' [OK]
Hint: Always add projection='polar' for polar axes [OK]
Common Mistakes:
  • Assuming plt.subplot() defaults to polar
  • Thinking plt.show() order matters here
  • Confusing plt.subplot() and plt.subplots()
5. You want to plot wind direction and speed data on a polar plot. Which approach correctly maps wind direction (degrees) to angle and speed to radius in matplotlib?
hard
A. Use wind direction in degrees as theta without conversion; plot with projection='polar'.
B. Use wind direction directly as radius and speed as theta; plot with projection='polar'.
C. Convert wind direction to radians, use as theta; use speed as radius; plot with projection='polar'.
D. Plot wind speed vs wind direction on Cartesian axes with projection='polar'.

Solution

  1. Step 1: Understand polar plot axes

    Polar plots use angles in radians for theta and distances for radius.
  2. Step 2: Map wind data correctly

    Wind direction in degrees must be converted to radians for theta; wind speed is radius. Plotting with projection='polar' shows correct directional data.
  3. Final Answer:

    Convert wind direction to radians, use as theta; use speed as radius; plot with projection='polar'. -> Option C
  4. Quick Check:

    Degrees to radians for theta, speed as radius [OK]
Hint: Convert degrees to radians for polar angles [OK]
Common Mistakes:
  • Using degrees directly without conversion
  • Swapping radius and angle values
  • Plotting on Cartesian axes expecting polar behavior
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard