Chapter 14 Quiz: Introduction to Data Visualization with matplotlib

Instructions: Choose the best answer for each question. The answer key with explanations is at the end.


Questions

1. In matplotlib's object-oriented interface, which code correctly creates a Figure with two side-by-side Axes?

A) fig, ax = plt.subplots()

B) fig, (ax1, ax2) = plt.subplots(1, 2)

C) fig = plt.figure(); ax1, ax2 = fig.add_axes([0,0,1,2])

D) fig, ax1, ax2 = plt.subplots(cols=2)


2. What is the primary difference between the pyplot interface and the object-oriented interface in matplotlib?

A) The pyplot interface is faster; the OO interface is slower

B) The pyplot interface uses implicit state (current Figure/Axes) while the OO interface uses explicit Figure and Axes objects

C) The pyplot interface supports more chart types than the OO interface

D) The OO interface can only create one chart per script


3. Priya wants her bar chart's y-axis to display $292K instead of 292000. Which code achieves this?

A) ax.set_ylabel("$K")

B) ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f"${x/1000:.0f}K"))

C) ax.format_yaxis("${:.0f}K")

D) ax.yaxis.format = "currency_k"


4. You have a bar chart where the tallest bar is $292,000. You add the line ax.set_ylim(250000, 320000) to zoom in. Why is this almost always a bad practice for bar charts?

A) It makes the bars shorter and harder to see

B) It truncates the y-axis so the bars no longer start at zero, making small differences appear enormous and misleading readers about relative magnitudes

C) matplotlib will throw an error if the ylim lower bound is not zero

D) It changes the bar colors


5. What is the correct method for adding horizontal reference line to an Axes at y=45,000?

A) ax.draw_line(y=45000, horizontal=True)

B) ax.add_line(45000, orientation="h")

C) ax.axhline(45000, color="gray", linestyle="--")

D) ax.hline(y=45000)


6. You create a histogram with ax.hist(order_values, bins=5). A colleague says the chart "oversimplifies the distribution." What does this mean, and what would you change?

A) Increasing the number of bins (e.g., to 20–25) would reveal finer detail in how the values are distributed

B) Decreasing the number of bins would show more detail

C) The bins parameter controls the color, not the shape

D) Histograms always have exactly 10 bins; the parameter is ignored


7. Which of the following is the correct way to save a figure as a PNG at 300 DPI, with a white background?

A) plt.save("file.png", resolution=300)

B) fig.savefig("file.png", dpi=300, facecolor="white", bbox_inches="tight")

C) plt.export("file.png", quality=300)

D) ax.savefig("file.png", dpi=300)


8. A product has the name "Professional Services Bundle" — 30 characters long. You are building a bar chart comparing 6 products by revenue. Why should you use a horizontal bar chart instead of a vertical bar chart?

A) Horizontal bars are always more accurate than vertical bars

B) Long category labels on a vertical bar chart either get truncated, overlap, or require awkward rotation; horizontal bars place labels on the y-axis where there is more room

C) matplotlib cannot display labels longer than 15 characters on vertical bars

D) There is no difference; choose based on personal preference


9. What does ax.spines["top"].set_visible(False) do?

A) Hides the tick marks on the top of the chart

B) Removes the top border line of the plot area, giving the chart a cleaner, more modern look

C) Hides the title of the chart

D) Changes the top spine color to white


10. You have a line chart with four regions (North, South, East, West). You write ax.legend() but the legend shows "Series 1, Series 2, Series 3, Series 4." What did you forget?

A) To call ax.set_legend_labels(["North", "South", "East", "West"])

B) To pass label="North" (and similar) to each ax.plot() call

C) To use ax.legend(labels=["North","South","East","West"])

D) To save the chart before showing the legend


11. What is the purpose of plt.tight_layout() ?

A) It compresses the chart to use less disk space when saved

B) It adjusts subplot parameters to prevent axis labels, titles, and other elements from overlapping or being clipped

C) It makes all lines thicker so they are easier to read

D) It removes all whitespace from the figure


12. Which chart type is most appropriate for answering the question "How is our total monthly revenue trending over the year?"

A) Pie chart

B) Scatter plot

C) Line chart

D) Histogram


13. You want to compare the revenue of 4 regions in Q1 and Q2 side by side. You decide to use a grouped bar chart. Which code correctly positions the Q2 bars to the right of the Q1 bars?

A)

ax.bar(x, q1_vals, width=0.35, label="Q1")
ax.bar(x, q2_vals, width=0.35, label="Q2")

B)

ax.bar([i - 0.35/2 for i in x], q1_vals, width=0.35, label="Q1")
ax.bar([i + 0.35/2 for i in x], q2_vals, width=0.35, label="Q2")

C)

ax.barh(x, q1_vals, width=0.35, label="Q1", position="left")
ax.barh(x, q2_vals, width=0.35, label="Q2", position="right")

D)

ax.grouped_bar([q1_vals, q2_vals], labels=["Q1","Q2"])

14. What does ax.fill_between(x, y1, y2, alpha=0.1, color="blue") do?

A) Draws a solid blue rectangle between y1 and y2

B) Fills the area between two lines or between a line and a baseline with a semi-transparent blue shading

C) Creates two separate blue lines at y1 and y2

D) Fills the bars of a bar chart between the heights y1 and y2


15. Which pandas .plot() call would produce a horizontal bar chart?

A) df.plot(kind="bar", horizontal=True)

B) df.plot(kind="hbar")

C) df.plot(kind="barh")

D) df.barh()


16. What does the bottom parameter do in ax.bar(x, values, bottom=previous_values)?

A) Moves the bars down by the specified number of pixels

B) Creates a stacked bar chart by setting the starting height of each bar to previous_values instead of zero

C) Sets the minimum y-axis value

D) Draws a horizontal line at the bottom of each bar


17. Priya creates a scatter plot and adds ax.scatter(x, y, s=deal_count). The s parameter represents what?

A) The slope of a trend line

B) The marker size — larger s values produce larger markers

C) The series index number

D) The transparency of each point


18. You are presenting a report to Sandra Chen and want to visualize market share across 4 regions. Sandra suggests a pie chart; your instinct says bar chart. Which is the stronger argument for the bar chart?

A) Pie charts require a paid matplotlib license

B) Human perception of angle/arc length is less accurate than perception of bar height, making it harder to precisely compare regional values in a pie chart

C) Pie charts are deprecated in modern matplotlib and should be avoided

D) matplotlib's pie chart does not support 4 segments


19. What happens when you call plt.show() in a script (not a Jupyter notebook)?

A) It saves the figure to a file

B) It opens an interactive window displaying the figure; the script pauses until the window is closed

C) It prints a text representation of the figure to the console

D) Nothing — plt.show() only works in Jupyter


20. You create a 2×2 subplot grid with fig, axes = plt.subplots(2, 2). How do you access the Axes in the bottom-right position?

A) axes[3]

B) axes["bottom_right"]

C) axes[1][1]

D) axes.bottom_right


Answer Key

Q Answer Explanation
1 B plt.subplots(1, 2) creates 1 row, 2 columns. The tuple unpacking (ax1, ax2) gives you named references.
2 B pyplot maintains a global "current figure" and "current axes" — convenient but fragile in complex scripts. The OO interface uses explicit fig and ax objects, which is safer and more predictable.
3 B plt.FuncFormatter takes a callable (value, tick_position) and returns a formatted string.
4 B A truncated y-axis on a bar chart is one of the most common ways charts mislead. Bars that should look "slightly taller" look dramatically different when the baseline is not zero. Line charts can use a zoomed range because the line itself encodes trend, but bar charts rely on height from zero.
5 C ax.axhline(y_value) draws a horizontal line across the full width of the Axes at the specified y value. ax.axvline() is the vertical equivalent.
6 A Fewer bins average over more data and hide the shape of the distribution. Increasing the bin count (to ~15–30 for most business data) shows the actual distribution shape.
7 B fig.savefig() is called on the Figure object. dpi=300 sets resolution, facecolor="white" prevents a transparent background, bbox_inches="tight" prevents label clipping.
8 B Long category names are the primary motivation for horizontal bar charts. The y-axis provides ample horizontal space for labels.
9 B Removing the top and right spines is a standard style choice that reduces visual clutter and gives charts a cleaner appearance without losing any information.
10 B The label keyword argument in ax.plot() is what ax.legend() reads to create the legend entries. Without it, matplotlib uses default names like "_line0".
11 B tight_layout() automatically adjusts subplot margins to prevent text overlap and clipping. It is almost always the right thing to call before savefig().
12 C A line chart is the correct choice for showing how a continuous variable changes over an ordered time sequence. The line connecting points implies continuity and trend.
13 B The x-positions must be manually offset by half the bar width. Q1 bars go at i - width/2 and Q2 bars at i + width/2, centering the pair around each x position.
14 B fill_between shades the region between two y-value arrays. Combined with alpha for transparency, it creates a visual context band (like a confidence interval or a "healthy range" band).
15 C kind="barh" produces a horizontal bar chart in pandas .plot().
16 B The bottom parameter tells each bar where to start its base. Passing the first series' values as bottom for the second series creates a stacked bar chart where the second series sits on top of the first.
17 B In ax.scatter(), s controls the marker size in points^2. Setting s=deal_count creates a bubble chart where bubbles are proportional to the deal count.
18 B Human perception of comparative length (bar heights) is significantly more accurate than perception of angle or arc length (pie segments). When precision in comparison matters, bar charts are superior.
19 B In a script, plt.show() opens an interactive window and blocks the script until the window is closed. In Jupyter, the figure is rendered inline and plt.show() is optional.
20 C plt.subplots(2, 2) returns a 2D NumPy array indexed by [row][column], both 0-based. Bottom-right is row 1, column 1, so axes[1][1].

Score Interpretation

  • 18–20 correct: Excellent — you have a strong foundation in matplotlib.
  • 14–17 correct: Good — revisit the sections covering the topics you missed.
  • 10–13 correct: Fair — spend more time with chart formatting and subplot sections.
  • Below 10: Work through the code examples in the chapter before attempting the exercises.