Bird
Raised Fist0
Data Analysis Pythondata~10 mins

Scatter plots in Data Analysis Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a scatter plot using matplotlib.

Data Analysis Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.[1](x, y)
plt.show()
Drag options to blanks, or click blank then click option'
Ascatter
Bplot
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() which creates a line plot instead of scatter plot.
Using plt.bar() which creates bar charts, not scatter plots.
2fill in blank
medium

Complete the code to add labels to the x and y axes in the scatter plot.

Data Analysis Python
import matplotlib.pyplot as plt
x = [5, 7, 8, 7, 2]
y = [99, 86, 87, 88, 100]
plt.scatter(x, y)
plt.xlabel([1])
plt.ylabel('Scores')
plt.show()
Drag options to blanks, or click blank then click option'
A'Weight'
B'Height'
C'Age'
D'Temperature'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the label text inside quotes.
Using the wrong label that does not match the data.
3fill in blank
hard

Fix the error in the code to correctly plot a scatter plot with red points.

Data Analysis Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y, color=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Ared
B'r'
Cr
D'red'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing color without quotes causing a NameError.
Using full color name without quotes.
4fill in blank
hard

Fill both blanks to create a scatter plot with blue points and size 100.

Data Analysis Python
import matplotlib.pyplot as plt
x = [2, 4, 6, 8]
y = [5, 15, 25, 35]
plt.scatter(x, y, color=[1], s=[2])
plt.show()
Drag options to blanks, or click blank then click option'
A'blue'
B50
C100
D'green'
Attempts:
3 left
💡 Hint
Common Mistakes
Using color without quotes.
Setting size too small to notice.
5fill in blank
hard

Fill all three blanks to create a scatter plot with green points, size 80, and alpha transparency 0.5.

Data Analysis Python
import matplotlib.pyplot as plt
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
plt.scatter(x, y, color=[1], s=[2], alpha=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A'green'
B80
C0.5
D'yellow'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing color without quotes.
Using alpha values outside 0 to 1 range.
Setting size too small or too large.

Practice

(1/5)
1. What does a scatter plot primarily show in data analysis?
easy
A. The relationship between two numerical variables
B. The total count of categories
C. The average value of a dataset
D. The sum of all data points

Solution

  1. Step 1: Understand scatter plot purpose

    A scatter plot is used to display how two numerical variables relate to each other by plotting points on a graph.
  2. Step 2: Compare options to definition

    Options B, C, and D describe other chart types or calculations, not relationships between two variables.
  3. Final Answer:

    The relationship between two numerical variables -> Option A
  4. Quick Check:

    Scatter plot = relationship between two numbers [OK]
Hint: Scatter plots always compare two numeric sets [OK]
Common Mistakes:
  • Confusing scatter plots with bar charts
  • Thinking scatter plots show totals or averages
  • Ignoring that scatter plots need two numeric variables
2. Which of the following is the correct way to create a scatter plot using matplotlib in Python?
easy
A. plt.bar(x, y)
B. plt.plot(x, y, kind='scatter')
C. plt.scatterplot(x, y)
D. plt.scatter(x, y)

Solution

  1. Step 1: Recall matplotlib scatter syntax

    Matplotlib uses plt.scatter(x, y) to create scatter plots.
  2. Step 2: Check other options for correctness

    plt.plot(x, y, kind='scatter') is incorrect syntax; plt.plot does not take kind='scatter'. plt.bar(x, y) creates bar charts. plt.scatterplot(x, y) is not a valid matplotlib function.
  3. Final Answer:

    plt.scatter(x, y) -> Option D
  4. Quick Check:

    Matplotlib scatter = plt.scatter(x, y) [OK]
Hint: Use plt.scatter(x, y) for scatter plots in matplotlib [OK]
Common Mistakes:
  • Using plt.plot with kind='scatter' (wrong syntax)
  • Confusing scatter with bar plot functions
  • Trying to use plt.scatterplot which doesn't exist
3. What will the following code output?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y)
plt.title('Test Plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
medium
A. An error because plt.scatter requires more arguments
B. A scatter plot with points at (1,10), (2,20), (3,25), (4,30) and labeled axes
C. A bar chart with bars at x positions 1 to 4
D. A line plot connecting points (1,10), (2,20), (3,25), (4,30)

Solution

  1. Step 1: Analyze the code commands

    The code uses plt.scatter(x, y) which plots points at given x and y coordinates. Titles and labels are added correctly.
  2. Step 2: Understand the plot output

    The plot will show points at (1,10), (2,20), (3,25), (4,30) with axis labels and a title. No lines or bars are drawn.
  3. Final Answer:

    A scatter plot with points at (1,10), (2,20), (3,25), (4,30) and labeled axes -> Option B
  4. Quick Check:

    plt.scatter plots points, not lines or bars [OK]
Hint: plt.scatter plots points, not lines or bars [OK]
Common Mistakes:
  • Thinking plt.scatter draws lines between points
  • Confusing scatter plot with bar chart
  • Assuming plt.scatter needs extra arguments
4. Identify the error in this code snippet that tries to create a scatter plot:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5]
plt.scatter(x, y)
plt.show()
medium
A. plt.scatter requires a third argument for color
B. plt.show() is missing parentheses
C. x and y lists have different lengths causing an error
D. No error, code runs fine

Solution

  1. Step 1: Check lengths of x and y lists

    x has 3 elements, y has 2 elements. Scatter plot requires equal length for x and y.
  2. Step 2: Understand matplotlib error behavior

    Matplotlib will raise a ValueError because x and y lengths differ. Options A and C are incorrect because color is optional and plt.show() is correctly called.
  3. Final Answer:

    x and y lists have different lengths causing an error -> Option C
  4. Quick Check:

    Equal length x and y needed for scatter [OK]
Hint: x and y must be same length for scatter plots [OK]
Common Mistakes:
  • Ignoring length mismatch between x and y
  • Thinking color argument is mandatory
  • Assuming plt.show() can be called without parentheses
5. You have a dataset with columns 'Age' and 'Income'. You want to create a scatter plot to find if older people tend to earn more. Which steps should you follow to create a clear scatter plot in Python using pandas and matplotlib?
hard
A. Import pandas and matplotlib, extract 'Age' and 'Income' columns as x and y, use plt.scatter(x, y), add title and axis labels, then plt.show()
B. Use plt.bar with 'Age' as x and 'Income' as height, then add labels
C. Plot a line graph with 'Age' on x-axis and 'Income' on y-axis without labels
D. Create a pie chart showing distribution of 'Income' grouped by 'Age' categories

Solution

  1. Step 1: Extract relevant columns for scatter plot

    Use pandas to get 'Age' and 'Income' columns as x and y arrays for plotting.
  2. Step 2: Plot scatter and add labels

    Use plt.scatter(x, y) to plot points, add a title and axis labels for clarity, then display with plt.show().
  3. Final Answer:

    Import pandas and matplotlib, extract 'Age' and 'Income' columns as x and y, use plt.scatter(x, y), add title and axis labels, then plt.show() -> Option A
  4. Quick Check:

    Scatter plot with labels = Import pandas and matplotlib, extract 'Age' and 'Income' columns as x and y, use plt.scatter(x, y), add title and axis labels, then plt.show() [OK]
Hint: Extract columns, use plt.scatter, add labels, then show plot [OK]
Common Mistakes:
  • Using bar or pie charts instead of scatter for relationships
  • Skipping axis labels and titles
  • Plotting line graph which implies continuous trend
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