Last Updated: July 15, 2025
Basic Plots
| Plot Type | Code |
|---|---|
| Line | plt.plot(x, y) |
| Scatter | plt.scatter(x, y, c=colors, s=sizes) |
| Bar | plt.bar(labels, values) |
| Histogram | plt.hist(data, bins=20) |
| Pie | plt.pie(values, labels=labels) |
Customizing Plots
| Element | Code |
|---|---|
| Title | plt.title("My Plot") |
| Labels | plt.xlabel("X"); plt.ylabel("Y") |
| Legend | plt.legend(["Series A", "Series B"]) |
| Colors | plt.plot(x, y, color='red', linestyle='--', marker='o') |
| Grid | plt.grid(True, alpha=0.3) |
Subplots
fig, axes = plt.subplots(2, 2, figsize=(10,8))2x2 grid of subplots
axes[0,0].plot(x, y)Plot in top-left subplot
plt.tight_layout()Auto-adjust spacing between subplots
Saving & Display
| Action | Code |
|---|---|
| Show | plt.show() |
| Save | plt.savefig("plot.png", dpi=150, bbox_inches='tight') |
Pro Tip: Always use plt.tight_layout() before saving — it prevents labels and titles from being cut off. For publication-quality figures, save at 300 DPI and use bbox_inches='tight' to trim whitespace.