Skip to content

Commit a8ba894

Browse files
authored
Merge pull request animator#1325 from Salma-Mamdoh/main
Subplots in Matplotlib
2 parents 6bc3784 + 07b13e0 commit a8ba894

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
28.3 KB
Loading

contrib/plotting-visualization/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Line Charts in Matplotlib](matplotlib-line-plots.md)
88
- [Scatter Plots in Matplotlib](matplotlib-scatter-plot.md)
99
- [Violin Plots in Matplotlib](matplotlib-violin-plots.md)
10+
- [subplots in Matplotlib](matplotlib-sub-plot.md)
1011
- [Introduction to Seaborn and Installation](seaborn-intro.md)
1112
- [Seaborn Plotting Functions](seaborn-plotting.md)
1213
- [Getting started with Seaborn](seaborn-basics.md)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
![subplot Chart](images/subplots.png)
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+
![subplot Chart](images/subplots.png)
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+
![subplot Chart](images/subplots.png)
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

Comments
 (0)