Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() instead of plt.boxplot()
Using plt.hist() which creates a histogram, not a box plot
✗ Incorrect
The plt.boxplot() function creates a box plot for the given data.
2fill in blank
mediumComplete 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'
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
✗ Incorrect
The plt.title() function adds a title to the plot.
3fill in blank
hardFix 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'
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
✗ Incorrect
To plot multiple data sets in one box plot, pass a list of lists: [data1, data2].
4fill in blank
hardFill 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'
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
✗ Incorrect
The labels parameter sets the box labels. Use plt.title() to add a title.
5fill in blank
hardFill 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'
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
✗ Incorrect
Use notch=True for notch style, labels for box labels, and plt.ylabel() to label the y-axis.