Matplotlib for Data Visualization
What is matplotlib?
Matplotlib is one of the most prominent and commonly used Python libraries that offer a lot of data visualisation features. There are numerous libraires in python and matplotlib is one of the most effective plotting packages. Matplotlib library was created by John D. Hunter in 2003. Latest version of matplotlib is 3.5.0, it was launched at 16th November 2021.
How to install matplotlib?
Step 1 − Check to see if Python and pip are already installed on your system.
To see if python and pip are installed on your system, type the following instructions into the command prompt. To check Python
python --version
The version of Python installed on your system will be displayed if python was successfully installed.
To check pip
pip -V
If pip has been successfully installed on your system, the version will be displayed.
Step 2 − Install Matplotlib
Matplotlib can be installed using pip in python. To install Matplotlib, type the following line in the command prompt.
pip install matplotlib
This command will begin the process of downloading and installing matplotlib-related packages. After that, you'll see a notification indicating that the installation was successful.
Step 3 − Check if it is installed successfully
Run the following command in the command prompt to see if matplotlib has been successfully installed on your machine. The version of matplotlib installed will be displayed if it was successfully installed.
import matplotlib
matplotlib.__version__
Importing matplotlib library
Now let’s use the matplotlib library. So for that you have to call it using import and then alias it with a shorter name. Code for that is :
import matplotlib.pyplot as plt
Different types of Chart using Matplotlib
Matplotlib visualizations appear in a variety of shapes and sizes.
Let’s look each of them with code.
1)Line plot:
The basic plot() function from the PyPlot instance is used to plot a line plot in Matplotlib. There's no particular lineplot() function - the generic one automatically plots using lines or markers.
# Importing matplotlib library
import matplotlib.pyplot as plt
# x-axis
x = [1, 2, 3, 4, 5, 6]
# x-axis
y = [1, 11, 17, 4, 8, 9]
# function to plot line in matplotlib
plt.plot(x, y)
# Function to show the plot
plt.show()
Output:
A bar graph is a graphical representation of data in which specific forms, such as rectangles, are used to highlight categories. The data disseminated in the dataset is represented by the length and heights of the bar chart. In a bar chart, one axis represents the values or counts associated with a specific category of a column in the dataset, while the other axis represents the values or counts connected with it. Bar charts can be drawn horizontally or vertically. A column chart is another name for a vertical bar chart. Pareto charts are bar charts that are arranged in a high-to-low-value counts way. There's is particular barplot() function - the generic one automatically bar plots.
# Importing matplotlib library
import matplotlib.pyplot as plt
# x-axis
x = [1, 2, 3, 4, 5, 6]
# x-axis
y = [1, 11, 17, 4, 8, 9]
# function to plot line in matplotlib
plt.bar(x, y)
# Function to show the plot
plt.show()
Output:
3)Scatter plot:
Scatter plots are used to visualise the relationship between variables, with dots representing the association. A scatter plot is created using the matplotlib library's scatter() method. Scatter plots are commonly used to depict the relationship between variables and how one impacts the other.
# Importing matplotlib library
import matplotlib.pyplot as plt
# x-axis
x = [1, 2, 3, 4, 5, 6]
# x-axis
y = [1, 11, 17, 4, 8, 9]
# function to plot
plt.scatter(x, y)
# Function to show the plot
plt.show()
Output
4) Pie chart:
A Pie Chart is a circular statistical layout that can only show one set of data at a time. The overall percentage of the given data is represented by the chart's area. The percentage of parts of the data is represented by the area of the pie slices. Pie wedges are the slices of the pie. The length of the wedge's arc determines the area of the wedge. The size of a wedge represents the percentage of that section of the data that is relative to the entire. Because they provide a brief summary, pie charts are often used in business presentations such as sales, operations, survey findings, and resources.
# Import libraries
from matplotlib import pyplot as plt
# Creating dataset
Subjects = ['Chemistry ', 'Computers ', 'English',
'Mathematics ']
Overall_Result = [19,21,20,40]
# Creating plot
fig = plt.figure(figsize =(10, 6))
plt.pie(Overall_Result, labels = Subjects)
# show plot
plt.show()
Output:
5)Histogram
A histogram is a visual representation of data presented in the form of groupings. It is a precise approach for displaying numerical data distribution graphically. It's a type of bar plot in which the X-axis shows bin ranges and the Y-axis represents frequency. To make a histogram, first establish a bin for the ranges, then divide the entire range of values into a series of intervals and count the values that fall into each interval. Bins are defined as non-overlapping, successive intervals of variables. To compute and create a histogram of x, use the matplotlib.pyplot.hist() function.
# Importing matplotlib library
import matplotlib.pyplot as plt
# x-axis
x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
num_bins = 5
# function to plot
plt.hist(x)
# Function to show the plot
plt.show()
Output:
- Avani Popat
- Mar, 10 2022