Matplotlib in Python: Explained Various Plots with Examples
Updated on Feb 24, 2025 | 12 min read | 5.7k views
Share:
For working professionals
For fresh graduates
More
Updated on Feb 24, 2025 | 12 min read | 5.7k 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
ypoints = np.array([10, 8, 12, 20, 3, 9])
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.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources