Skip to main content\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 22.5 Multiple Choice Questions
1.
csp-10-2-1: What is the standard way to import matplotlib's pyplot library in python?
import matplot as plt
Incorrect! matplot is not a library.
import matplotlib.pyplot as plt
Correct! This is the correct way to import the library.
from matplotlib import pyplot as plt
Incorrect! This is not the correct way to import.
import matplotlib pyplot as plt
Incorrect! There is a period between matplotlib and pyplot instead of space.
2.
csp-10-2-2: What is the correct way to plot a line graph?
plot(x, y)
Incorrect!
plt.plot(x, y)
Correct! This is the correct way.
plt(x, y)
Incorrect!
plot.plt(x, y)
Incorrect!
3.
csp-10-2-3: How do you set a title for a plot using matplotlib library?
plt.set.title("Title")
Incorrect! plt has no function set.
plt.Title("Title")
Incorrect! Title needs to be lowercased.
plt.title("Title")
Correct! This is the correct way to set a title.
plt.set_title("Title")
Incorrect! plt has no function set_title.
4.
csp-10-2-4: How do you set a title for the axis using matplotlib library?
ax.set_title("Axis Title")
Correct! This is the correct way to set axis title.
ax.title("Axis Title")
Incorrect! ax has no method title.
ax.axis_title("Axis Title")
Incorrect! ax has no method axis_title.
ax.setTitle("Axis Title")
Incorrect! ax has no method setTitle.
5.
csp-10-2-5: How to create a subplot in a figure with three plots side by side and two rows of subplots?
ax1 = fig.add_subplot(123)
Incorrect! This means row 1, column 2, and plot 3, which is not possible in a 1 * 2 figure.
ax1 = fig.add_subplot(231)
Correct! This is the correct way to create a subplot for this case.
ax1 = fig.add_subplot(321)
Incorrect! This means row 3, column 2 and plot 1. Row and column need to be switched.
ax1 = fig.add_subplot(3*2)
Incorrect! This is incorrect way to create subplot.
6.
csp-10-2-6: What is the function for creating a horizontal bar plot?
plt.bar_hor(x, y)
Incorrect! There is no function bar_hor.
plt.hbar(x, y)
Incorrect! There is no function hbar.
plt.barh(x, y)
Correct! This is the correct way to create a horizontal bar.
plt.bar(x, y)
Incorrect! This is to create a vertical bar.
7.
csp-10-2-7: Which line of code would create a line plot in form of red squares with x = x and y = y?
ax.plot(x, y, 'red', marker = 'square')
Incorrect!
ax.plot(x, y, 'R-', marker = 'sqr')
Incorrect!
ax.plot(x, y, 'r', 's')
Incorrect!
ax.plot(x, y, 'r-', marker = 's')
Correct! This is the correct way.
8.
csp-10-2-8: What single line of code will set the x and y labels and the title of a plot?
ax.set((xlabel, ylabel, title), ('Year', 'Amount', 'Title of Graph'))
Incorrect!
ax.(xlabel='Year', ylabel='Amount', title='Title of Graph')
Incorrect!
ax.set(xlabel='Year', ylabel='Amount', title='Title of Graph')
Correct! This is the correct way.
ax.set(xlabel='Year'), ax.set(ylabel='Amount'), ax.set(title='Title of Graph')
Incorrect!