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

Customizing plots

Titles, labels, colours and sizes are set with plot arguments or the returned Axes.

In this page:

  1. Customizing plots
Syntax
python
df.plot(title="title", xlabel="x label", ylabel="y label",
        color="color", figsize=(width, height), grid=True, legend=True)

Customizing plots

plot() accepts title, xlabel, ylabel, color, figsize, grid and legend. For more control, call methods on the returned Axes such as ax.set_title. plt.tight_layout tidies spacing.

Note: Set figsize=(8, 4) to control the aspect ratio.

Example: Customizing plots

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"month": [1, 2, 3], "sales": [10, 15, 12]})
ax = df.plot(x="month", y="sales", title="Monthly sales", color="green", figsize=(6, 3), grid=True)
ax.set_xlabel("Month")
ax.set_ylabel("Units")
print(ax.get_title(), "|", ax.get_xlabel(), "|", ax.get_ylabel())

# Output:
# Monthly sales | Month | Units
Related Topics
Common Mistakes
  1. Setting labels on the wrong object
  2. Forgetting legends on multiple lines
  3. Cramped figures without tight_layout
Chapter Summary
  • Arguments set titles and labels
  • The Axes offers finer control
  • figsize sets the size
  • tight_layout fixes spacing
🔒

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.