Skip to content

Commit 7b20936

Browse files
committed
Subplots in Matplotlib
1 parent 0ab0668 commit 7b20936

File tree

4 files changed

+348
-1
lines changed

4 files changed

+348
-1
lines changed
Loading
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# Bar Plots in Matplotlib
2+
A bar plot or a bar chart is a type of data visualisation that represents data in the form of rectangular bars, with lengths or heights proportional to the values and data which they represent. The bar plots can be plotted both vertically and horizontally.
3+
4+
It is one of the most widely used type of data visualisation as it is easy to interpret and is pleasing to the eyes.
5+
6+
Matplotlib provides a very easy and intuitive method to create highly customized bar plots.
7+
8+
## Prerequisites
9+
10+
Before creating bar plots in matplotlib you must ensure that you have Python as well as Matplotlib installed on your system.
11+
12+
## Creating a simple Bar Plot with `bar()` method
13+
14+
A very basic Bar Plot can be created with `bar()` method in `matplotlib.pyplot`
15+
16+
```Python
17+
import matplotlib.pyplot as plt
18+
19+
# Creating dataset
20+
x = ["A", "B", "C", "D"]
21+
y = [2, 7, 9, 11]
22+
23+
# Creating bar plot
24+
plt.bar(x,y)
25+
plt.show() # Shows the plot
26+
```
27+
When executed, this would show the following bar plot:
28+
29+
![Basic Bar Plot](images/basic_bar_plot.png)
30+
31+
The `bar()` function takes arguments that describes the layout of the bars.
32+
33+
Here, `plt.bar(x,y)` is used to specify that the bar chart is to be plotted by taking the `x` array as X-axis and `y` array as Y-axis. You can customize the graph further like adding labels to the axes, color of the bars, etc. These will be explored in the upcoming sections.
34+
35+
Additionally, you can also use `numpy` arrays for faster generation when handling large datasets.
36+
37+
```Python
38+
import matplotlib.pyplot as plt
39+
import numpy as np
40+
41+
# Using numpy array
42+
x = np.array(["A", "B", "C", "D"])
43+
y = np.array([2, 7, 9, 11])
44+
45+
plt.bar(x,y)
46+
plt.show()
47+
```
48+
Its output would be the same as above.
49+
50+
## Customizing Bar Plots
51+
52+
For creating customized bar plots, it is **highly recommended** to create the plots using `matplotlib.pyplot.subplots()`, otherwise it is difficult to apply the customizations in the newer versions of Matplotlib.
53+
54+
### Adding title to the graph and labeling the axes
55+
56+
Let us create an imaginary graph of number of cars sold in a various years.
57+
58+
```Python
59+
import matplotlib.pyplot as plt
60+
61+
fig, ax = plt.subplots()
62+
63+
years = ['1999', '2000', '2001', '2002']
64+
num_of_cars_sold = [300, 500, 700, 1000]
65+
66+
# Creating bar plot
67+
ax.bar(years, num_of_cars_sold)
68+
69+
# Adding axis labels
70+
ax.set_xlabel("Years")
71+
ax.set_ylabel("Number of cars sold")
72+
73+
# Adding plot title
74+
ax.set_title("Number of cars sold in various years")
75+
76+
plt.show()
77+
```
78+
79+
![Title and axis labels](images/title_and_axis_labels.png)
80+
81+
Here, we have created a `matplotlib.pyplot.subplots()` object which returns a `Figure` object `fig` as well as an `Axes` object `ax` both of which are used for customizing the bar plot. `ax.set_xlabel`, `ax.set_ylabel` and `ax.set_title` are respectively used for adding labels of X, Y axis and adding title to the graph.
82+
83+
### Adding bar colors and legends
84+
85+
Let us consider our previous example of number of cars sold in various years and suppose that we want to add different colors to the bars from different centuries and respective legends for better interpretation.
86+
87+
This can be achieved by creating two separate arrays `bar_colors` for bar colors and `bar_labels` for legend labels and passing them as arguments to parameters color and label respectively in `ax.bar` method.
88+
89+
```Python
90+
import matplotlib.pyplot as plt
91+
92+
fig, ax = plt.subplots()
93+
94+
years = ['1998', '1999', '2000', '2001', '2002']
95+
num_of_cars_sold = [200, 300, 500, 700, 1000]
96+
bar_colors = ['tab:green', 'tab:green', 'tab:blue', 'tab:blue', 'tab:blue']
97+
bar_labels = ['1900s', '_1900s', '2000s', '_2000s', '_2000s']
98+
99+
# Creating the customized bar plot
100+
ax.bar(years, num_of_cars_sold, color=bar_colors, label=bar_labels)
101+
102+
# Adding axis labels
103+
ax.set_xlabel("Years")
104+
ax.set_ylabel("Number of cars sold")
105+
106+
# Adding plot title
107+
ax.set_title("Number of cars sold in various years")
108+
109+
# Adding legend title
110+
ax.legend(title='Centuries')
111+
112+
plt.show()
113+
```
114+
115+
![Bar colors and Legends](images/bar_colors_and_legends.png)
116+
117+
Note that the labels with a preceding underscore won't show up in the legend. Legend titles can be added by simply passing `title` argument in `ax.legend()`, as shown. Also, you can have a different color for all the bars by passing the `HEX` value of that color in the `color` parameter.
118+
119+
### Adding labels to bars
120+
121+
We may want to add labels to bars representing their absolute (or truncated) values for instant and accurate reading. This can be achieved by passing the `BarContainer` object (returned by `ax.bar()` method) which is basically a aontainer with all the bars and optionally errorbars to `ax.bar_label` method.
122+
123+
```Python
124+
import matplotlib.pyplot as plt
125+
126+
fig, ax = plt.subplots()
127+
128+
years = ['1998', '1999', '2000', '2001', '2002']
129+
num_of_cars_sold = [200, 300, 500, 700, 1000]
130+
bar_colors = ['tab:green', 'tab:green', 'tab:blue', 'tab:blue', 'tab:blue']
131+
bar_labels = ['1900s', '_1900s', '2000s', '_2000s', '_2000s']
132+
133+
# BarContainer object
134+
bar_container = ax.bar(years, num_of_cars_sold, color=bar_colors, label=bar_labels)
135+
136+
ax.set_xlabel("Years")
137+
ax.set_ylabel("Number of cars sold")
138+
ax.set_title("Number of cars sold in various years")
139+
ax.legend(title='Centuries')
140+
141+
# Adding bar labels
142+
ax.bar_label(bar_container)
143+
144+
plt.show()
145+
```
146+
147+
![Bar Labels](images/bar_labels.png)
148+
149+
**Note:** There are various other methods of adding bar labels in matplotlib.
150+
151+
## Horizontal Bar Plot
152+
153+
We can create horizontal bar plots by using the `barh()` method in `matplotlib.pyplot`. All the relevant customizations are applicable here also.
154+
155+
```Python
156+
import matplotlib.pyplot as plt
157+
158+
fig, ax = plt.subplots(figsize=(10,5)) # figsize is used to alter the size of figure
159+
160+
years = ['1998', '1999', '2000', '2001', '2002']
161+
num_of_cars_sold = [200, 300, 500, 700, 1000]
162+
bar_colors = ['tab:green', 'tab:green', 'tab:blue', 'tab:blue', 'tab:blue']
163+
bar_labels = ['1900s', '_1900s', '2000s', '_2000s', '_2000s']
164+
165+
# Creating horizontal bar plot
166+
bar_container = ax.barh(years, num_of_cars_sold, color=bar_colors, label=bar_labels)
167+
168+
# Adding axis labels
169+
ax.set_xlabel("Years")
170+
ax.set_ylabel("Number of cars sold")
171+
172+
# Adding Title
173+
ax.set_title("Number of cars sold in various years")
174+
ax.legend(title='Centuries')
175+
176+
# Adding bar labels
177+
ax.bar_label(bar_container)
178+
179+
plt.show()
180+
```
181+
182+
![Horizontal Bar Plot-1](images/horizontal_bar_plot_1.png)
183+
184+
We can also invert the Y-axis labels here to show the top values first.
185+
186+
```Python
187+
import matplotlib.pyplot as plt
188+
189+
fig, ax = plt.subplots(figsize=(10,5)) # figsize is used to alter the size of figure
190+
191+
years = ['1998', '1999', '2000', '2001', '2002']
192+
num_of_cars_sold = [200, 300, 500, 700, 1000]
193+
bar_colors = ['tab:green', 'tab:green', 'tab:blue', 'tab:blue', 'tab:blue']
194+
bar_labels = ['1900s', '_1900s', '2000s', '_2000s', '_2000s']
195+
196+
# Creating horizontal bar plot
197+
bar_container = ax.barh(years, num_of_cars_sold, color=bar_colors, label=bar_labels)
198+
199+
# Adding axis labels
200+
ax.set_xlabel("Years")
201+
ax.set_ylabel("Number of cars sold")
202+
203+
# Adding Title
204+
ax.set_title("Number of cars sold in various years")
205+
ax.legend(title='Centuries')
206+
207+
# Adding bar labels
208+
ax.bar_label(bar_container)
209+
210+
# Inverting Y-axis
211+
ax.invert_yaxis()
212+
213+
plt.show()
214+
```
215+
216+
![Horizontal Bar Plot-2](images/horizontal_bar_plot_2.png)

contrib/plotting-visualization/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
- [Pie Charts in Matplotlib](matplotlib-pie-charts.md)
77
- [Line Charts in Matplotlib](matplotlib-line-plots.md)
88
- [Scatter Plots in Matplotlib](matplotlib-scatter-plot.md)
9+
- [subplots in Matplotlib](matplotlib-sub-plot.md)
910
- [Introduction to Seaborn and Installation](seaborn-intro.md)
1011
- [Seaborn Plotting Functions](seaborn-plotting.md)
1112
- [Getting started with Seaborn](seaborn-basics.md)
1213
- [Bar Plots in Plotly](plotly-bar-plots.md)
13-
- [Pie Charts in Plotly](plotly-pie-charts.md)
14+
- [Pie Charts in Plotly](plotly-pie-charts.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)