Bar Chart

Bar Chart


  Data visualization

Contents

Bar Chart

A bar chart represents data values in the form of vertical bars. Each vertical bar in the graph represents an individual category. The bars are used to compare values in different categories. In a bar chart, the length of a bar is proportional to the value it represents and the width remains same for all bars. One axis of the chart represents categories and the other axis represents the value scale.

Below we see a bar chart plotted using the bar function defined in pyplot module. The bar chart displays cars sales during a ten year period for an automobile company. The first argument to the bar function indicates the position of the bar on the x-axis with the center at the x-tick position. The second argument indicates the height.

from matplotlib import pyplot as plt
import numpy as np
year = [2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
toyota_sales = [1843669,1496211,1488588,1396837,1764802,1893874,2004373,2098545,2106332,2129177,2128201]
x_pos = np.arange(len(year))
plt.bar(x_pos,toyota_sales,color='#623aa2',alpha=0.25,edgecolor='k',label='Toyota')
plt.xticks(x_pos,year,rotation=30)
plt.title('Toyota Car sales')
plt.xlabel('Year')
plt.ylabel('No of units sold')
plt.show()

Clustered Bar Chart

A clustered or a grouped bar chart is used to compare multiple data sets side by side. Say, you want to compare values of multiple datasets that come under the same category, then a clustered bar chart comes in handy. The previous example can be extended to display car sales of different automobile companies.

In the previous example, we used a bar chart to display the sales of an automobile company for a ten year period. Now we would like to compare the sales of three different companies for the same period. So we are going to have three vertical bars under each category, each bar representing a company. In order to differentiate the three datasets we use different colors for the bars.

year = [2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
hyundai_sales = [401742,435064,538228,645691,703007,720783,725718,761710,768057,664943,667634]
honda_sales = [1284261,1045061,1096874,1023986,1266569,1359876,1373029,1409386,1476582,1486827,1445894]
toyota_sales = [1843669,1496211,1488588,1396837,1764802,1893874,2004373,2098545,2106332,2129177,2128201]
x_pos = np.arange(len(year))
width = 0.25
plt.bar(x_pos-width, toyota_sales, edgecolor='k',color='#E59998',width=width,label='Toyota')
plt.bar(x_pos, honda_sales, edgecolor='k',color='#FAD8AA',width=width,label='Honda',alpha=0.75)
plt.bar(x_pos+width, hyundai_sales, edgecolor='k',color='#538790',width=width,label='Hyundai')
plt.xticks(x_pos,year,rotation=30)
plt.title('Car sales')
plt.xlabel('Year')
plt.ylabel('No of units sold')
plt.legend()
plt.show()

Horizontal Bar Chart

Horizontal Bar Charts represent data in the form of horizontal bars, each bar representing an individual category. The data categories are shown on the y-axis and the data values are shown on the x-axis. The length of a bar is proportional to the value it represents.

The example below demonstrates how to plot a bar chart, the input datasets required for plotting are available in a csv file. We will import the built-in csv module to work with csv files.

import csv
with open (r'C:\Users\Ajay Tech\Desktop\air_pollution_index.csv') as input_file:
    csv_file = csv.reader(input_file,delimiter = ',')
    Header = next(csv_file)
    country = []
    index = []
    for row in csv_file:
        country.append(row[0])
        index.append(int(row[1]))

plt.bar(country,index,color='#ff753e') 
plt.title('Air Pollution Index')
plt.xlabel('Country')
plt.ylabel('Pollution Index')
plt.show()

The above graph is plotted using the bar function. As can be observed from the figure the x-axis labels are overlapping with each other because the labels are too long. This problem can be solved using a horizontal bar chart, which makes optimal use of the space available. If the data labels are long or if you have too many data sets to plot, then horizontal bar charts can be used for plotting.

plt.barh(country,index,color='#ff753e') 
plt.title('Air Pollution Index')
plt.xlabel('Pollution Index')
plt.ylabel('Country')
plt.show()