Sub topic 8
Plots that can be read without you
This chapter assumes you can run pandas and matplotlib code in a notebook and know what a histogram and a box plot show. Here you will learn to choose a plot for the question you are asking and to label it so a reader can understand it without you explaining it.
Pick the plot from the question
Start with the question and the data types involved. The plot follows from those.
| Question | Variables | Plot |
|---|---|---|
| How is one numeric value spread? | One numeric | Histogram |
| How does a numeric value differ across groups? | Numeric and category | Box plot |
| How do two numeric values move together? | Two numeric | Scatter plot |
| How many rows fall in each category? | One category | Bar chart |
Label every plot
A plot is readable on its own when it has four things:
- A title that says what is shown, such as "Order amount distribution".
- An x-axis label and a y-axis label. Include units when there are any.
- A legend if more than one series appears.
- A size large enough to read.
figsize=(10, 4)works well for two side-by-side plots in a notebook.
The next cell draws a histogram and a grouped box plot. It assumes df is the cleaned orders DataFrame from the cleaning step.
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
df["amount"].plot.hist(bins=30, ax=axes[0])
axes[0].set_title("Order amount distribution")
axes[0].set_xlabel("Amount")
axes[0].set_ylabel("Number of orders")
df.boxplot(column="amount", by="region", ax=axes[1])
axes[1].set_title("Order amount by region")
axes[1].set_xlabel("Region")
axes[1].set_ylabel("Amount")
fig.suptitle("")
plt.tight_layout()
plt.show()Two details in this cell are easy to miss. pandas boxplot(by=...) adds its own figure-level title, so fig.suptitle("") clears it. The explicit titles and labels replace the default ones, which are often missing or vague.
Common errors
- Unlabeled axes. The reader cannot tell whether
amountis in dollars or thousands of dollars. - A bin count left at the default. Try two or three values for
bins. Too few hides the shape, and too many shows noise. - Plotting before cleaning. Missing or negative amounts distort a histogram. Plot the cleaned
df, notdf_raw, unless you are plotting to show a quality problem. - One plot doing too much. If you need a paragraph to explain a plot, split it into two.
Keep plots reproducible
Plot code should only read the data and draw. Do not change df inside a plotting cell. Otherwise the plot depends on the order in which cells were run, which is the hidden state problem covered later in this tutorial.
Exercise
A colleague opens your notebook and sees a histogram with no title and no axis labels. Name two things they cannot know from the plot, and name the matplotlib calls that fix them.
Show answer
They cannot know what variable is plotted or what units it uses, and they cannot tell what the bar heights count. Use set_title to name the plot, set_xlabel to name the variable with its units, and set_ylabel to say what is counted, such as "Number of orders".