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]

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:

Plotting Bar Graphs In Matplotlib

#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

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

Operators In Python

Spread the love

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

If-else In Python

Spread the love

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

Loops In Python

Spread the love

Spread the love There are 2 type of loops in python. for loop while loop Let’s have detailed information about loops in python. For loops in Python for loops in…
Spread the love
View Post

Python Lists

Spread the love

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