Customizing plots
Titles, labels, colours and sizes are set with plot arguments or the returned Axes.
In this page:
Syntax
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
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
- Setting labels on the wrong object
- Forgetting legends on multiple lines
- 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: