|  | 
|  | 1 | +### 1. Using `plt.subplots()` | 
|  | 2 | + | 
|  | 3 | +The `plt.subplots()` function is a versatile and easy way to create a grid of subplots. It returns a figure and an array of Axes objects. | 
|  | 4 | + | 
|  | 5 | +#### Code Explanation | 
|  | 6 | + | 
|  | 7 | +1. **Import Libraries**: | 
|  | 8 | + ```python | 
|  | 9 | + import matplotlib.pyplot as plt | 
|  | 10 | + import numpy as np | 
|  | 11 | + ``` | 
|  | 12 | + | 
|  | 13 | +2. **Generate Sample Data**: | 
|  | 14 | + ```python | 
|  | 15 | + x = np.linspace(0, 10, 100) | 
|  | 16 | + y1 = np.sin(x) | 
|  | 17 | + y2 = np.cos(x) | 
|  | 18 | + y3 = np.tan(x) | 
|  | 19 | + ``` | 
|  | 20 | + | 
|  | 21 | +3. **Create Subplots**: | 
|  | 22 | + ```python | 
|  | 23 | + fig, axs = plt.subplots(3, 1, figsize=(8, 12)) | 
|  | 24 | + ``` | 
|  | 25 | + | 
|  | 26 | + - `3, 1` indicates a 3-row, 1-column grid. | 
|  | 27 | + - `figsize` specifies the overall size of the figure. | 
|  | 28 | + | 
|  | 29 | +4. **Plot Data**: | 
|  | 30 | + ```python | 
|  | 31 | + axs[0].plot(x, y1, 'r') | 
|  | 32 | + axs[0].set_title('Sine Function') | 
|  | 33 | +  | 
|  | 34 | + axs[1].plot(x, y2, 'g') | 
|  | 35 | + axs[1].set_title('Cosine Function') | 
|  | 36 | +  | 
|  | 37 | + axs[2].plot(x, y3, 'b') | 
|  | 38 | + axs[2].set_title('Tangent Function') | 
|  | 39 | + ``` | 
|  | 40 | + | 
|  | 41 | +5. **Adjust Layout and Show Plot**: | 
|  | 42 | + ```python | 
|  | 43 | + plt.tight_layout() | 
|  | 44 | + plt.show() | 
|  | 45 | + ``` | 
|  | 46 | + | 
|  | 47 | +#### Result | 
|  | 48 | + | 
|  | 49 | +The result will be a figure with three vertically stacked subplots. | 
|  | 50 | + | 
|  | 51 | + | 
|  | 52 | +### 2. Using `plt.subplot()` | 
|  | 53 | + | 
|  | 54 | +The `plt.subplot()` function allows you to add a single subplot at a time to a figure. | 
|  | 55 | + | 
|  | 56 | +#### Code Explanation | 
|  | 57 | + | 
|  | 58 | +1. **Import Libraries and Generate Data** (same as above). | 
|  | 59 | + | 
|  | 60 | +2. **Create Figure and Subplots**: | 
|  | 61 | + ```python | 
|  | 62 | + plt.figure(figsize=(8, 12)) | 
|  | 63 | +  | 
|  | 64 | + plt.subplot(3, 1, 1) | 
|  | 65 | + plt.plot(x, y1, 'r') | 
|  | 66 | + plt.title('Sine Function') | 
|  | 67 | +  | 
|  | 68 | + plt.subplot(3, 1, 2) | 
|  | 69 | + plt.plot(x, y2, 'g') | 
|  | 70 | + plt.title('Cosine Function') | 
|  | 71 | +  | 
|  | 72 | + plt.subplot(3, 1, 3) | 
|  | 73 | + plt.plot(x, y3, 'b') | 
|  | 74 | + plt.title('Tangent Function') | 
|  | 75 | + ``` | 
|  | 76 | + | 
|  | 77 | +3. **Adjust Layout and Show Plot** (same as above). | 
|  | 78 | + | 
|  | 79 | +#### Result | 
|  | 80 | + | 
|  | 81 | +The result will be similar to the first method but created using individual subplot commands. | 
|  | 82 | + | 
|  | 83 | + | 
|  | 84 | + | 
|  | 85 | +### 3. Using `GridSpec` | 
|  | 86 | + | 
|  | 87 | +`GridSpec` allows for more complex subplot layouts. | 
|  | 88 | + | 
|  | 89 | +#### Code Explanation | 
|  | 90 | + | 
|  | 91 | +1. **Import Libraries and Generate Data** (same as above). | 
|  | 92 | + | 
|  | 93 | +2. **Create Figure and GridSpec**: | 
|  | 94 | + ```python | 
|  | 95 | + from matplotlib.gridspec import GridSpec | 
|  | 96 | +  | 
|  | 97 | + fig = plt.figure(figsize=(8, 12)) | 
|  | 98 | + gs = GridSpec(3, 1, figure=fig) | 
|  | 99 | + ``` | 
|  | 100 | + | 
|  | 101 | +3. **Create Subplots**: | 
|  | 102 | + ```python | 
|  | 103 | + ax1 = fig.add_subplot(gs[0, 0]) | 
|  | 104 | + ax1.plot(x, y1, 'r') | 
|  | 105 | + ax1.set_title('Sine Function') | 
|  | 106 | +  | 
|  | 107 | + ax2 = fig.add_subplot(gs[1, 0]) | 
|  | 108 | + ax2.plot(x, y2, 'g') | 
|  | 109 | + ax2.set_title('Cosine Function') | 
|  | 110 | +  | 
|  | 111 | + ax3 = fig.add_subplot(gs[2, 0]) | 
|  | 112 | + ax3.plot(x, y3, 'b') | 
|  | 113 | + ax3.set_title('Tangent Function') | 
|  | 114 | + ``` | 
|  | 115 | + | 
|  | 116 | +4. **Adjust Layout and Show Plot** (same as above). | 
|  | 117 | + | 
|  | 118 | +#### Result | 
|  | 119 | + | 
|  | 120 | +The result will again be three subplots in a vertical stack, created using the flexible `GridSpec`.  | 
|  | 121 | + | 
|  | 122 | + | 
|  | 123 | + | 
|  | 124 | +### Summary | 
|  | 125 | + | 
|  | 126 | +- **`plt.subplots()`**: Creates a grid of subplots with shared axes. | 
|  | 127 | +- **`plt.subplot()`**: Adds individual subplots in a figure. | 
|  | 128 | +- **`GridSpec`**: Allows for complex and custom subplot layouts. | 
|  | 129 | + | 
|  | 130 | +By mastering these techniques, you can create detailed and organized visualizations, enhancing the clarity and comprehension of your data presentations. | 
0 commit comments