0
0
Matplotlibdata~20 mins

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

Choose your learning style9 modes available
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.