Matplotlib Manual
Matplotlib Manual
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and
Javascript for Platform compatibility.
3.5.1
PYPLOT
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported
under the plt alias:
xp = np.array([0, 6,8])
yp = np.array([20, 50,30])
print(xp)
print(yp)
plt.plot(xp, yp)
plt.show()
[0 6 8]
[20 50 30]
plt.plot(xp,yp,'o')
plt.show()
[1 3 5 7 9]
[10 30 50 70 90]
plt.plot(yp)
plt.show()
marker argument : to emphasize each point with a specified
marker
In [25]: plt.plot(yp, marker = '*')
plt.show()
plt.plot(y1p)
plt.plot(y2p)
plt.show()
In [47]: x1p = np.array([0, 1, 2, 3])
y1p = np.array([3, 8, 1, 10])
x2p = np.array([0, 1, 2, 3])
y2p = np.array([6, 2, 7, 11])
plt.plot(cgpa,maths,'o')
plt.xlabel("CGPA")
plt.ylabel("MATHS MARKS")
plt.title("MARKS ANALYZE\n",loc='right')
plt.grid(c = 'red', ls = '--', lw = 0.5)
plt.show()
SUB-PLOTS
In [62]: #plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
#subplot(rows,column,position)
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("plot1")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("plot2")
plt.show()
In [64]: #plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 2, 4)
plt.plot(x,y)
plt.title("plot2")
plt.suptitle("Main Title")
plt.show()
SCATTER PLOTS
In [65]: x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
In [66]: x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y, color = 'RED')
In [68]: x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array(["red","green","blue","yellow","pink","black","orange","purple","
plt.scatter(x, y, c=colors)
plt.show()
In [69]: x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.colorbar()
plt.show()
BAR PLOTS
In [70]: x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
plt.show()
plt.barh(x, y)
plt.show()
plt.hist(x)
plt.show()
PIE CHARTS
In [74]: y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]