Python | Bar Graph

Python | Bar Graph: In this tutorial, we are going to learn about the bar graph and its implementation with examples.
Submitted by Anuj Singh, on July 09, 2020

A bar graph is a type of data visualization technique that is very often used to represent data in the form of a vertical bar. The height of the bar is proportional to the value. They allow us to compare data very clearly and therefore, bar graphs or bar charts are included in most of the presentations worldwide. A bar graph can be used to show comparisons between discrete classes and one of the two axes represents the specific categories that are being compared, and the other axis represents a measured value (for most of the cases, the y-axis represents the value and x-axis consist of discrete classes).

matplotlib allows us to use this particular class of data visualization via an inbuilt function

The following are examples for understanding the implementation of bar graphs.

1) Standard Bar Graph

Syntax:, y)
  • x - x axis distribution
  • y - height of the bar
Python Bar Graph 1

2) Standard Bar Graph with Color Change (Green)

Syntax:, y, color='g')
  • color='g' - green color bars
Python Bar Graph 2

3) Bar Graph with String


xx = np.array(['a','b','c','d','e','f','g','u']), y, color='g')
  • xx - using names instead of numeric values on individual bar
Python Bar Graph 3

4) A Random Bar graph with using opaque ratio

Syntax:, np.random.randint(0,50,26), alpha = 0.6)
  • alpha=0.6 - opaque ratio as 0.6
Python Bar Graph 4

Python code to demonstrate example of bar graph

# Data Visualization using Python
# Bar Graph

import numpy as np
import matplotlib.pyplot as plt

N = 8
x = np.array([1,2,3,4,5,6,7,9])
xx = np.array(['a','b','c','d','e','f','g','u'])
y = np.random.rand(N)*10

# a normal bar plot with default features, y)
plt.title('Normal bar Plot')

# a bar plot with green collour
plt.figure(), y, color='g')
plt.title('Different Colour')

# a bar plot with string points
plt.figure(), y, color='g')
plt.title('Bar Graph with String')

# a combined bar Plot
plt.figure(), np.random.randint(0,50,26), alpha = 0.6)
plt.title('Random Plot')


Output is as figure


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.