What is matplotlib bar chart? How to use it in Python?
In the era of big data, the ability to visualize information effectively is more crucial than ever. Data visualization not only helps in simplifying the complex data but also plays a key role in data analysis by enabling us to detect patterns, trends, and outliers within the data. Among the various tools available for data visualization in Python, Matplotlib stands out for its versatility and ease of use. It is a powerful library that allows users to create a wide range of static, animated, and interactive visualizations in Python.
Introduction to Matplotlib
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It was created by John D. Hunter and has since become the foundation of many other visualization libraries built on top of it. Matplotlib makes it easy to generate plots, histograms, power spectra, bar charts, error charts, scatterplots, etc., with just a few lines of code. Its importance in the Python data visualization ecosystem cannot be overstated; it is often the first tool that data scientists and analysts reach for when they need to visualize data.
What is a Bar Chart?
A bar chart or bar graph is a chart that presents categorical data with rectangular bars with lengths proportional to the values that they represent. Bar charts are used for comparing the quantities of different categories or groups. One of the key features of bar charts is their versatility. They can be used to represent data in various forms, including stacked, grouped, and horizontal configurations. Bar charts are most effective when you need to compare data across different categories or to track changes over time if the changes are relatively large.
Getting Started with Matplotlib
Before you can create visualizations with Matplotlib, you need to install it. If you’re using pip, you can install Matplotlib by running pip install matplotlib
in your terminal or command prompt. Once installed, you’ll also want to ensure you have NumPy installed as well, as it’s often used alongside Matplotlib for numerical computations. You can install NumPy using pip install numpy
.
Creating Your First Bar Chart
To create a bar chart, you first need to import the necessary libraries. Matplotlib’s pyplot
module provides a MATLAB-like interface for making plots and graphs, which is very convenient for creating a wide range of visualizations quickly.
import matplotlib.pyplot as plt
import numpy as np
Data Preparation
Let’s prepare some data to plot. For a simple example, consider plotting the number of users for different social media platforms.
platforms = ['Facebook', 'Instagram', 'Twitter', 'LinkedIn']
users = [2.7, 1.3, 0.4, 0.7] # in billions
Plotting the Bar Chart
With the data ready, you can now plot the bar chart using plt.bar
.
plt.bar(platforms, users)
plt.xlabel('Social Media Platforms')
plt.ylabel('Users in Billions')
plt.title('Social Media Users Worldwide')
plt.show()
Displaying the Chart
The plt.show()
function mentioned above is what you’ll use to display the bar chart. This command tells Matplotlib to render the plot and display it to the user. It’s the last step in the basic workflow of creating visualizations with Matplotlib.
Customizing Bar Charts
Matplotlib provides numerous options to customize your bar charts, making your visualizations more effective and appealing.
Changing Bar Colors and Borders
You can change the color of the bars by adding the color
parameter to the plt.bar
function. Similarly, to add or customize the border, you can use the edgecolor
parameter.
plt.bar(platforms, users, color='skyblue', edgecolor='black')
Adjusting Bar Width and Alignment
The width of the bars can be adjusted using the width
parameter, and their alignment (center or edge) can be specified with the align
parameter.
plt.bar(platforms, users, color='skyblue', edgecolor='black', width=0.4, align='center')
Adding Labels, Titles, and Legends
To enhance the readability of a bar chart, adding labels and titles is crucial. Labels inform the reader about the kind of data presented, while titles give an overview of the chart’s purpose. Legends are essential when a chart contains multiple datasets. Here’s how to add these elements:
1import matplotlib.pyplot as plt
2
3x = ['A', 'B', 'C', 'D']
4y = [1, 3, 2, 5]
5
6plt.bar(x, y)
7plt.xlabel('Categories') # Label for x-axis
8plt.ylabel('Values') # Label for y-axis
9plt.title('Bar Chart Example') # Chart title
10plt.legend(['Dataset 1']) # Legend
11plt.show()
Using Horizontal Bar Charts
Horizontal bar charts are useful for comparing quantities when the category names are lengthy or when there are a large number of categories. Matplotlib’s barh
function is used to create horizontal bar charts. Here’s an example:
plt.barh(x, y)
plt.xlabel('Values')
plt.ylabel('Categories')
plt.title('Horizontal Bar Chart Example')
plt.show()
Advanced Bar Chart Techniques
Stacked Bar Charts
Stacked bar charts allow you to display the total of different categories along with their breakdown. This is done by plotting bars on top of one another.
y1 = [1, 2, 3, 4]
y2 = [2, 3, 4, 5]
plt.bar(x, y1)
plt.bar(x, y2, bottom=y1)
plt.title('Stacked Bar Chart')
plt.legend(['Dataset 1', 'Dataset 2'])
plt.show()
Grouped Bar Charts
Grouped bar charts are excellent for comparing multiple datasets. You can create them by adjusting the bar positions to be side-by-side.
1import numpy as np
2
3x = np.arange(len(y))
4width = 0.35
5
6fig, ax = plt.subplots()
7bars1 = ax.bar(x - width/2, y1, width, label='Dataset 1')
8bars2 = ax.bar(x + width/2, y2, width, label='Dataset 2')
9
10ax.set_xlabel('Categories')
11ax.set_title('Grouped Bar Chart')
12ax.legend()
13plt.show()
Adding Error Bars
Error bars are graphical representations of data variability and can be added to bar charts to indicate the uncertainty or variability of the data.
error = [0.2, 0.4, 0.5, 0.3]
plt.bar(x, y, yerr=error, capsize=5)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart with Error Bars')
plt.show()
Sharing is caring
Did you like what Vishnupriya wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: