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 www.matplotlib.org.

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.

Example

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.

Character
Description
'-'
Solid line style
'--'
Dashed line style
'-.'
Dash-dot line style
':'
Dotted line style
'.'
Point marker
','
Pixel marker
'o'
Circle marker
'v'
Triangle_down marker
'^'
Triangle_up marker
'<'
Triangle_left marker
'>'
Triangle_right marker
'1'
Tri_down marker
'2'
Tri_up marker
'3'
Tri_left marker
'4'
Tri_right marker
's'
Square marker
'p'
Pentagon marker
'*'
Star marker
'h'
Hexagon1 marker
'H'
Hexagon2 marker
'+'
Plus marker
'x'
X marker
'D'
Diamond marker
'd'
Thin_diamond marker
'|'
Vline marker
'_'
Hline marker

The colour abbreviations that are defined are:

Character
Color
'b'
Blue
'g'
Green
'r'
Red
'c'
Cyan
'm'
Magenta
'y'
Yellow
'k'
Black
'w'
White

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

Example

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.

Example

Sine Wave

subplot()

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.

Example

The output produced by the above code appears as:

Sub Plot

bar()

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.

Example

The output produced by the above code appears as:

Bar Graph

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

NumPy Topics