Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python Bar Plot – Visualize Categorical Data in Python

Python BARPLOT

Hey, readers. In this article, we will be focusing on creating a Python bar plot.

Data visualization enables us to understand the data and helps us analyze the distribution of data in a pictorial manner.

BarPlot enables us to visualize the distribution of categorical data variables. They represent the distribution of discrete values. Thus, it represents the comparison of categorical values.

The x axis represents the discrete values while the y axis represents the numeric values of comparison and vice versa.

Let us now focus on the construction of Bar plots in the upcoming section.


Creating a Python Bar Plot Using Matplotlib

Python matplotlib module provides us with various functions to plot the data and understand the distribution of the data values.

The matplotlib.pyplot.bar() function is used to create a Bar plot using matplotlib module.

Syntax:

matplotlib.pyplot.bar(x, height, width, bottom, align)
  • x: The scalar x-coordinates of the barplot
  • height: The height of the bars to be plotted
  • bottom: The vertical baseline
  • width: The width of the bars to be plotted(optional)
  • align: The type of alignment of the bar plot(optional).

Further, we need to make sure and understand that only categorical data values can be provided to the barplot.

Let us now try to implement a barplot using the matplotlib module.

Example:

import matplotlib.pyplot as plt
country = ['INDIA', 'JAPAN', 'CHINA', 'USA', 'GERMANY']
population = [1000,800,600,400,1100]
plt.bar(country,population)
plt.show()

Output:

BARPLOT Using Matplotlib
BARPLOT Using Matplotlib

Bar Plot using Seaborn module

Python Seaborn module is built over the Matplotlib module and offers us with some advanced functionalities to have a better visualization of the data values.

Syntax:

seaborn.barplot(x,y)

Example:

import seaborn as sn
import matplotlib.pyplot as plt
import pandas as pd
BIKE = pd.read_csv("BIKE.csv")
sn.barplot(x="season",y="cnt",data=BIKE)
plt.show()

Output:

BARPLOT Using Seaborn
BARPLOT Using Seaborn

Conclusion

Thus, in this article, we have understood the various techniques to construct a Python bar plot .

Feel free to comment below, in case you come across any question.

For more such posts related to Python, Stay tuned and till then, Happy Learning!!


References