Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a line plot using pyplot interface.
Matplotlib
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.draw() instead of plt.show() which does not display the plot window.
Calling plt.figure() which creates a new figure but does not display the plot.
✗ Incorrect
The plt.show() function displays the plot window when using pyplot interface.
2fill in blank
mediumComplete the code to create a figure and axes using the object-oriented interface.
Matplotlib
import matplotlib.pyplot as plt fig, [1] = plt.subplots() [1].plot([1, 2, 3], [3, 2, 1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'figure' instead of 'axes' which is the wrong object for plotting.
Using 'axis' which is not the correct variable name returned by plt.subplots().
✗ Incorrect
plt.subplots() returns a figure and axes object; the axes object is commonly named 'ax'.
3fill in blank
hardFix the error in the code by choosing the correct method to set the title using the axes object.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [3, 2, 1]) ax.[1]('My Plot') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ax.title() which does not exist for axes objects.
Using ax.label() which is unrelated to titles.
✗ Incorrect
The axes object uses set_title() method to set the plot title.
4fill in blank
hardFill both blanks to create a scatter plot using the axes interface and set x and y labels.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.[1]([1, 2, 3], [4, 5, 6]) ax.[2]('X axis') ax.set_ylabel('Y axis') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ax.plot() instead of ax.scatter() for scatter plot.
Using ax.xlabel() which is not a method.
✗ Incorrect
Use ax.scatter() to create scatter plot and ax.set_xlabel() to set x-axis label.
5fill in blank
hardFill all three blanks to create a bar chart using pyplot, set title and y label.
Matplotlib
import matplotlib.pyplot as plt plt.[1](['A', 'B', 'C'], [5, 7, 3]) plt.[2]('Category Counts') plt.[3]('Count') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() instead of plt.bar() for bar chart.
Using plt.xlabel() instead of plt.ylabel() for y-axis label.
✗ Incorrect
Use plt.bar() for bar chart, plt.title() for title, and plt.ylabel() for y-axis label.