4 Matplotlib
4 Matplotlib
Matplotlib is a low-level graph plotting library in python that serves as a visualization utility.
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and
JavaScript for Platform compatibility.
Matplotlib Pyplot:
Pyplot:
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported
under the plt alias:
Matplotlib Plotting:
If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to
the plot function.
Example:
plt.plot(xpoints, ypoints)
plt.show()
Result:
To plot only the markers, you can use shortcut string notation parameter 'o', which means
'rings'.
Example:
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
Matplotlib Markers:
Markers:
You can use the keyword argument marker to emphasize each point with a specified marker:
Marker Reference:
Marker Description
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)
'+' Plus
's' Square
'D' Diamond
'p' Pentagon
'H' Hexagon
'h' Hexagon
'^' Triangle Up
'2' Tri Up
'|' Vline
'_' Hline
You can also use the shortcut string notation parameter to specify the marker.
This parameter is also called fmt, and is written with this syntax:
marker|line|color
Line Reference:
Color Reference:
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Marker Size:
You can use the keyword argument markersize or the shorter version, ms to set the size of the
markers:
Marker Color:
You can use the keyword argument markeredgecolor or the shorter mec to set the color of the
edge of the markers:
You can use the keyword argument markerfacecolor or the shorter mfc to set the color inside
the edge of the markers:
Example:
import numpy as np
Matplotlib Bars:
Creating Bars:
With Pyplot, you can use the bar() function to draw bar graphs:
Example :
Draw 4 bars:
plt.bar(x,y)
plt.show()
Result:
Example:
plt.pie(y)
plt.show()
Result: