Cyberithub

How to Plot Multiple Graphs in Python Using Matplotlib

Advertisements

Python can be used to plot multiple graphs. When we want to visualize data, we often need to plot multiple graphs. The library used for visualizing data in terms of graphs is Matplotlib. Performing a detailed analysis of the data helps you understand which features are important, what’s their correlation with each other which features would contribute in predicting the target variable. Different types of visualizations and plots can help you to achieve that. Here we will cover different examples related to the multiple plots using matplotlib.

How to Plot Multiple Graphs in Python Using Matplotlib

How to Plot Multiple Graphs in Python Using Matplotlib

Also Read: How to use Scikit-Learn in Python [Complete Tutorial]

Advertisements

Prerequisites

To go through below tutorial, as a prerequisite it is expected that you have a running Windows or Linux System with Python installed in your System. To install python libraries, it is also expected that you have pip python package manager available in your System.

 

Installation

If you don’t have matplotlib available in your Windows system then you can install it by using this pip install matplotlib command as shown below.

Advertisements
C:\Users\cyberithub>pip install matplotlib
Collecting matplotlib
Downloading matplotlib-3.6.0-cp310-cp310-win_amd64.whl (7.2 MB)
---------------------------------------- 7.2/7.2 MB 8.7 MB/s eta 0:00:00
Collecting kiwisolver>=1.0.1
Downloading kiwisolver-1.4.4-cp310-cp310-win_amd64.whl (55 kB)
---------------------------------------- 55.3/55.3 kB ? eta 0:00:00
Collecting packaging>=20.0
Downloading packaging-21.3-py3-none-any.whl (40 kB)
---------------------------------------- 40.8/40.8 kB 1.9 MB/s eta 0:00:00
Collecting cycler>=0.10
Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting numpy>=1.19
Downloading numpy-1.23.3-cp310-cp310-win_amd64.whl (14.6 MB)
---------------------------------------- 14.6/14.6 MB 6.8 MB/s eta 0:00:00
Collecting pyparsing>=2.2.1
Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
---------------------------------------- 98.3/98.3 kB ? eta 0:00:00
Collecting contourpy>=1.0.1
Downloading contourpy-1.0.5-cp310-cp310-win_amd64.whl (164 kB)
---------------------------------------- 164.1/164.1 kB 10.3 MB/s eta 0:00:00
Collecting python-dateutil>=2.7
Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
---------------------------------------- 247.7/247.7 kB 7.7 MB/s eta 0:00:00
Collecting fonttools>=4.22.0
Downloading fonttools-4.37.3-py3-none-any.whl (959 kB)
---------------------------------------- 960.0/960.0 kB 7.6 MB/s eta 0:00:00
Collecting pillow>=6.2.0
Downloading Pillow-9.2.0-cp310-cp310-win_amd64.whl (3.3 MB)
---------------------------------------- 3.3/3.3 MB 8.7 MB/s eta 0:00:00
Collecting six>=1.5
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, pyparsing, pillow, numpy, kiwisolver, fonttools, cycler, python-dateutil, packaging, contourpy, matplotlib
Successfully installed contourpy-1.0.5 cycler-0.11.0 fonttools-4.37.3 kiwisolver-1.4.4 matplotlib-3.6.0 numpy-1.23.3 packaging-21.3 pillow-9.2.0 pyparsing-3.0.9 python-dateutil-2.8.2 six-1.16.0

If you are using Debian/Ubuntu based Linux system, then you can install matplotlib by using pip install matplotlib command as shown below. If you are having latest Python 3 then you can install matplotlib library by using sudo apt install python3-matplotlib command.

cyberithub@ubuntu:~$ pip install matplotlib
Collecting matplotlib
Downloading matplotlib-3.6.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (9.4 MB)
|████████████████████████████████| 9.4 MB 1.5 MB/s
Collecting fonttools>=4.22.0
Downloading fonttools-4.37.3-py3-none-any.whl (959 kB)
|████████████████████████████████| 959 kB 7.6 MB/s
Requirement already satisfied: cycler>=0.10 in /usr/lib/python3/dist-packages (from matplotlib) (0.10.0)
Requirement already satisfied: python-dateutil>=2.7 in ./.local/lib/python3.8/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: packaging>=20.0 in /usr/lib/python3/dist-packages (from matplotlib) (20.3)
Requirement already satisfied: numpy>=1.19 in ./.local/lib/python3.8/site-packages (from matplotlib) (1.23.3)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/lib/python3/dist-packages (from matplotlib) (1.0.1)
Requirement already satisfied: pillow>=6.2.0 in /usr/lib/python3/dist-packages (from matplotlib) (7.0.0)
Requirement already satisfied: pyparsing>=2.2.1 in /usr/lib/python3/dist-packages (from matplotlib) (2.4.6)
Collecting contourpy>=1.0.1
Downloading contourpy-1.0.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295 kB)
|████████████████████████████████| 295 kB 6.6 MB/s
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7->matplotlib) (1.14.0)
Installing collected packages: fonttools, contourpy, matplotlib
Successfully installed contourpy-1.0.5 fonttools-4.37.3 matplotlib-3.6.0

 

Importing

The plt alias is typically used to import the majority of the Matplotlib functions, which are located in the pyplot submodule. To use matplotlib, we will import this library in our Jupyter notebook file first. With this, we will import numpy and pandas libraries that are required.

Advertisements

How to Plot Multiple Graphs in Python Using Matplotlib 2

Now the pyplot module can be referred as plt as shown above.

 

Pyplot

Let’s try to draw a very simple diagram with plt.

Advertisements

How to Plot Multiple Graphs in Python Using Matplotlib 3

The output of this code is:-

How to Plot Multiple Graphs in Python Using Matplotlib 4

As we can see in above fig, on the x-axis, the range of number are from 0 to 6 as we have defined range in code snapshot x = np.array(0,6). As well as on the y-axis, the range of number are from 0 to 6 as we have defined range in code snapshot y = np.array(0,250). In short, The x is the horizontal axis. The y is the vertical axis.

 

Subplots

We can draw more than one plot using plt.subplots(). Let’s try to build two plots in one figure.

How to Plot Multiple Graphs in Python Using Matplotlib 5

In this example, ax1 is used for first plot and ax2 used for second plot. And we have assigned values to x and y in previous section pyplot. The output of the above code snippet is:-

How to Plot Multiple Graphs in Python Using Matplotlib 6

In this snapshot, we can see on first figure, liner is originating from bottom to upwards, its showing positive values. In the Second figure, line originating from upper edge to bottom, its showing negative values.

 

Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.

How to Plot Multiple Graphs in Python Using Matplotlib 7

The output of the above code will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 8

We can draw two points in the diagram at our required positions. Let’s draw one at position (1, 8) and one in position (3, 10):-

How to Plot Multiple Graphs in Python Using Matplotlib 9

We can clearly see two points in following figure. If we consider first point that is at the bottom corner at the position of 1 at x-axis and 3 at the y-axis according to the coordinates we have specified in the code and same goes for other point/coordinates.

How to Plot Multiple Graphs in Python Using Matplotlib 10

Other than that, if we do not specify the points in the x-axis, they will get the default values 0, 1, 2, 3, etc. depending on the length of the y-points. So, if we take the same example as above, and leave out the x-points, the diagram will look like this:-

How to Plot Multiple Graphs in Python Using Matplotlib 11

The output of this code is:-

How to Plot Multiple Graphs in Python Using Matplotlib 12

We can clearly see in this figure it’s not bothering x-points at all.

 

Matplotlib Line style

We can plot line of multiple styles by using “linestyle” or ls keyword. Let’s plot dotted line first:-

How to Plot Multiple Graphs in Python Using Matplotlib 13

The output of the above code will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 14

Let’s try dashdot line now:-

How to Plot Multiple Graphs in Python Using Matplotlib 15

The output dashdot line will look like:-

How to Plot Multiple Graphs in Python Using Matplotlib 16

 

Matplotlib Line Color

Another interesting feature is we can change the color of line as well.  We can do this by using “color” keyword or simply “c”. Let’s have a look how we can do this:-

How to Plot Multiple Graphs in Python Using Matplotlib 17

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 18

 

Matplotlib Line Width

We can use the keyword “linewidth” or ”lw” to change the width of the line. The linewidth value is a floating number, in points. Let’s have a look how we can do this:-

How to Plot Multiple Graphs in Python Using Matplotlib 19

The output will look like:-

How to Plot Multiple Graphs in Python Using Matplotlib 20

 

Multiple Lines

We can plot as many lines as we want by simply adding more plt.plot() function. Let’s have a look how we can do this.

How to Plot Multiple Graphs in Python Using Matplotlib 21

The output will look like:-

How to Plot Multiple Graphs in Python Using Matplotlib 22

 

Matplotlib Labels and Title

We can label x-axis and y-axis by using xlabel() and ylabel() functions with Pyplot. Suppose we want to write “Average Exercise Time” on x-axis and “weight loss” at y-axis. Let’s have a look how we can do this:-

How to Plot Multiple Graphs in Python Using Matplotlib 23

We can clearly see labels on x-axis and y-axis.

How to Plot Multiple Graphs in Python Using Matplotlib 24

Other than x and y labeling, we can also put title for entire graph or chart by using title() function with pyplot. Let’s have a look how we can do this:-

How to Plot Multiple Graphs in Python Using Matplotlib 25

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 26

We can clearly see Title on top of graph.

 

 

Matplotlib Gridlines

we can add grid lines to the plot by using the grid() function with Pyplot. Suppose we want to add grid lines in above graph. Let’s see how we can do this:-

How to Plot Multiple Graphs in Python Using Matplotlib 27

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 28

Let’s suppose we want to have grid on y-axis only. We can do this by axis parameter in the grid() function to specify which grid lines to display.

How to Plot Multiple Graphs in Python Using Matplotlib 29

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 30

Conversely, if we want to have grid x-axis only. Then we will specify axis = ‘y’.

 

 

Matplotlib Scatter Plot

A Scatter (XY) Plot has points that show the relationship between two sets of data. we can draw a scatter plot by using scatter() function with Pyplot. Scatter() function used for plotting scatter plot takes two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis. Let’s see how it works:-

How to Plot Multiple Graphs in Python Using Matplotlib 31

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 32

We can set our own color for each scatter plot with the color or the c argument. Suppose we want to see dots or results of one plot or graph in green color and the other are in pink color in same figure or graph. Let’s see how it works.

How to Plot Multiple Graphs in Python Using Matplotlib 33

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 34

One most interesting factor is we can even set a specific color for each dot by using an array of colors as value for the c argument. The number of colors in c array should be equal to number of elements in x and y array to color easy dot in graph. Let’s see how we can do this:-

How to Plot Multiple Graphs in Python Using Matplotlib 35

The output with each dot colored will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 36

 

Matplotlib Bar Chart

A bar chart is used when you want to show a distribution of data points or perform a comparison of metric values across different subgroups of your data. Bar() function used for plotting bar plot takes two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis. Let’s see how it works:-

How to Plot Multiple Graphs in Python Using Matplotlib 37

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 38

Let’s see another example of bar chart according to categorical data:-

How to Plot Multiple Graphs in Python Using Matplotlib 39

The output for categorical data on x-axis is:-

How to Plot Multiple Graphs in Python Using Matplotlib 40

We can also specify color of bar chart as we want by using color or c argument. Let’s see an example:-

How to Plot Multiple Graphs in Python Using Matplotlib 41

The purple colored bar chart will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 42

 

 

Matplotlib Histogram Charts

Histogram is used to show the frequency distribution of a small number of data points for a single variable. Histograms frequently divide data into different "bins" or "range groups" and count how many points are in each bin.

We can use the hist() function to create histograms. This function will take an array of numbers to create a histogram, the array is sent into the function as an argument. Let’s see how it works:-

How to Plot Multiple Graphs in Python Using Matplotlib 43

The output will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 44

 

Matplotlib Pie Charts

We can use the pie() function to create pie charts. This function will take an array of numbers to create a pie chart, the array is sent into the function as an argument. Let’s see how it works:-

How to Plot Multiple Graphs in Python Using Matplotlib 45

The output of pie chart will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 46We can also label our pie chart as per our requirements. Let’s see an example:-

How to Plot Multiple Graphs in Python Using Matplotlib 47

The labelled pie chart will be:-

How to Plot Multiple Graphs in Python Using Matplotlib 48

 

 

Conclusion

Matplotlib is powerful library of python that can be used for making or plotting multiple graphs or charts for data visualization or data analysis. Matplotlib is open source and we can use it freely. In this article, we have learnt that how we can plot multiple graphs by using matplotlib python. We have also covered how we can use python matplotlib for data visualization and graphical plotting.

Leave a Comment