Matplotlib Tutorial 3 bar Graph and Histogram Charts
We all know the bar chart, it helps us to represent categorical data. Each rectangular bar represents data value. With the help of matplotlib in python, we will learn to create vertical and horizontal bars.
Let’s first focus on vertical bar charts. In the later section, we will create horizontal bar charts.
Create Vertical bar graph using matplotlib
Let’s take the sample data from the following table:
| Color | Number of cars |
|---|---|
| Red cars | |
| Blue cars | |
| Silver cars | |
| Black cars | |
| Green cars |
import matplotlib.pyplot as plt
x = ["Red Cars", "Blue Cars", "Silver Cars", "Black Cars", "Green Cars"]
y = [12, 20, 12, 16, 4]
plt.bar(x,y, label="Color")
plt.xlabel("Car Colors" )
plt.ylabel("No. of Cars")
plt.title("Vertical Bar Chart")
plt.legend()
plt.show()
The above code repeats the same code studied in Matplotlib Tutorial 2. The only new function we used is plt.bar(x,y, label=”Color”) which is used to plot a bar graph with x and y as the list for x-axis and y-axis, the label is used to represent the title to be used inside the legend.
Given below is the code shown in Python Online code editor where you can edit the code to experiment with the data and bar chart.
Create Horizontal bar graph using matplotlib
In order to create, horizontal bar chart simply use the function
plt.barh(x,y, label=”Color”)
Create Histogram charts using matplotlib
You must know the difference between bar graphs and histograms. A bar graph helps us to represent the individual discrete values. But sometimes, if data is too large, we need to group the elements so they can be considered as ranges. Suppose, we need to identify range of different ages of people playing video games. In the latter case, plotting a bar graph will not give us an idea about the number of people who play video games between 60 to 70 years of age are either more, less or equal than the number of people that fall in the range of age group 20 to 30.
This is the reason a bar graph contains spaces and a histogram is continuous over a range of data.
Let’s assume the following data.
population_with_ages = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 10, 9, 11, 18, 20, 21, 23, 25, 27, 29, 30, 31, 33, 35, 39, 41, 43, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 65, 70, 72, 75, 77, 82] range_with_ages = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
Examine the below code
import matplotlib.pyplot as plt
population_with_ages = [11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 9, 11, 18, 20, 21, 23, 25, 27, 29, 30, 31, 33, 35, 39, 41, 43, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 65, 70, 72, 75, 77, 82]
range_with_ages = [10, 20, 30, 40, 50, 60, 70, 80, 90]
plt.hist(population_with_ages,range_with_ages, label="Color", histtype="bar",rwidth=0.9)
plt.xlabel("Population with age group 0 to 90" )
plt.ylabel("Frequency of Video Game Players")
plt.title("Video game Players")
plt.legend()
plt.show()
The code line we need to focus on is
plt.hist(population_with_ages,range_with_ages, label=”Color”, histtype=”bar”,rwidth=0.9)
the hist() function will take the arguments, population_with_ages,range_with_ages, label=”Color”. We have used rwidth because rwidth=0.9 will avoid the histogram from being continuous.