Bird
Raised Fist0
Matplotlibdata~10 mins

Box plot with plt.boxplot in Matplotlib - 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 box plot of the data list using matplotlib.

Matplotlib
import matplotlib.pyplot as plt

data = [7, 8, 5, 6, 9, 10, 4]
plt.[1](data)
plt.show()
Drag options to blanks, or click blank then click option'
Ascatter
Bhist
Cboxplot
Dplot
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() instead of plt.boxplot()
Using plt.hist() which creates a histogram, not a box plot
2fill in blank
medium

Complete the code to add a title 'My Box Plot' to the plot.

Matplotlib
import matplotlib.pyplot as plt

data = [3, 5, 7, 9, 11]
plt.boxplot(data)
plt.[1]('My Box Plot')
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Bylabel
Cxlabel
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.xlabel() or plt.ylabel() which label axes, not the title
Using plt.legend() which adds a legend, not a title
3fill in blank
hard

Fix the error in the code to correctly display the box plot for multiple data sets.

Matplotlib
import matplotlib.pyplot as plt

data1 = [1, 2, 3, 4, 5]
data2 = [2, 3, 4, 5, 6]
plt.boxplot([1])
plt.show()
Drag options to blanks, or click blank then click option'
Adata1
B[data1, data2]
Cdata1 + data2
Ddata1, data2
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data1 and data2 separated by comma without brackets
Adding the two lists which merges data instead of separate boxes
4fill in blank
hard

Fill both blanks to create a box plot with custom labels for two data sets.

Matplotlib
import matplotlib.pyplot as plt

data1 = [10, 20, 30]
data2 = [15, 25, 35]
plt.boxplot([data1, data2], labels=[1])
plt.[2]('Custom Box Plot')
plt.show()
Drag options to blanks, or click blank then click option'
A['Set A', 'Set B']
B['Data1', 'Data2']
Ctitle
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect label format like a string instead of list
Using plt.xlabel() instead of plt.title() for the plot title
5fill in blank
hard

Fill all three blanks to create a box plot with notch style, custom labels, and a y-axis label.

Matplotlib
import matplotlib.pyplot as plt

data1 = [5, 7, 9]
data2 = [6, 8, 10]
plt.boxplot([data1, data2], [1]=True, labels=[2])
plt.[3]('Values')
plt.show()
Drag options to blanks, or click blank then click option'
Anotch
B['Group 1', 'Group 2']
Cylabel
Dxlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.xlabel() instead of plt.ylabel() for y-axis label
Not passing labels as a list
Forgetting to set notch=True for notch style

Practice

(1/5)
1. What does the plt.boxplot() function primarily show about a dataset?
easy
A. The exact values of all data points
B. The spread and center of the data using quartiles
C. Only the average value of the data
D. The correlation between two variables

Solution

  1. Step 1: Understand box plot purpose

    A box plot summarizes data distribution by showing quartiles and spread.
  2. Step 2: Identify what plt.boxplot() displays

    This function creates a box plot that visualizes median, quartiles, and outliers.
  3. Final Answer:

    The spread and center of the data using quartiles -> Option B
  4. Quick Check:

    Box plot = Data spread and center [OK]
Hint: Box plots show spread and center, not individual points [OK]
Common Mistakes:
  • Thinking boxplot shows all data points exactly
  • Confusing boxplot with scatter plot
  • Assuming boxplot shows correlation
2. Which of the following is the correct way to create a vertical box plot of data stored in a list named data using matplotlib?
easy
A. plt.boxplot(data, orientation='horizontal')
B. plt.boxplot(data, vertical=False)
C. plt.boxplot(data, vert=True)
D. plt.boxplot(data, vert='no')

Solution

  1. Step 1: Check parameter for vertical boxplot

    The parameter to control vertical or horizontal is vert, which accepts True or False.
  2. Step 2: Match correct syntax

    Using plt.boxplot(data, vert=True) creates a vertical box plot correctly.
  3. Final Answer:

    plt.boxplot(data, vert=True) -> Option C
  4. Quick Check:

    Use vert=True for vertical boxplot [OK]
Hint: Use vert=True for vertical boxplot in plt.boxplot [OK]
Common Mistakes:
  • Using wrong parameter name like 'vertical'
  • Passing string instead of boolean for vert
  • Confusing orientation parameter which plt.boxplot doesn't use
3. What will be the output of the following code?
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
data = np.random.normal(0, 1, 100)
plt.boxplot(data, notch=True)
plt.show()
medium
A. An error because notch=True is invalid
B. A scatter plot of the 100 data points
C. A histogram of the data distribution
D. A box plot with a notch showing median confidence interval

Solution

  1. Step 1: Understand the code

    The code generates 100 random normal values and plots a box plot with notch=True.
  2. Step 2: Interpret notch=True effect

    The notch creates a narrow region around the median to indicate confidence interval visually.
  3. Final Answer:

    A box plot with a notch showing median confidence interval -> Option D
  4. Quick Check:

    notch=True adds median confidence notch [OK]
Hint: notch=True adds median confidence notch in boxplot [OK]
Common Mistakes:
  • Thinking notch=True creates scatter or histogram
  • Assuming notch=True causes error
  • Confusing boxplot with other plot types
4. Identify the error in this code snippet that tries to plot a box plot:
import matplotlib.pyplot as plt
data = [1, 2, 3, 4, 5]
plt.boxplot(data, notch='yes')
plt.show()
medium
A. The 'notch' parameter should be a boolean, not a string
B. The data list must be a numpy array
C. plt.boxplot requires data to be a dictionary
D. plt.show() is missing

Solution

  1. Step 1: Check the 'notch' parameter type

    The 'notch' parameter expects a boolean (True or False), not a string like 'yes'.
  2. Step 2: Confirm data type and plt.show()

    Data as list is valid, and plt.show() is present, so no error there.
  3. Final Answer:

    The 'notch' parameter should be a boolean, not a string -> Option A
  4. Quick Check:

    notch must be True or False, not 'yes' [OK]
Hint: Use True/False for notch, not strings [OK]
Common Mistakes:
  • Passing string instead of boolean for notch
  • Thinking data must be numpy array
  • Forgetting plt.show() (not error here though)
5. You have two datasets: data1 and data2. You want to compare their distributions side by side using box plots with different colors and horizontal orientation. Which code snippet correctly achieves this?
hard
A. plt.boxplot([data1, data2], vert=False, patch_artist=True, boxprops=[dict(facecolor='lightblue'), dict(facecolor='orange')])
B. plt.boxplot(data1, data2, vert=True, color=['red', 'green'])
C. plt.boxplot([data1, data2], orientation='horizontal', colors=['blue', 'orange'])
D. plt.boxplot([data1, data2], vert=True, patch_artist=True, boxprops=dict(facecolor='red'))

Solution

  1. Step 1: Check data input format

    To plot multiple datasets side by side, pass a list of datasets: [data1, data2].
  2. Step 2: Verify horizontal orientation and color options

    Use vert=False for horizontal boxplots, patch_artist=True to fill boxes with color, and boxprops to set facecolor.
  3. Step 3: Evaluate options

    plt.boxplot([data1, data2], vert=False, patch_artist=True, boxprops=[dict(facecolor='lightblue'), dict(facecolor='orange')]) correctly uses these parameters. Options A, B, and D use invalid parameters or syntax. plt.boxplot([data1, data2], vert=True, patch_artist=True, boxprops=dict(facecolor='red')) is vertical, not horizontal.
  4. Final Answer:

    plt.boxplot([data1, data2], vert=False, patch_artist=True, boxprops=[dict(facecolor='lightblue'), dict(facecolor='orange')]) -> Option A
  5. Quick Check:

    Multiple data + vert=False + patch_artist for color [OK]
Hint: Use vert=False and patch_artist=True for colored horizontal boxplots [OK]
Common Mistakes:
  • Passing datasets separately instead of as list
  • Using wrong parameter names like 'orientation'
  • Forgetting patch_artist=True to fill colors
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