← Back to Pandas Course | Chapter 12: Visualization Basics | Lesson 1 of 6

df.plot() basics

df.plot() draws a chart from your data in one line, using Matplotlib behind the scenes.

In this page:

  1. df.plot() basics
Syntax
python
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

python
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
  1. Forgetting plt.show in scripts
  2. Plotting text columns
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.