Matplotlib in Python: Explained Various Plots with Examples
By Rohit Sharma
Updated on Feb 24, 2025 | 12 min read | 6.29K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Feb 24, 2025 | 12 min read | 6.29K+ views
Share:
Out of the several libraries available in Python, matplotlib in python is one such visualization library that helps in the visualization of 2D plots of an array. The data visualization library is built on NumPy arrays. It was in the year 2002, that the multi-platform data visualization library was introduced by John Hunter. The library offers visualization of data and the graphical plotting of the data providing an alternative to MATLAB. Matplotlib’s APIs i.e. Application Programming Interfaces which are used by the developers to embed plots in GUI applications.
Several graphical plots like bar, line, histogram, scatter, etc. are offered by matplotlib. The visualization offered by the matplotlib plot allows access to huge amounts of data visually. The visual data plot can be generated through a code of few lines only due to the structured nature of a python matplotlib script.
Two APIs are used for overlaying the matplotlib scripting layer:
Check out our data science online courses to upskill yourself
The installation of the matplotlib library can be done through downloading of matplotlib and its dependencies from the Python Package Index (PyPI) as a binary package.
The command that can be used for installing the library is
python -m pip install matplotlib
In an operating system like Windows, Linux, and macOS, matplotlib and its dependencies are present as wheel packages. In such cases the command to be executed is.
python -mpip install -U matplotlib
The library is also available as uncompiled source files whose installation is fairly complex as the local system will require having the appropriate compiler for the OS. Also, the ActiveState Platform may be used for building matplotlib from source and package it for the required OS.
The importing of the matplotlib in python is carried out through the commands
The Matplotlib UI Menu is generated when plots are created through the Matplotlib. Customization of the plot and toggling of elements along with the ability to zoom into the plots are offered by the Matplotlib UI.
The NumPy is a package under python for carrying out scientific computations. Matplotlib is built over the NumPy and it uses the functions provided by NumPy for its numerical data and multi-dimensional arrays.
Pandas is a library of python that is used for the manipulation of data and analysis by matplotlib. It is not a required dependency for the matplotlib but provides a data frame.
Matplotlib plots allow the visual representation of huge volumes of data. With the plots, the trends and specific patterns present in data could be identified which is essential for making correlations. Matplotlib Plots basically provide a way for reasoning about quantitative information.
Using two points
Example: A code showing the plotting of lines and the generated plot
Popular Data Science Programs
ypoints = np.array([10, 8, 12, 20, 3, 9])
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
Syntax: plt.plot(ypoints, marker = ‘o’, ms = 30)
Syntax: plt.plot(ypoints, ls = ‘:’)
Syntax: plt.grid(axis = ‘x’)
Syntax: plt.grid(color = ‘green’, linestyle = ‘–‘, linewidth = 0.5)
Multiple plots can be drawn in one figure using the subplots() function.
Plot generated
Syntax: plt.scatter(x, y, c=colors, cmap=’viridis’)
Viridis is an in-built colormap available in matplotlib.
Syntax: plt.bar(x, y, color = “green”).
Syntax: plt.bar(x, y, width = 0.2).
upGrad’s Exclusive Data Science Webinar for you –
Watch our Webinar on The Future of Consumer Data in an Open Data Economy
Syntax: mylabels = [“cars”, “bikes”, “cycles”, “buses”]
Syntax: myexplode = [0.2, 0, 0, 0]
Syntax: mylabels = [“cars”, “bikes”, “cycles”, “buses”]
mycolors = [“black”, “hotpink”, “blue”, green””]
x = np.random.normal(90, 100, 200)
print(x)
As discussed in the article, matplotlib in python can be used for the plotting of the data in various styles. Further various options are available to enhance our plots allowing the user to label, resize, and color as per their wish. Therefore, python and its libraries are quite helpful for the analysis and handling of data in the present age.
Python programming training over the field of data science is available in the course Executive PG Programme in Data Science offered by upGrad. If you are willing to get trained under industry experts and explore the various opportunities held by data science, you can enroll in the course. The course is offered by IIIT-Bangalore and designed especially for Entry to mid-level professionals within the age group of 21 to 45 years. Irrespective of any gender, if you fall within this mentioned category and dream of becoming a leading data scientist, come join us in this venture. For any assistance ship, our team is ready to help you.
Check out All Python tutorial concepts Explained with Examples.
Pandas is one of the most important and useful Python modules that has various use cases. The following are some of the advantages of the Pandas module.
1. Pandas allow convenient data filtering and subsetting.
2. Its code is clean and understandable so that the users can focus more on the core aim.
3. Since it is written in NumPy, it inherits some of the useful features of NumPy as well.
1. The Matplotlib library provides numerous useful APIs to embed various types of plots including histogram, line and bar plots, scatter plots, and bar graphs.
2. This powerful library can help you create 2D plots using data stored in an array. Its simple code structure allows you to embed any type of plot just by adding a few simple lines of code.
3. It has an object-oriented interface which makes it a powerful alternative to MATLAB and Pyplot. It is highly customizable and but requires some experience to use the advanced features.
4. If you need to embed simpler plots in your application, then you should go for its MATLAB style Python interface. However, if you have complex plots, then its OOP interface would be a much better option.
834 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources