In the previous section, we have seen how to plot the graphs in Matplotlib. However, Matplotlib is more than just plotting the graphs. We can also plot the pixels on a Matplotlib GUI. There is a particular function which can show the image on Matplotlib main window. This function is called the imshow() function.
However, the question arises how this happens. For showing an image on Matplotlib, we have to convert the image into NumPy. NumPy is a special module in python that is used for scientific calculation and for calculating the pixel values. We will see in depth about the NumPy module. Here we will convert the image into NumPy, and except giving the data in the list data structure, we will provide it in NumPy array data structures.
Following is the method to show the image:
If we print the image it will be printed as a NumPy array following is the output of the above-given code:
It is a nested array each nested array shows one pixel as RGBA value. This NumPy array, when subjected to Matplotlib, will be plotted on a window:
In the first line, we import Matplotlib to plot the graph, and then we import the image module of Matplotlib to read the image file from the local device
The imshow() function plot the pixel on the main window and in last we show the image.
The above code output the following image.
In Image processing and object detection, we identify the image with its histogram. Just like the height and attributes of different persons are different similarly the object has different histograms. Histograms are the pixel height of an object. Now let’s suppose we have an image of hand as follows we will first plot the image and then its histogram.
Following is the code of outputting the histogram:
The output will be the following:
Now to make a histogram of the above image following code is to be followed:
The output of the above code is as follows:
As we can see the image is not too much dense that is why the histogram is clear. If the image is denser the graph of a histogram is also dense. We will see in much detail about histogram when we will learn about OpenCV.
Plotting with Animation:
As I have already stated that plotting in Matplotlib is not just plotting a graph, it is way more than that. We can plot a graph, plot images, medical images, can make 3d plots and now we are going to animate the simple line graph which can make it more effective.
Following is the code of animating the graph:
First, we will see what is happening in the code. First, as usual, we imported the pyplot library from Matplot, we also imported the NumPy module because we are going to plot the sin wave on a window. In the third line, we imported the function method from the Matplot library.
We initialize the subplot and also initialize the x and y as an empty list data structure. Then we define an init function which assigns the x limit of the gram and y limit. Here the y limit is in range of -1 and 1. Then there is another function which is an update function which takes the frame. It will be updated on each successive cycle and plot the x and y on Matplot lib window. We append the data in x and y and set the data as x and y and return the plot instance named as g_plot. Then we start the animation by calling the module func animation in which we have provided the figure instance, update_func, and its parameters. After this, we plot it in the graph. The graph will be updated every seconf and will be completed slowly making an animated effect.
The above image is taken just a few seconds after it started. At the end the graph looks like this:
This graph is made by using the scientific formula of creating the sine wave.
The Matplotlib is so vast that we can’t completely cover it in just three articles. We can do a lot more. However, this is it for now as I can’t want to make it more complicated. Data visualization is the key to data science without data visualization data science is nothing. So, make yourself efficient before we dive into data science.