It is easy to understand the data when it is visualized beautifully. Microsoft has its separate software known as Microsoft excel which is extensively used for its mathematical calculation and visualizing the data by just one click. There are lots and lots of websites like Stock Exchange websites or cryptocurrency websites which shows the market trend with the help of graph because the data is so vast that it is not possible to understand by writing in a column.
In python, Matplotlib is the module that is used to visualize the data beautifully. It is the basic modules of all new visualizing toolkit. Not just you can plot a graph of data ranging from one point to the other, but also you can plot pixel of an image and even on a higher level we will see we can plot the medical images which are present in the form of slices.
In this article, we will learn Matplotlib as the beginner level by just using lists and normal function of python. At a higher level, after learning pandas and NumPy, we will see how to plot more dynamical graphs.
We have to install the Matplotlib by using the pip command as it does not come pre-installed like some of the modules. To install Matplotlib, you have to open your terminal or a command prompt and write the following command:
After running this command open your python IDE and import Matplotlib. If it imports without giving any error, then it is fine else try again to install Matplotlib.
As mention in figure 1, the import line does not give any error which indicates it is installed and we are good to go.
Plotting a simple graph:
To plot a simple graph, we need some information or data set that is to be represented. Initially, we will take the data in the form of the list, but it can be considered as the NumPy array or pandas data frame. Following is the method to plot a simple graph of 1 and 0 numbers in the list as the data set.
In the above code, we first import the Matplotlib pyplot class. It is convenient to call the module as plt otherwise it will be a huge name. You can name it whatever you want, but I suggest you write it as plt because where ever you see the code even the original python documentation, they follow the same standard.
We then plot a graph by giving a list of integers as an argument. Here the data is in the range of zero and one. The second argument is r- which indicates that it is the line graph.
In third and 4th line we gave the x and y label their respective name. Moreover, in last we call the show function. If show function is not called the code will run fine, but it will not visualize the graph. Following graph will be outputted:
As we can see on the y-axis number label is printed, and the same in case of x-axis the label is printed. On the y-axis, the range is from 0 to 1 as the data set.
There are many types of the graph which we can use according to our need. Now we will see how to make a dot graph. However, before using it, we must know where we use a dot graph. Usually, dot graphs are used where we want to mark the point or to make the clusters to see where the density is high and where it is low. Suppose we have to analyze the data of crime of a particular country. We have data from different cities, and the data is so vast that we cannot understand it by just looking, so we plot the graph according to the location and make the clusters. By these clusters, we can see where the density of crime is high and where it is low.
Following is the code of the dot graph:
In the above code, we have given the three arguments in the plot function. The first list is the points of the x-axis and the second list are the points of the y-axis. Here the row indicates the graph is dotted. All the remaining code is the same as we mentioned above. Following graph is returned as an output:
Graphs on the same window:
Sometimes there is a need for applying some functionality on a data and analyze their changes on a graph. In such scenarios, we plot multiple lines on the same graph to know what are their changes concerning the time and increasing functionality.
Now we will plot a graph in which we take data and apply the multiplication of 3 and 5 and see their changes.
In the above example, we plot data with the multiplication of three and five make the color red, blue and green. r, b, and g indicate the color. Figure 4 shows the outputted result as a graph.
In the above figure 4, the red line indicates the original data, and the blue line indicates the data that is multiplied by the digit 3 and green line shows the data set multiply by 5.
Saving the Image:
The graph can be saved in the local disk as a png or jpg file. There are two ways to store the image in the local drive.
The one method is to click the floppy disk icon on the window showing the graph this will save them as an image.
The other way of storing as an image is by calling the same function as follows: