Bird
Raised Fist0
Matplotlibdata~20 mins

3D scatter plots in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
3D Scatter Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 3D scatter plot with color mapping
What will be the color of the point at coordinates (2, 3, 4) in the 3D scatter plot generated by the code below?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3]
y = [2, 3, 4]
z = [3, 4, 5]
colors = ['red', 'green', 'blue']

scatter = ax.scatter(x, y, z, c=colors)
plt.show()
AYellow
BGreen
CBlue
DRed
Attempts:
2 left
💡 Hint
Colors are assigned in order to the points based on the list provided.
data_output
intermediate
1:30remaining
Number of points plotted in 3D scatter plot
How many points will be plotted by the following 3D scatter plot code?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(0, 1, 10)
y = np.linspace(0, 1, 10)
z = np.linspace(0, 1, 10)

ax.scatter(x, y, z)
plt.show()
A1
B1000
C30
D10
Attempts:
2 left
💡 Hint
The scatter plot plots points for each index in the arrays.
visualization
advanced
2:00remaining
Effect of marker size on 3D scatter plot
Which option shows the correct effect of changing marker size in a 3D scatter plot?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.array([1, 2, 3])
y = np.array([2, 3, 4])
z = np.array([3, 4, 5])

ax.scatter(x, y, z, s=100)
plt.show()
APoints appear larger than default size
BPoints are connected by lines
CPoints are invisible
DPoints appear smaller than default size
Attempts:
2 left
💡 Hint
The 's' parameter controls marker size in scatter plots.
🔧 Debug
advanced
2:00remaining
Identify the error in 3D scatter plot code
What error will the following code produce when run?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111)

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]

ax.scatter(x, y, z)
plt.show()
ANo error, plot displays correctly
BAttributeError: 'AxesSubplot' object has no attribute 'scatter'
CTypeError: scatter() missing 1 required positional argument: 'zs'
DValueError: x, y, and z must be the same length
Attempts:
2 left
💡 Hint
Check if the subplot is created with 3D projection.
🚀 Application
expert
3:00remaining
Interpreting clustering from 3D scatter plot data
Given the 3D scatter plot data below, which cluster center is closest to the point (5, 5, 5)?
Matplotlib
import numpy as np

points = np.array([[1, 2, 3], [5, 5, 5], [9, 8, 7]])
cluster_centers = np.array([[0, 0, 0], [5, 5, 6], [10, 10, 10]])

# Calculate distances from point (5,5,5) to each cluster center
point = np.array([5, 5, 5])
distances = np.linalg.norm(cluster_centers - point, axis=1)
closest_index = np.argmin(distances)
closest_center = cluster_centers[closest_index]
closest_center
A[5 5 6]
B[0 0 0]
C[10 10 10]
D[1 2 3]
Attempts:
2 left
💡 Hint
Distance is shortest between points closest in all three dimensions.

Practice

(1/5)
1. What is the main purpose of using a 3D scatter plot in matplotlib?
easy
A. To display text annotations in 3D space
B. To visualize data points in three dimensions and observe patterns
C. To plot a line graph with three lines
D. To create a bar chart with three bars

Solution

  1. Step 1: Understand the role of 3D scatter plots

    3D scatter plots show points in three dimensions, helping to see relationships among three variables.
  2. Step 2: Compare with other plot types

    Bar charts and line graphs do not show points in 3D space, and text annotations are not the main purpose.
  3. Final Answer:

    To visualize data points in three dimensions and observe patterns -> Option B
  4. Quick Check:

    3D scatter plots = visualize points in 3D [OK]
Hint: 3D scatter plots show points in 3D space [OK]
Common Mistakes:
  • Confusing 3D scatter with bar or line plots
  • Thinking 3D scatter is for text annotations
  • Assuming 3D scatter plots show continuous surfaces
2. Which of the following is the correct way to create a 3D scatter plot axis in matplotlib?
easy
A. ax = plt.axes(projection='3d')
B. ax = plt.subplots(projection='3d')
C. ax = plt.figure(projection='3d')
D. ax = plt.subplot(projection='3d')

Solution

  1. Step 1: Recall how to create 3D axes

    In matplotlib, plt.axes(projection='3d') creates a 3D axes object.
  2. Step 2: Check other options

    plt.subplot and plt.subplots do not accept projection directly; plt.figure creates a figure, not axes.
  3. Final Answer:

    ax = plt.axes(projection='3d') -> Option A
  4. Quick Check:

    Use plt.axes with projection='3d' for 3D axes [OK]
Hint: Use plt.axes(projection='3d') to get 3D axes [OK]
Common Mistakes:
  • Using plt.subplot instead of plt.axes for 3D
  • Passing projection to plt.figure instead of axes
  • Confusing plt.subplots with plt.subplot
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter([1, 2], [3, 4], [5, 6], c='r', marker='o')
plt.show()
medium
A. A 3D scatter plot with two red circular points at coordinates (1,3,5) and (2,4,6)
B. A 2D scatter plot with red points
C. A syntax error due to missing import
D. An empty plot with no points

Solution

  1. Step 1: Analyze the code for 3D scatter plot creation

    The code creates a figure, adds a 3D subplot, and plots two points with coordinates (1,3,5) and (2,4,6) in red circles.
  2. Step 2: Confirm the plot output

    The points will appear in 3D space as red circles; no errors occur.
  3. Final Answer:

    A 3D scatter plot with two red circular points at coordinates (1,3,5) and (2,4,6) -> Option A
  4. Quick Check:

    3D scatter with given points = red circles at (1,3,5) and (2,4,6) [OK]
Hint: Check coordinates and color for scatter points [OK]
Common Mistakes:
  • Thinking it creates 2D plot instead of 3D
  • Assuming syntax error without checking imports
  • Expecting no points plotted
4. Identify the error in this code that tries to plot a 3D scatter plot:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter([1,2,3], [4,5,6], [7,8,9])
plt.show()
medium
A. scatter function does not accept three arguments
B. The lists for x, y, z have different lengths
C. plt.figure() is not imported correctly
D. Missing projection='3d' in add_subplot, so 3D plotting fails

Solution

  1. Step 1: Check subplot creation for 3D

    The code uses fig.add_subplot(111) without projection='3d', so it creates a 2D axes.
  2. Step 2: Understand scatter with 3D data

    On 2D axes, passing three lists to scatter will treat the third list as point sizes instead of z-coordinates, producing a 2D scatter plot rather than 3D.
  3. Final Answer:

    Missing projection='3d' in add_subplot, so 3D plotting fails -> Option D
  4. Quick Check:

    3D scatter needs projection='3d' [OK]
Hint: Always add projection='3d' for 3D plots [OK]
Common Mistakes:
  • Forgetting projection='3d' in add_subplot
  • Thinking scatter can't take three arguments
  • Assuming list length mismatch causes error
5. You want to plot a 3D scatter plot with points colored by their z-value using a colormap. Which code snippet correctly achieves this?
hard
A. ax.scatter(x, y, z, c='z', colormap='viridis')
B. ax.scatter(x, y, z, color='z', cmap='viridis')
C. ax.scatter(x, y, z, c=z, cmap='viridis')
D. ax.scatter(x, y, z, colors=z, cmap='viridis')

Solution

  1. Step 1: Understand color mapping in scatter

    To color points by a variable, pass that variable to c= and specify cmap for colormap.
  2. Step 2: Check correct parameter names

    c is correct for colors; color or colors with string 'z' or colormap are incorrect.
  3. Final Answer:

    ax.scatter(x, y, z, c=z, cmap='viridis') -> Option C
  4. Quick Check:

    Use c=variable and cmap='name' for color mapping [OK]
Hint: Use c=values and cmap='name' to color points [OK]
Common Mistakes:
  • Using color='z' instead of c=z
  • Using colormap instead of cmap
  • Passing colors=z which is invalid