Basics of matplotlib library

Ashish Gusain
The Startup
Published in
4 min readApr 24, 2020

A picture is worth a thousand words, and with Python’s matplotlib library, it fortunately takes far less than a thousand words of code to create a production-quality graphic.

This article is a beginner-to-intermediate-level walkthrough on matplotlib that mixes theory with examples. While learning by example can be tremendously insightful, it helps to have even just a surface-level understanding of the library’s inner workings and layout as well.

Initial 2 lines, we are importing numpy and matplotlib libraries. In the third line, we have declared the size of the graph area as 10*5. Now, both x and y axis should have their values known to be displayed and therefore, we have assigned values to these 2 variables as arrays of same length. Now, finally you can plot them and alongside give a title as well as names to x-axis and y-axis.

For showing more than 1 curves in the same graph, you can use plotting as shown in the code.

Now, you might be wondering how to show multiple graphs together at the same time. So, here is what you need to do. Line 6 has a function named subplot which has 3 values. These values indicates row number, column number and an index number respectively. Thus by giving different index numbers you can know the position of a particular graph and by changing row and column numbers, you can change the number of graphs in a particular row or column.

Now, let’s see how we can work on images using matplotlib. An image is imported using opencv lib and now you can draw different bounding boxes and write your texts over them as shown above. This is quiet useful when you are working with deep-learning models and dealing with object-detection, as you keep on visualising bounding boxes.

Now, we can see how you can visualize the data in the form of pie charts. Provide names and share values (numeric) and it will automatically convert them into fractions to display as seen above. Some terms like share contains all the values, explode tells which portion in the chart should be exploded out, startangle changes the angle of the pie chart accordingly.

The above code shows a histogram displayed in the form of a bar graph.

Finally, you can also display scattered points in 2D as well as in 3D projection. This becomes quiet useful when you are working with Machine Learning models and wants to visualize the dataset.

Okay, so here comes an end to our explaination but there is much more that you can do with this library. If, you are a beginner, then the above given examples are more than enough for you for the time being and you can go much forward with the above examples only.

Dis-advantages:

Okay, so one thing you would have seen in the above codes that I am using numpy too much. Yes, matplotlib will force you to use numpy or some other libraries as you cannot store values in an array all the time.

Also, this library is only for python, so you can’t integrate this particular library with other languages.

So, finally here we can conclude the story. I have tried to keep all the basic uses of this library in front of you in the shortest way I can. Hope, you guys enjoyed reading this story.

This is all from my side. You can reach me via:

Email : ashishgusain12345@gmail.com

Github : https://github.com/AshishGusain17

LinkedIn : https://www.linkedin.com/in/ashish-gusain-257b841a2/

--

--

Ashish Gusain
The Startup

Full Stack Developer | MERN Stack | Data Science | ML