Introduction
Matplotlib offers extensive customization options for creating publication-quality figures.
Styling
# Line styles
plt.plot(x, y, linestyle="--", linewidth=2, color="red")
# Markers
plt.plot(x, y, marker="o", markersize=5)
# Combined
plt.plot(x, y, "b-", label="Line 1")
plt.plot(x2, y2, "ro", label="Points")
# Grid
plt.grid(True, linestyle="--", alpha=0.7)
Labels and Legends
plt.xlabel("X Label", fontsize=12, fontweight="bold")
plt.ylabel("Y Label", fontsize=12)
plt.title("Title", fontsize=14, pad=20)
plt.legend(loc="upper right")
plt.tick_params(axis="both", labelsize=10)
Saving Figures
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
plt.savefig("plot.pdf") # Vector format
plt.savefig("plot.svg") # SVG format
Themes
plt.style.use("seaborn-v0_8-darkgrid")
plt.style.use("ggplot")
plt.style.use("default")
Practice Problems
- Create multi-line chart with legend
- Add annotations to key points
- Customize axis limits and ticks
- Save figure in different formats
- Apply different style themes