|
1 | 1 | --- |
2 | | -seoTitle: Matplotlib Python Library – Data Visualization Reference Guide |
| 2 | +seoTitle: Matplotlib Python Library - Data Visualization Reference Guide |
3 | 3 | description: "Matplotlib creates static, animated, and interactive visualizations in Python. Covers pyplot, axes, subplots, plot types, customization, and saving figures." |
4 | 4 | keywords: "Matplotlib, Python visualization, pyplot, subplots, plot types, data visualization, charts, graphs, figure customization, Python library, scientific plotting" |
5 | 5 | --- |
6 | 6 |
|
7 | | -- [Docs](https://matplotlib.org/stable/contents.html) |
| 7 | +- # 📘 **Overview** |
| 8 | + - **What is it?**: Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is the core graphics engine behind many other plotting libraries (such as Seaborn and Pandas plotting). |
| 9 | + - **Key Features**: |
| 10 | + - **Highly Customizable**: Total control over fonts, axes, line styles, colors, and layout configurations. |
| 11 | + - **Two Interfaces**: State-based interface (via `pyplot` - good for quick plots) and Object-Oriented interface (recommended for clean, robust layouts). |
| 12 | + - **Export Options**: Export figures to multiple high-quality formats (PNG, PDF, SVG, etc.). |
| 13 | + - **Installation**: |
| 14 | + - ```bash |
| 15 | + pip install matplotlib |
| 16 | + ``` |
| 17 | +- # 🧾 **Core Concepts** |
| 18 | + - **Figure**: The overall window or page that contains all plot elements (axes, title, legend, etc.). Think of it as a canvas. |
| 19 | + - **Axes**: A coordinate area within a Figure where data is plotted. A single Figure can contain multiple Axes (subplots). |
| 20 | + - **Axis**: The actual helper lines and ticks marking scale values (e.g. X-axis, Y-axis). |
| 21 | +- # 💻 **Common Code Patterns & Cheat Sheet** |
| 22 | + - **Basic Line Plot (Object-Oriented Style)**: |
| 23 | + - ```python |
| 24 | + import matplotlib.pyplot as plt |
| 25 | + import numpy as np |
| 26 | + |
| 27 | + # Generate data |
| 28 | + x = np.linspace(0, 10, 100) |
| 29 | + y = np.sin(x) |
| 30 | + |
| 31 | + # Create Figure and Axes |
| 32 | + fig, ax = plt.subplots(figsize=(8, 4)) |
| 33 | + |
| 34 | + # Plot data |
| 35 | + ax.plot(x, y, label="Sine Wave", color="purple", linewidth=2, linestyle="-") |
| 36 | + |
| 37 | + # Customize labels & grid |
| 38 | + ax.set_title("Simple Sine Plot", fontsize=14) |
| 39 | + ax.set_xlabel("X-Axis") |
| 40 | + ax.set_ylabel("Y-Axis") |
| 41 | + ax.legend(loc="upper right") |
| 42 | + ax.grid(True, linestyle="--", alpha=0.6) |
| 43 | + |
| 44 | + # Render / Show |
| 45 | + plt.tight_layout() |
| 46 | + plt.show() |
| 47 | + ``` |
| 48 | + - **Creating Subplots (Multi-plot Layouts)**: |
| 49 | + - ```python |
| 50 | + # Create 1 row with 2 columns of plots |
| 51 | + fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) |
| 52 | + |
| 53 | + # Left plot: Scatter |
| 54 | + ax1.scatter(np.random.rand(50), np.random.rand(50), color="blue", alpha=0.5) |
| 55 | + ax1.set_title("Scatter Plot") |
| 56 | + |
| 57 | + # Right plot: Histogram |
| 58 | + ax2.hist(np.random.randn(1000), bins=30, color="orange", edgecolor="black") |
| 59 | + ax2.set_title("Histogram") |
| 60 | + |
| 61 | + plt.tight_layout() |
| 62 | + plt.show() |
| 63 | + ``` |
| 64 | + - **Saving Plots**: |
| 65 | + - ```python |
| 66 | + fig, ax = plt.subplots() |
| 67 | + ax.plot([1, 2, 3], [4, 5, 6]) |
| 68 | + |
| 69 | + # Save as PNG with transparent background and high resolution |
| 70 | + fig.savefig("my_plot.png", dpi=300, bbox_inches="tight", transparent=True) |
| 71 | + ``` |
| 72 | +- # 💡 **Best Practices & Tips** |
| 73 | + - **Object-Oriented API**: Always prefer using `fig, ax = plt.subplots()` rather than calling `plt.plot()` directly. The OO API keeps your code cleaner when working with subplots or complex layouts. |
| 74 | + - **Tight Layout**: Always call `plt.tight_layout()` before saving or showing a plot to prevent axes labels and titles from overlapping. |
| 75 | + - **Closing Figures**: If you are generating many plots in a loop (e.g., in a background job or web server), always call `plt.close(fig)` or `plt.close('all')` to prevent memory leaks. |
| 76 | +- # 🔗 **Navigation & Internal Links** |
| 77 | + - **Parent**: [[Python]] |
| 78 | + - **Related Notes**: [[Data Science]] | [[Machine Learning]] | [[NumPy]] | [[Pandas]] | [[Seaborn]] | [[Plotly]] |
0 commit comments