Sub topic 2
What goes on the x and y axes: the index, x and y
This chapter assumes you can build a DataFrame, select columns, and have matplotlib installed. It explains how DataFrame.plot decides what to draw on each axis.
The default rule
With no arguments, plot uses the index as the x axis. It draws one line per numeric column, and each column is a y series. The column names become the legend labels.
A default integer index (0, 1, 2, ...) gives you a meaningless x axis. That is the most common first surprise. Here the month names are an ordinary column, so they are not used as x values.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
"month": ["Jan", "Feb", "Mar", "Apr"],
"sales": [120, 135, 150, 170],
"costs": [80, 90, 95, 110],
})
df.plot()
plt.show()The x axis shows 0 to 3. The month column is skipped because it holds text, not numbers.
Option 1: move the labels into the index
set_index turns a column into the index. After that, plot uses it as the x axis.
df.set_index("month").plot(marker="o")
plt.show()You now get two lines, sales and costs, with Jan to Apr along the x axis. marker="o" draws a dot at each data point.
Option 2: name the columns with x and y
The x and y arguments choose columns explicitly. You do not have to change the index.
df.plot(x="month", y=["sales", "costs"], marker="o")
plt.show()y accepts a single column name or a list of names. If you give x but leave out y, pandas plots all remaining numeric columns against x.
Which approach to use
| Situation | Use |
|---|---|
| The labels are already the index | `df.plot()` |
| The labels are in a column and you want all numeric columns | `df.set_index("col").plot()` |
| You want only some columns, without changing the DataFrame | `df.plot(x="col", y=[...])` |
How the mapping works
Plotting a single column
Selecting one column gives a Series. A Series has an index but no columns, so it draws a single line against its index.
df.set_index("month")["sales"].plot()
plt.show()Common errors
TypeError: no numeric data to plot: every column you selected holds text or other non-numeric values. Checkdf.dtypes, and convert withpd.to_numericif numbers were read in as strings.- A column vanishes from the chart: it is non-numeric, so
plotskipped it. This happens silently when you do not passy. - Passing
xas a list:xtakes one column name. Useset_indexwith several columns only if you want a multi-level axis, which is rarely what you need for a quick chart. - Using the wrong tool for x: if you pass
x="month"and also want other columns, do not putmonthiny.
Exercise
You have a DataFrame with columns "city", "temp" and "rain", and the default integer index. Write one line that plots both temp and rain as lines with city names on the x axis, without modifying df.
Show answer
df.plot(x="city", y=["temp", "rain"])
Exercise
Why does df.plot() on a DataFrame with a text column "month" and numeric columns "sales" and "costs" show 0, 1, 2, 3 on the x axis?
Show answer
The default index is the integers 0 to n-1, and plot uses the index for x. The month column is text, so it is not used as x and is skipped as a y series. Use set_index("month") or x="month" to fix it.