top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Matplotlib -- Python's powerful plotting library

In my previous blog, I discussed about Numerical Python NumPy and powerful data manipulations library Pandas .

In this blog , I will be showing the stunning visualizations using one of the comprehensive library from python "MATPLOTLIB".


What is Python Matplotlib ?


The name Matplotlib derived from the words "Matlab" and "plotting"

It was originally developed by John.D.Hunter in 2003 as way to replicate Matlab's plotting capabilities in python.

Matplotlib is a plotting library for python programming language and it is numerical mathematics extensions NumPy.


Matplotlib library built upon NumPy. It is tree like hierarchical structure which consists of objects that makeup each of these plots.


Let us see the Anatomy of a figure in Matplotlib.

Figure : The entire window in which plots are drawn. It serves as the outer most container for all elements.

Axes : The region of the figure where data is plotted. Axes said to be subplot of figure. A figure can have one or more axes. Each axis has own set of coordinate axes (x-axis and y-axis). Axes comes with tick marks, lines, legends, text boxes.


Axis : The x-axis and y-axis with an individual axis. They represent the value of ranges of data where plotted.


X label and Y label : Text labels along x-axis and y-axis respectively.


To get started , import the necessary libraries


Matplotlib consists of several plots such as :

  • Line plots

  • Bar plots

  • Histogram plots

  • Scatter plots

  • Pie plots

  • 3D plots and many more


Matplotlib is compatible with both NumPy and Pandas

But Firstly Lets understand the Matplotlib visualization using NumPy Arrays


Matplotlib using NumPy Arrays with example


Line synchronization Plots: The line plot is a basic and easiest graphical analysis technique that plays important role when expressing the relationship between two or more variables.


So, Let's create 3 different line plots with different styles, markers and labels .

In this code, a line chart is created using sample 'a', 'b', 'c' data points displaying a,b,c coordinates meeting points clearly.


Bar plots: Bar charts or Bar plots that represents the category of data with rectangular bars of length and heights that are proportional to values.

In this code, y1 and y2 are plotting on top of each other. These stacked bar plot values display different colors to show their cumulative total.


Back to Back Bar charts  are just combination of two bar charts that draws with respect to x-axis.

In this code, plt.barh() function used to created horizontal bar plot for both arrays A and B. arange function used to create an array for evenly spaced values, which defines the boundaries of bins.


Histogram Plots: A data provided in form of some groups are represented by Histogram plots. It is an accurate method for the graphical representation of numerical data distribution. It is a type of bar plot  where x-axis represents the bin range while y-axis gives information about frequency.


This code creates a stacked histogram chart with two random datasets using plt.hist() function.


2D Histograms: A 2D histogram is also called heatmap, where the values are grounded into bins and each bin assigned as color based on number of data points it contains.


In the above code , plt.hexbin() is the function used to create 2D hexagonal binning with the random distribution data arrays of x and y where mean is set to 3.




Scatter plots: Scatter plots are the data points on the graph between x-axis and y-axis in matplotlib library. The points in the graph looks scattered hence the plot named as scattered plot. 

In Scatter plots, by default the size of the data points are always small in size . Unless the parameter 'S' is specified to display the difference in sizes .


Sub Plots: The subplot function in Pyplot module is used to create a figure and set of subplots.

Subplots are often said as the supporting plots for the main plot.


Lets understand the concept of subplots from students marks and grades plotting:



Now, Lets see exactly the supporting plots here ,




Finally, Lets combine all the plots to see the perfect graph,



Pie charts: Pie charts are the statistical graphs divided into slices that represents different data values and the sum upto 100%.  In matplotlib library the pie chart has a feature explode that allows to separate the slices.


Let us see how we specify the explode portions to visualize the profit and sale distribution.


3D Plots are used to visualize the data that are having 3 dimensions such as, 2 dependent and 1 independent variable. By plotting the data in 3D we get deeper view of the data.


Lets understand some basic different 3D plots


3D Scatter plot: This plot shows numeric column values in rotatable and 3 dimensional view.



3D Line plot or 3D Point : Its a graphical representation of data where set of points in 3D space are connected by line segments.




3D Surface Plots: Its a graphical representation of mathematical functions, where surface plot represents variations in functions. It's a valuable tool in visualizing complex relationship patterns in multi dimensional data.


When creating 3D surface plot, first we build a mesh grid ,

After building mesh grid , now lets see the magic with just one line code using 'Z' function to get the values of X and Y






3D Wire Frame plot: It's an edge or skeletal representation of real world object.


Now , Lets generate the wire frame with again one line code from the above dimensions of X Y And Z values.



In conclusion, Matplotlib is a must known tool who are interested in visualizing the insights of data graphically.

Its ability to create wide range of plots not only just with NumPy , integration with Pandas and Seaborn is also in demand. However, Matplotlib 3D plots are bit complex for the new users like me, but constant practice helps to overcome the challenges.


Overall Matplotlib predominantly used in various fields where data plays a crucial role in analyzing and decision making such as Engineering and Technology, Academic research, Finance and Economics, Geospatial Analysis, Machine learning and Artificial Intelligence.


I hope this documentation of code with examples helps you understand the fundamentals of Matplotlib plotting.


Happy Coding !!




64 views0 comments

Recent Posts

See All

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
bottom of page