The bar chart in matplotlib will display data with parallel rectangular bars of equal width along axis. It is used to make comparison between different groups. Barplot or bar chart is the common type of plot. It shows relationship between the numerical and categorical variable. You can display height of several individuals using bar chart. They are confounded with histograms which are different. The mistake is to use the barplots to represent average value of each group. The bar graph present data with rectangular bar with height or length which is proportional to value that they represent. They can be plotted vertically or horizontally. Bar graphs are good if you want to present data of different group compared with each other.
Firstly to create any bar chart/graph as single or multiple we need to import libraries to implement our task.
Suppose you want to show the comparison between cities in term of annual income.
#name of graph
Plt.title(“simple bar graph”)
#Assign name of x axis
plt.xlabel(“students”)
##Assign name of y axis
Plt.ylabel(“math score”)
#change bar color
plt.show()
You can make style in your graph using the following functions –
Plt.title():-used for specifying title of your plot.
Plt.xlabel():-used for labeling x axis.
Plt.ylabel():-used for labeling y axis.
Color:-option in plt.bar () for defining color of bars.
To get the gridline you add the function call grid () with the color, line style, width and axis. You can also add alpha value.
Import numpy as np
Import pandas as pd
From pandas import series, Dataframe
Import matplotlib.plot as plt
Data= [23, 45, 56, 78,213]
Plt.bar (range (len(data)),data, color=’royalblue’,alpha=0.7)
Plt.grid(color=’#95a5a6’,linestyle=’--’,linewidth=2,axis=’y’,alpha=0.7)
Plt.show()
You can stack bar charts on top of each other graphs. That is useful when you multiple values combine into greater.
Example:-
Import numpy as np
Import pandas as pd
From pandas import series, Dataframe
Import matplotlib.pyplot as plt
Data1= [23, 85, 72, 43, 52]
Data2= [42, 35, 21, 16, 9]
Plt.bar (range (len(data1)),data1)
Plt.bar(range(len(data2)),data2,bottom=data1)
Plt.show()
The bottom parameter of the pyplot.bar () function will you to specify a starting value for a bar.
Instead of running from zero to a value, it will go from the bottom to value.
The firstly pyplot.bar () will plots the blue bars.
Then pyplot.bar () will plots the red bars, with the bottom of the red bars is at the top of the blue bars.