Quick reference for the Matplotlib charting syntax covered in Module 15.
| Chart | Syntax |
|---|---|
| Line | plt.plot(x, y) |
| Bar | plt.bar(x, y) |
| Horizontal Bar | plt.barh(x, y) |
| Pie | plt.pie(values, labels=labels) |
| Histogram | plt.hist(data, bins=20) |
| Scatter | plt.scatter(x, y) |
| Box Plot | plt.boxplot(data) |
plt.title("My Chart Title")
plt.xlabel("X Axis Label")
plt.ylabel("Y Axis Label")
plt.legend(["Series A", "Series B"])
plt.grid(alpha=0.3)
plt.show()| Parameter | Purpose |
|---|---|
| color='orange' | Line/bar color |
| marker='o' | Point marker style |
| linewidth=2 | Line thickness |
| linestyle='--' | Dashed line style |
| figsize=(8,5) | Figure size (in plt.subplots()) |
| rotation=45 | Rotate axis tick labels (plt.xticks) |
fig, ax = plt.subplots(figsize=(8,5))
ax.plot(x, y)
ax.set_title("Chart Title")
# Multiple subplots in a grid
fig, axes = plt.subplots(1, 2, figsize=(10,4))
axes[0].plot(x, y)
axes[1].bar(categories, values)
# Save to file
plt.savefig("chart.png", dpi=150, bbox_inches="tight")