Sub topic 1
Your first chart: calling plot on a DataFrame
Prerequisites
This chapter assumes you can do the following:
- Write basic Python: imports, lists, dictionaries and function calls.
- Create a pandas DataFrame and select columns from it.
- Run Python code in a script or notebook with matplotlib installed (
pip install matplotlib).
You don't need to know matplotlib itself. pandas calls it for you.
One call, one chart
Every DataFrame has a plot method. With no arguments, it draws a line chart. pandas hands the drawing work to matplotlib, a plotting library, so you still need matplotlib installed.
The example below uses a small table of monthly sales and costs.
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.set_index("month").plot(marker="o")
plt.show()This code produces one chart with two lines, one for sales and one for costs. The x axis shows the month names, and a legend names each line. marker="o" draws a dot at each data point.
What plot does with your data
The default rule is simple:
- The index supplies the x values.
- Each numeric column becomes one line.
- The column names become the legend labels.
The set_index("month") call matters here. A new DataFrame gets a default index of 0, 1, 2, 3. Without set_index, the x axis would show those numbers. The month column would be treated as data, and because it holds text, pandas would skip it.
The next chapter covers the x and y arguments in detail. For now, remember that the index is the default x axis.
Where plt.show fits
df.plot() builds the chart but does not always display it. In a script, you need plt.show() to open a window. In a Jupyter notebook, the chart usually appears as soon as the cell finishes, so plt.show() is optional there.
The method also returns a matplotlib Axes object, the drawing area that holds the chart. You can ignore it for now. A later chapter shows how to use it for finer control and for saving files.
Plot a single column
You can also call plot on one column. A single column is a Series, and a Series has the same method. It draws one line.
df.set_index("month")["sales"].plot(marker="o")
plt.show()Selecting with double brackets, such as [["sales"]], keeps a DataFrame instead. Use a list of column names whenever you want a subset of columns on the same chart.
What happens when it fails
The most common first error is this message:
TypeError: no numeric data to plotIt means that after pandas dropped the non-numeric columns, nothing was left to draw. Two causes are common:
- Every column holds text, so none can be plotted.
- Numbers were read in as strings, for example from a CSV with stray symbols. Check
df.dtypesand convert withpd.to_numeric.
A later chapter covers other pitfalls, such as missing data and crowded labels.
Summary of the pattern
- Put the labels you want on the x axis into the index with
set_index. - Keep only numeric columns you want to compare.
- Call
.plot(). - Call
plt.show()if you are running a script.
Exercise
You run df.plot() on the sales table without calling set_index("month"). What appears on the x axis, and why is there no line for the month column?
Show answer
The x axis shows the default index values 0, 1, 2, 3. The month column holds text, so pandas skips it and plots only the numeric columns, sales and costs.