Skip to content

Commit 3f065dc

Browse files
authored
Merge pull request animator#657 from piyush-poddar/introduction-to-bar-plots-in-matplotlib
Added Content: Bar Plots in Matplotlib
2 parents 077e569 + 7bb7b05 commit 3f065dc

File tree

8 files changed

+218
-1
lines changed

8 files changed

+218
-1
lines changed
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)
21.6 KB
Loading
24.2 KB
Loading
11.6 KB
Loading
25.1 KB
Loading
25.1 KB
Loading
17.7 KB
Loading
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# List of sections
22

3-
- [Installing Matplotlib](matplotlib_installation.md)
3+
- [Installing Matplotlib](matplotlib-installation.md)
4+
- [Bar Plots in Matplotlib](matplotlib-bar-plots.md)

0 commit comments

Comments
 (0)