Ultimate Guide to Python's Matplotlib: A Library Used to Plot Charts
Ultimate Guide to Python's Matplotlib: A Library Used to Plot Charts

Photo by Cookie the Pom on Unsplash

Data visualization refers to the graphical or visual representation of data and information using elements like charts, graphs and maps, etc. Over the years, data visualization has gained immense popularity as it provides an easy interpretation of even massive amounts of data by displaying it in the form of patterns, trends and so on. By following these patterns and trends a user can facilitate his decision making.

Python uses the Matplotlib library's pyplot for data visualization. Pyplot is a collection of methods within the Matplotlib library, which can be used to create 2D charts, graphs and represent data interactively and effectively. The Matplotlib library is preinstalled with Anaconda distribution or can also be installed easily from the internet.

Installing Matplotlib

1. If you have Anaconda navigator, open the navigator window, click environments and scroll down to find Matplotlib library. It is preinstalled on your computer.

2. If you don't have Anaconda navigator, that isn't a problem. Just go to https://pypi.org/project/matplotlib/#files

Here you will find the library. Download and install it, and you are ready to create wonderful charts and graphs in python itself.

Types of charts offered by Matplotlib

It offers a wide range of charts of which the most prominently used ones are listed below:

1. Line Chart

It connects important points called 'markers' through the use of straight-line segments. These points will represent the data that you will enter while making the chart.

2. Bar Chart

It uses bars to represent the data. The height of the bars is variegated to depict the differences in the given data. Bar charts can be plotted horizontally as well as vertically depending upon the need of the user.

3. Pie Chart

Slices of a circular area are used to depict the data. The slice with larger area represents a higher value, whereas a smaller one is represented by less area.

4. Scatter plot

Scatter chart just plots the data in the form of dots. It differs from the line chart by not joining the dots using straight lines.

Now, let's move on to the steps to create these charts.

Note: You will have to give the command to import Matplotlib before you set out to create charts. For this just type the below-mentioned command in your Jupyter or python window:

import matplotlib.pyplot as pl

This will import Matplotlib to your window and you will have to just use ‘pl' in place of the long ‘matplotlib.pyplot' every time you create your charts.

Line Charts

To create a line chart you must assign some data beforehand. This data can be given in the form of lists, or dictionaries in python. Here I will use lists to create charts:

import matplotlib.pyplot as pl a= [ 1, 2, 3, 4] b= [2, 4, 6, 8] pl.plot(a,b) pl.show()

Here a and b were lists that were created consisting of values 1,2,3,4 and 2,4,6,8 respectively. The command pl.plot(a,b) was given to plot a line chart using values in ‘a' as the x-axis and ‘b' as the y axis.

Here is the plotted chart:

Image source: Author

You can also give names to the x and y-axis as follows:

pl.xlabel(“ values in a”) pl.ylabel(“values in b") pl.plot(a,b) pl.show()

Here the x-axis will be named as ‘values in a' and y-axis as ‘values in b'.

Bar Charts

Let’s move on to drawing bar charts. Bar charts also require the same steps as the line chart. The only difference arrives while giving the command to plot the data.

import matplotlib.pyplot as pl a= [ 1, 2, 3, 4] b= [2, 4, 6, 8] pl.bar(a,b) pl.show()

While giving the command to plot a bar chart we need to specify ‘bar’ for the same, as we have done above.

Image source: Author

To name the x and y-axis the same procedure can be followed.

pl.xlabel(“ values in a”) pl.ylabel(“values in b") pl.bar(a,b) pl.show()

The width of the bars can also be altered using the ‘width’ command. The value given in the width command should be numeric, otherwise, Python will raise an error.

pl.bar(a,b, width=<value>)

Scatter Charts

Scatter charts allow you to change the way it’s data points or markers look by specifying the marker size and marker type. In this type of chart, it is compulsory for you to specify any of the two or both while giving the command to create the same.

a= [ 1, 2, 3, 4] b= [2, 4, 6, 8] pl.plot(a,b, “o", markersize=10) pl.show()

Here the data points would like ‘o' letter and would have a marker size equal to 10. If we don’t specify them then instead of the scatter chart, a line chart would be plotted.

Image source: Author

Changing the x label and y label would remain the same for the scatter charts as well.

pl.xlabel(“ values in a”) pl.ylabel(“values in b") pl.plot(a,b, “o", markersize=10) pl.show()

Pie charts

Contrary to other charts, a pie chart can also function with just one of the list. But for clarity in the understanding of the readers as well as users we specify the labels for each slice, which requires the use of the second list.

a= [‘Sam’, ‘Tina’, ‘Joe’, ‘Mark’] b= [100, 200, 300, 400] pl.pie(b, labels=a) pl.show()

The list represents the contributions made by 4 members to organize a party. The different members will be depicted by different colour as follows:

Image source: Author

You can also give a title to your pie chart:
['Niyati Jain']
['Programming', 'Computer Science', 'Design', 'Digital Life', 'Technology']