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
3D Scatter Plots
📖 Scenario: You are a data analyst working with a dataset of points in 3D space. You want to visualize these points to understand their distribution and relationships.
🎯 Goal: Create a 3D scatter plot using matplotlib to visualize the points in three dimensions.
📋 What You'll Learn
Create three lists named x, y, and z with the exact values provided.
Create a variable color to specify the color of the points.
Use matplotlib to create a 3D scatter plot with the given data and color.
Display the plot using plt.show().
💡 Why This Matters
🌍 Real World
3D scatter plots help scientists and engineers visualize data points in three dimensions, such as geographic locations, sensor data, or experimental results.
💼 Career
Data analysts and data scientists use 3D scatter plots to explore complex datasets and communicate insights visually to teams and stakeholders.
Progress0 / 4 steps
1
Create the 3D data points
Create three lists called x, y, and z with these exact values: x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11], y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78], and z = [10, 15, 13, 15, 10, 15, 10, 15, 10, 15].
Matplotlib
Hint
Use square brackets to create lists and separate numbers with commas.
2
Set the color for the points
Create a variable called color and set it to the string 'green' to specify the color of the scatter points.
Matplotlib
Hint
Assign the string 'green' to the variable color using the equals sign.
3
Create the 3D scatter plot
Import matplotlib.pyplot as plt and Axes3D from mpl_toolkits.mplot3d. Then create a figure and add a 3D subplot. Use ax.scatter(x, y, z, c=color) to create the 3D scatter plot with the data and color.
Matplotlib
Hint
Use plt.figure() to create a figure and fig.add_subplot(111, projection='3d') to add a 3D plot.
4
Display the 3D scatter plot
Use plt.show() to display the 3D scatter plot.
Matplotlib
Hint
Call plt.show() to open the plot window.
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
Step 1: Understand the role of 3D scatter plots
3D scatter plots show points in three dimensions, helping to see relationships among three variables.
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.
Final Answer:
To visualize data points in three dimensions and observe patterns -> Option B
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
Step 1: Recall how to create 3D axes
In matplotlib, plt.axes(projection='3d') creates a 3D axes object.
Step 2: Check other options
plt.subplot and plt.subplots do not accept projection directly; plt.figure creates a figure, not axes.
Final Answer:
ax = plt.axes(projection='3d') -> Option A
Quick Check:
Use plt.axes with projection='3d' for 3D axes [OK]
Hint: Use plt.axes(projection='3d') to get 3D axes [OK]
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
Step 1: Check subplot creation for 3D
The code uses fig.add_subplot(111) without projection='3d', so it creates a 2D axes.
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.
Final Answer:
Missing projection='3d' in add_subplot, so 3D plotting fails -> Option D
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
Step 1: Understand color mapping in scatter
To color points by a variable, pass that variable to c= and specify cmap for colormap.
Step 2: Check correct parameter names
c is correct for colors; color or colors with string 'z' or colormap are incorrect.
Final Answer:
ax.scatter(x, y, z, c=z, cmap='viridis') -> Option C
Quick Check:
Use c=variable and cmap='name' for color mapping [OK]
Hint: Use c=values and cmap='name' to color points [OK]