df.plot() basics
df.plot() draws a chart from your data in one line, using Matplotlib behind the scenes.
In this page:
Syntax
df.plot(kind="line")
df["column"].plot(kind="bar")
df.plot() basics
Both Series and DataFrames have a .plot() method. The kind argument chooses the chart type: line (default), bar, hist, box, scatter and more.
The result is a Matplotlib Axes object you can customize.
Below the figure is rendered off-screen so we can confirm it worked.
Note:
In scripts call plt.show(); notebooks display figures automatically.
Example: df.plot() basics
import io
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"x": [1, 2, 3, 4], "y": [1, 4, 9, 16]})
ax = df.plot(x="x", y="y")
buf = io.BytesIO()
plt.savefig(buf, format="png")
print(type(ax).__name__)
print("rendered:", buf.getbuffer().nbytes > 0)
# Output:
# Axes
# rendered: True
Related Topics
Common Mistakes
- Forgetting plt.show in scripts
- Plotting text columns
- Not installing Matplotlib
Chapter Summary
- .plot() charts data
- kind picks the chart type
- It returns a Matplotlib Axes
- Requires Matplotlib
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: