What is Matplotlib?
Matplotlib is a low-level graph plotting library in python that serves as a visualization utility.
Matplotlib was created by John D. Hunter.
Matplotlib is open source and we can use it freely.
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:
import matplotlib.pyplot as plt
Now the Pyplot package can be referred to as plt.
Matplotlib Plotting:
Plotting x and y points:
The plot() function is used to draw points (markers) in a diagram.
By default, the plot() function draws a line from point to point.
The function takes parameters for specifying points in the diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.
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:
Draw a line in a diagram from position (1, 3) to position (8, 10):
import matplotlib. Pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()
Result:
Plotting Without Line:
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):
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints, 'o')
plt.show()
Result:
Matplotlib Markers:
Markers:
You can use the keyword argument marker to emphasize each point with a specified marker:
Marker Reference:
You can choose any of these markers:
Marker Description
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)
'+' Plus
'P' Plus (filled)
's' Square
'D' Diamond
'd' Diamond (thin)
'p' Pentagon
'H' Hexagon
'h' Hexagon
'v' Triangle Down
'^' Triangle Up
'<' Triangle Left
'>' Triangle Right
'1' Tri Down
'2' Tri Up
'3' Tri Left
'4' Tri Right
'|' Vline
'_' Hline
Format Strings fmt:
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:
Line Syntax Description
'-' Solid line
':' Dotted line
'--' Dashed line
'-.' Dashed/dotted line
Color Reference:
Color Syntax Description
'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 matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, 'x--r', ms = 50, mec = 'r', mfc = 'b')
plt.show()
label, title, grid:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, 'x--r', ms = 50, mec = 'r', mfc = 'b')
plt.title("Sports Watch Data")
plt.xlabel("A")
plt.ylabel("B")
plt.grid()
plt.show()
Matplotlib Bars:
Creating Bars:
With Pyplot, you can use the bar() function to draw bar graphs:
Example :
Draw 4 bars:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
plt.show()
Result:
Matplotlib Pie Charts:
Creating Pie Charts
With Pyplot, you can use the pie() function to draw pie charts:
Example:
A simple pie chart:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
plt.show()
Result:
Example:(for color and lables of pie chart)
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]
plt.pie(y, labels = mylabels, colors = mycolors)
plt.show()
Result: