plotting bar graphs in matplotlib

Plotting Bar Graphs In Matplotlib

Spread the love

In this article, we will learn, 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.

Let’s start with plotting bar graph using python and matplotlib.

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],y,alpha = 0.8,align = 'center')
plt.title("Marks in each subject")

Our Bar graph will looks like this:

Plotting Bar Graphs In Matplotlib


If you want to plot a horizontal bar graph instead of vertical you can try plt.barh function. Arguments that 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, males, w,alpha=opacity,color='b',label='Males') + w, females, w,alpha=opacity,color='g',label='Females')

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

Have a look at our new bar graph

Plotting Bar Graphs In Matplotlib

Official Documentation Of Matplotlib : Check Here

Spread the love
1 comment
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Data Types In Python

Spread the love

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…
Spread the love
View Post

If-else In Python

Spread the love

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…
Spread the love
View Post

Operators In Python

Spread the love

Spread the loveOperators in python is a symbol that perform certain operation on one or more variable or a value. There are 7 types of operators in python Arithmetic operators…
Spread the love
View Post

Python Lists

Spread the love

Spread the loveLists are Non-Primitive data structure in python. Lists are the ordered collection of data. It is changeable. List contains different Primitive Data types like String, Integer, Float and…
Spread the love
View Post