 # Plotting Bar Graphs In Matplotlib

A bar chart is a type of a chart that can visualize categorical data with rectangular bars and can be easily plotted on a vertical or horizontal axis.

Related Post: Plotting Line Graphs In Python Using Matplotlib

### Example 1 Of Matplotlib Bar Graph

``````import matplotlib.pyplot as plt

x  = ["English","Science","History","Arts","Mathematics"]
y = [65,88,75,82,92]

plt.bar(x,y,alpha = 0.8,align = 'center')
plt.ylabel("Marks")
plt.xlabel("Subject")
plt.title("Marks in each subject")
plt.show()
``````

Our Bar graph will looks like this:

#NOTES :

If you want to plot a horizontal bar graph instead of vertical you can try plt.barh function. Arguments that plt.bar takes are below

• x : sequence of scalars
• height : scalar or sequence of scalars
• width: scalar or array-like, optional
• align : {‘center’, ‘edge’}, optional, default: ‘center’
• yerr : to place error bars ,optional,give it a value error
• One can also plot multiple bars for same variable as given below.
• color: scalar or array-like, optional, The colors of the bar faces.
• tick_label: string or array-like, optional, The tick labels of the bars,Default: None (Use default numeric labels.)

Let’s look at another example of Plotting Bar Graphs In Matplotlib

### Example 2 Of Matplotlib Bar Graph

``````import numpy as np
import matplotlib.pyplot as plt

# data to plot
n_groups = 3
males = [65,25,10]
females = [58,35,7]

# create plot
ax = plt.subplot(111)
x = np.arange(n_groups)
w = 0.35
opacity = 0.8

ax.bar(x, males, w,alpha=opacity,color='b',label='Males')

ax.bar(x + w, females, w,alpha=opacity,color='g',label='Females')

plt.xlabel('Taster Types')
plt.ylabel('Ratio')
plt.title('Taster type distribution in males and females')
plt.xticks(x + w, ('Tasters',"Super-Tasters","Non-tasters"))
plt.legend()
plt.tight_layout()
plt.show()
``````

Have a look at our new bar graph

Official Documentation Of Matplotlib : Check Here

##### You May Also Like  ## Data Types In Python

Spread the loveIn python, there are various data types, but on the basis of their complexity they can be divided into two types :There are two types of data types… ## Variables In Python

Spread the loveVariables are used to store values in the memory of a computer. a variable name is a name that given to the memory block of the computer to… ## If-else In Python

Spread the loveIf-else in python used to check conditions. These statements are also used to make decisions. before we move further have a quick look at comparison operators, which we… ## Operators In Python 