NumPy Matplotlib - NumPy

What is NumPy Matplotlib?

The plotting library used for Python is known as Matplotlib. As an alternate to MatLab, An effective open source environment is provided by this Matplotlib along with NumPy.

The Matplotlib was first developed by John D Hunter and the version that is currently being used is 1.5.1. The link to obtain the source code form is

The Matplotlib is imported into the Python by using the statement as follows:

To plot 2D data, pyplot() is the function used in matplotlib library. The equation y = 2x + 5 is plotted by the following script.


The values of the x axis is created from np.arange() function and ndarray object y stores the values on the y axis. These values are plotted using plot() function of pyplot submodule of matplotlib package.

The show() function is used for graphical representation.

The output produced by the code is:

Matplotlib Demo

A format string can be added to the plot() function to display the values instead of the linear graph by using the following characters.

Solid line style
Dashed line style
Dash-dot line style
Dotted line style
Point marker
Pixel marker
Circle marker
Triangle_down marker
Triangle_up marker
Triangle_left marker
Triangle_right marker
Tri_down marker
Tri_up marker
Tri_left marker
Tri_right marker
Square marker
Pentagon marker
Star marker
Hexagon1 marker
Hexagon2 marker
Plus marker
X marker
Diamond marker
Thin_diamond marker
Vline marker
Hline marker

The colour abbreviations that are defined are:


Instead of lines, the circles representing the points can be displayed by using the “ob” format string in the plot() function.


The output produced by the above code appears as:

Colour Abbrevation

Sine Wave Plot

By using the matplotlib, the sine wave plot can be produced by the following script.


Sine Wave


Different things can be plotted in the same figure by suing the subplot() function. The sine and cosine values can be plotted by the following script.


The output produced by the above code appears as:

Sub Plot


The bar graphs can be generated by suing the bar() function of the pyplot submodule. The bar graphs of two sets x and y arrays is produced as follows.


The output produced by the above code appears as:

Bar Graph

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

NumPy Topics