Matplotlib Tutorial _ A Basic Guide to Use Matplotlib with Python

Matplotlib Tutorial : A Basic Guide to Use Matplotlib with Python

Matplotlib is a Python plotting library which helps you to create visualization of the data in 2 -D graph. The Matplotlib Tutorial article is completely for beginners. In this tutorial, you will know the different ways to plot graph in Python Programming language.In Today’s world, you can find complications in different ways everywhere. Therefore we try to find best and quick solution to handle these complications. You all know big data, machine learning, data science e.t.c are the future trend. Companies using big data technologies have large data sets. They use these data sets for many purposes from predictions to recommendation e.t.c. For example, a finance company will try to choose the best stocks for investing using data visualizations.

1. Simple Introduction to Matplotlib

Before we begin in details, let’s understand the concept of Matplotlib. Matplotlib is a Python library for data visualizations. It helps you to mostly plot 2- D dimensional graphs. It is built upon Numpy and Scipy framework.There are various types of graph plotting can be done using Matplotlib. The following graph plots can be done.

  1. Bar Graph
  2. Histogram
  3. Pie Plot
  4. Scatter Plot
  5. Area Plot

Matplotlib library is already installed default to the newer version of python. However, if want to know how to install Matplotlib in details, then there is official Matplotlib website for it.

2. Why Matplotlib for Plotting graphs: Advantages

Matplotlib is opensource visualization library. There are also other Data Visualization tools but most of the tools in the list are paid. However, let’s focus on the some of the major advantages of Matplotlib.

  1. Matplotlib is open source library, thus it makes everyone to use it without paying any licenses fees.
  2. It runs on any operating system Windows, Linux, MacOs e.t.c.
  3. Matplotlib is a complete package for data visualization. Data can be acquired, elaborate and plot easily using this tool.
  4. It uses in any many application as it has a lot of graphs types, features. Thus making it more customizable according to various use cases.
  5. The last advantage of Maplotlib is that it uses Python which is very popular language among the data scientist.

The above are all the advantages of Matplotlib. Now you must be thinking what will be the disadvantage of it. Then there is only one disadvantage. It cannot be used with other programming languages except for Python. It means you can use it within Python code only. However, There is one programming language, where you can use it. That is Julia but via  PyPlot package.

In the next section of Matplotlib Tutorial, you will learn how to plot different types of the graph with the simple example. We are using Pycharm Python IDE for programming.But you are free to use other IDEs. Read the Best Python IDEs for Data Science article to find out the other IDEs. In addition, you make sure that you should type all code yourself to learn easily.

3. Line Chart Plotting

You must import matplotlib library module before plotting the figures. The methods plot(x,y) is use for plotting the graph and show()  for displaying the figures.

First Simple Matplotib Plotting

import matplotlib.pyplot as plt #import the Python Matplotlib sub-module for graph plotting pyplot.
x = [1,2,3,4] # x axis
y = [1,2,3,4] # y axis
plt.plot(x,y)# plotting the graph
plt.show() #Displaying the figures

The above code is very basic and simple example of Line Plotting.Below is the definition of each line code. It describes  what these lines are mean by ( functionality ). Please remember it will be very useful for you to understand the other examples.

Line 1: import matplotlib.pyplot as plt will import the Python Matplotlib sub-module for graph plotting pyplot.

Line 2 : plt.plot(x,y)  is actually a plotting command. This command will plot the values from x values to the horizontal axis and y values to the Y- axis.

Line 3: plt.show() command will open the window contains the image of the plot. The output of the above codes is below.

matplotlib tutorial Firstplot python code
Firstplot

Range Plotting (Parabola)

import matplotlib.pyplot as plt #import the Python Matplotlib sub-module for graph plotting pyplot
import numpy as np  #import the numpy module
x =np.arange(0.0,6.0,0.01)
y = [x**2 for x in x]
plt.plot(x,y)# plotting the graph 
plt.show() #Displaying the figures

arange(x,y,z) is the part of Numpy library. Here x and y are elements with defined datatypes inside the parameters. For example inside the parameter of the function arange(0.0, 6.0, 0.01)  double data types have been used. This command generates the sequence of elements from x to y with spacing as defined in z. The statement y = [x**2 for x in x] will square the values of x and assign it into the y variable.

Parabola matplotlib
Parabola

Multiple Lines Plotting on the Same Graph

Matplotlib has also the ability to plot multiple numbers of lines on the same graph. In addition, all the lines have also the different color. The method plot() method can contains many lines. Like plot(x,y1, x,y2,x,y3…).

# File name: MultipleLines.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x =np.arange(1,10) # values from 1 to 10
y1 = x+2
y2 = x*6
y3 = x/5
plt.plot(x,y1, x,y2, x,y3) # plot the figure
plt.show()

plt.plot(x,y1, x,y2, x,y3) contain three lines x+2, x*6 and x/5. You can see the figure matplotlib automatically define a color of the line. In this case, orange color for the y1= x+2 line, blue for the y2 = x*6 and green for the last y3= x/5 line.

 

MultipleLines graph
MultipleLines

We can also change the color of the specific line. Matplotlib provide this feature. You just have to put the color = “color name” as the arguments in the plt.plot().The complete code of this case is below.

# File name: MultipleLines.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x =np.arange(1,10)# values from 1 to 10
y1 = x+2 
y2 = x*6
y3 = x/5
plt.plot(x,y1,color="red")
plt.plot(x,y2,color="pink")
plt.plot(x,y3,color="black")
plt.show()

plt.plot(x,y1,color=”red”): Color of this line will be red

plt.plot(x,y2,color=”pink”): Pink in color

plt.plot(x,y3,color=”black”): This line will be of black color

MultipleLinesCustom Color
MultipleLinesCustom Color

4. How to add Grids, Axes, and Label to the Plot Diagram?

You can see all the above figures have no labels and grids. In fact, you can add all these features inside the plot diagram. In this section of matplotlib tutorial, you will learn how to add grids and labels inside the figure.

Adding Grids to the Figure

You may know adding grids to any graphs increase the readability of it. Its okay not to put grids inside the graphs that have one line. But what about the figure with the multiple lines. We must put grids in this type of situation.

In all the above examples, you can put grid using the command plt.grid(True).

How to add Label to Figure?

You can also add labels to define more the x-axes and y-axes.

plt.xlabel(“This is x-axis label”): You will able to add the label to X-axis.
plt.ylabel(“This is y-axis label”): This command will add the label to Y-axis.

Like in the following example.

# File name: MultipleLines.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x =np.arange(1,10)
y1 = x+2 
y2 = x*6
y3 = x/5
plt.plot(x,y1,color="red")
plt.plot(x,y2,color="pink")
plt.plot(x,y3,color="black")
plt.grid(True)
plt.xlabel("This is x-axis label")
plt.ylabel("This is y-axis label")
plt.show()

 

MultipleLinesWithGrid_Labels
MultipleLinesWithGrid_Labels

There are other features also Matplotlib provides. For example Title and legends. Each article has a particular title. It describes what the article is about. In the same way, Title describes the graph and legends explains all the lines inside the figure. Lets us take the example of Multiple Lines plotting. We will add title  to it. The complete code is given below.

# File name: MultipleLines.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x =np.arange(1,10)
y1 = x+2 
y2 = x*6
y3 = x/5

plt.plot(x,y1,color="red")
plt.plot(x,y2,color="pink")
plt.plot(x,y3,color="black")
plt.grid(True)
plt.xlabel("This is x-axis label")
plt.ylabel("This is y-axis label")
plt.title("Multiple Line Charts")
plt.show()

plt.title(“Title name”) is used to add a title to the graph.

5. Bar Chart Plotting

I hope you are getting interested in more about how to plot others chart plotting. In this section of matplotlib tutorial, you will know plotting of bar chart.

You must know about the bar chart. It is a rectangular display either horizontal or vertical axis. The length of the bar is directly proportional to the values they represent.

Simple Bar chart Plotting

You will use the bar() method for plotting the bar chart. The plt.bar([x1,x2,x3…],[y1,y2,y3…])  method is use in the code. x1,x2,x3… are values on the x-axis and y1,y2,y.. are height of bar chart. The following code will display a very simple bar chart.

# File name: SimpleBarChart.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
plt.bar([1,2,3],[5,2,1])
plt.xlabel("This is x-axis label")
plt.ylabel("This is y-axis label")
plt.title("Simple Bar Chart")
plt.show()

 

plt.bar([1,2,3],[5,2,1]) command will plot ar chart at the distance 1,2 and 3 with height of 5,2 and 1 respectively.

Simple Bar chart figure

The default width of the bar chart is 0.8. But you can change it is using width as an argument to the command plt.bar(). For example to change the width of 0.3,use this command  plt.bar([1,2,3],[5,2,1],width=0.3). In the same way, you can also change the color of the bar chart. You have to use color as an argument to the plt.bar(). For example to change the color use the following command.

 plt.bar([1,2,3],[5,2,1],width=0.3,color=”red”)

Below is the Overall code of the above example

# File name: SimpleBarChart.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
plt.bar([1,2,3],[5,2,1],width=0.3,color="red")
plt.xlabel("This is x-axis label")
plt.ylabel("This is y-axis label")
plt.title("Simple Bar Chart")
plt.show()
Simple Bar chart with red color
Simple Bar chart with red color

Let’s take another example of plotting bar chart. Suppose you have a list of students and their marks out of 100. You have to plot a bar chart of it.

student_name= [ “John”,”Sahil”,”Monica”,”Lara” ]

marks = [75,98,53,70]

You have to draw the values in x axis using the number of elements in the list that is length. Therefore you will use the statement index = np.arange(len(student_name)) to find out the total number of values in the x -axis. It will  generate array of sequences number .

# File name: StudentBarChart.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
student_name= [ "John","Sahil","Monica","Lara" ]
index = np.arange(len(student_name))
marks = [75,98,53,70]
plt.bar(index,marks)
plt.xlabel("Student Name")
plt.ylabel("Marks")
plt.title("Student Performance")
plt.xticks(index,student_name)
plt.show()

There are two list student_name and marks. In this case it will generate array [0,1,2,3]. It will decide the number of bars in the chart. In the same way, plt.xticks(index,student_name) will put the name of students on the x-axis.

StudentBarChart
Student Bar Chart

6. Pie Chart Plotting

The pie chart is the circular representation of the values. It is divided into sectors and is directly proportional to the values. You can compare pie chart as a whole pizza and one piece of pizza as sectors.

How to plot a simple pie chart?

The pie() function in the Maplotlib provides you to plot pie chart from an array x. Let us understand more with the simple example. Suppose percentage of programming language Python, Java, and C++ used by developers are 60%,30%, and 10% respectively. The simple code of pie chart of this example is the below.

Therefore you will make the following lists.

percentage = [60,30,10]

language= [“Python”,”Java”,”C++”]

Then you will put these list as arguments in the pie() method.

# File name: SimplePieChart.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
percentage = [60,30,10]
language= ["Python","Java","C++"]
plt.pie(percentage,labels=language)
plt.title("Simple Pie Chart")
plt.show()

You can see the above code plt.pie(percentage,labels=language) has been used for plotting pie chart for the given example. The code line language= [“Python”,”Java”,”C++”] use to labels the chart.

Output

SimplePieChart
SimplePieChart

7. Scatter Plotting

Scatter Plots displays two sets of data values in the single figure. You can also think it as aggregates of the points not connected to the lines. Points are determined on the basis of the value of x and value of y. In addition, linear regression, the correlation also uses scatter plotting.

How to plot a simple scatter plot?

We use scatter(x,y) function of matplotlib for plotting to scatter plot. One thing you should keep in mind that the number of values of x and y should be the same for plotting it. Below is the complete code for plotting simple scatter plot.

# File name: StudentScatterPlot.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x= np.random.rand(100)
y=np.random.rand(100)
plt.title("Simple Scatter Plot")
plt.scatter(x,y)
plt.show()

The np.random.rand(100) statement will generate the random numbers between 0 and 100. X and Y have been assigned random numbers between 0 to 100 and plt.scatter(x,y) function will plot a scatter chart. You can also change the size and color as arguments.

plt.scatter(x,y,size,color)

Output

SimpleScatterPlot
Matplotlib Tutorial: Simple Histogram Chart

8. Histogram Charts Plotting

Histogram chart displays the data sets for each category as the bar. Bars are also called as bins in Matplotlib. As you use the function pie() for display pie chart, in the same way, You use the function hist() to display histogram chart. The simple code for plotting the histogram chart is given below.

# File name: SimpleHistogramChart.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x= np.random.rand(100)
plt.title("Simple Histogram Chart")
plt.hist(x,10)
plt.show()

The plt.hist() function has been used for showing the histogram chart.

Output

Matplotlib tutorial Simple Histogram Chart
Matplotlib tutorial Simple Histogram Chart

9. Area Chart Plotting

You must have been able to draw line chart using matplotlib. In the similar way area chart is just a line chart . Only difference is that the area between the x -axis and the line is filled with color. You will use the function fillbetween(x,y) function of matplotlib library to use this. Below is the code for the simple area chart plotting example.

# File name: AreaChart.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,7)
y = [1,2,3,4]
plt.fill_between(x,y)
plt.title("Simple Area Chart")
plt.show()

 

10. Some Other Features  of Matplotlib

In the previous sections, You have learned how to plot different types of charts with Matplotlib. In this section of matplotlib tutorial, you will know the other major features of matplotlib. What are the other things you can do with the chart using it?

Adding text inside the figure

You have already know xlabel(),ylabel(), and title() for describing the chart. Now lets us know to add some text inside the figure. You have to use text() function for this. In fact, you will add x and y coordinates as well as text as an argument to show the text inside the figure. The code for this case is given below.

# File name: AddTextInFigure.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x= np.arange(0,2*np.pi,0.02) # values of x from 0 to 2pie
y=np.sin(x)
plt.plot(x,y)
plt.text(1.5,1,"Sin(90)=1")
plt.title("Add the text inside Figure")
plt.show()

The command plt.text(1.5,1,”Sin(90)=1″) will display the text ” Sin(90) = 1 inside the figure at the coordinates x =1.5 and y =1 .

Output

AddTextFigure
Matplotlib tutorial Adding Text to the Figure

How to add Subplots to the Figure?

In the previous examples, you were plotting single charts on  single figure. It means it has only one x and y-axes. But you can easily put more than one axes to the figure using subplot functions. There are two functions require for creating subplots.

  1. figure() 
  2. add_subplot(n_rows,n_coloumns,figureNumber)

The figure() functions return the figure. You can add one or more axes instances in the figure. In the same way add_subplot() function return the axes instance for adding the subplots to the figure returned. The function add_subplot take three parameters

  1. n_rows : The number of rows of subplots
  2. n_coloumns: It is the total number of columns of subplots
  3. figurenumber: It is the integer whose values are from 1 to n_rows*n_columns.

You will understand more on how to draw subplots using the example below.

# File name: Subplots.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
figure= plt.figure()
figure.suptitle("Subplot Graph")
axis1 = figure.add_subplot(2,1,1)
axis1.plot([1,2,3],[1,2,3])
axis1.set_title("First Plot")
axis2 = figure.add_subplot(2,1,2)
axis2.plot([1,2,3],[3,2,1])
axis2.set_title(("Second Plot"))
plt.show()

The function figure.add_subplot(2,1,1) will return axes to the variable axis1. You can see the arguments for this function are the number of rows that is 2, number of columns that is 1 and figurenumber which varies from n_rows*n_columns that are from 1 to 2.

Output

subplots
Matplotlib tutorial Subplots Figure

11. Adding Multiple Figures

In the previous examples, there was only one figure on the chart. In this section, you will know how to add multiple figures in the same chart. The code of this example is similar to the subplot example. Only you have to some modification with the axes and plot an extra figure. The below simple code for adding multiple figures will help you to understand more.

# File name: MultipleFigure.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
figure1= plt.figure()
figure1.suptitle("Subplot Graph")
axis1 = figure1.add_subplot(1,1,1)
axis1.plot([1,2,3],[1,2,3])
axis1.set_title("First Figure ")
figure2= plt.figure()
axis2 = figure2.add_subplot(1,1,1)
axis2.plot([1,2,3],[3,2,1])
axis2.set_title(("Second Figure"))
plt.show()

You can see in the code you have created two figures figure1 and figure2. The add_subplot(1,1,1) has been given three arguments 1,1, and 1. It means one figure will have 1 number of rows and columns. Therefore, it considers as the full plot as with two different figures.

Output: When you run the above program, the output will be two figures. It will the two subplots in two figure.

 

How to save a Plot Figure?

The above example of chart plotting only gives the output. However, you can also write the code to save the plot to a particular location on the computer. The function savefig(“FilepPath/File Name.png” ) will allow you to save the plotted figure. You can also define the path and file format before saving to the computer.  The png, pdf, ps, eps and svg are the various file formats you can save.You will understand more with the following code.

# File name: AddTextInFigure.py
# Author: Data Science Learner
# Begin code
import matplotlib.pyplot as plt
import numpy as np
x= np.arange(0,2*np.pi,0.02)
y=np.sin(x)
plt.plot(x,y)
plt.text(1.5,1,"Sin(90)=1")
plt.title("Add the text inside Figure")
plt.show()
plt.savefig("TextFigurePlot.png")

The function plt.savefig() will save the plot figure with the file format png and TextFigurePlot as the filename.

 

Conclusion

Data visualization plays a crucial role in understanding many things that are not understandable without it. It reduces the time to analyze the data and making decisions based on it. There are also the different thing that matplotlib libraries can do. But in this article of matplotlib tutorial, we try to cover those features which are generally used. Line chart, Bar chart, Histogram and scatter charts are the most used charts in every field that uses a large amount of data. In addition, Matplotlib is open source package, thus making it widely used tools for data visualization. You don’t have to pay extra fees for analyzing and visualizing the data. In addition, it is also able to run in all the operating systems like Windows, MacOS, and Linux. You can find other data visualizations but you cannot compare matplotlib with other tools.

We hope that this matplotlib tutorial has given most of the answers you were searching. Even if you unable to find this matplotlib tutorial article, You can give suggestion to us for improving this article. You are always welcome to contact us.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner