Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Matplotlib Manual

Python matplotlib

Uploaded by

Vaishali Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Matplotlib Manual

Python matplotlib

Uploaded by

Vaishali Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

In [5]: import matplotlib as mp


print(mp.__version__)

3.5.1

PYPLOT
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported
under the plt alias:

In [19]: import matplotlib.pyplot as plt


import numpy as np

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]

In [20]: plt.plot(xp, yp, 'o')


plt.show()
In [21]: xp=np.arange(1,10,2)
yp=np.arange(10,100,20)
print( xp,"\n",yp)

plt.plot(xp,yp,'o')
plt.show()

[1 3 5 7 9]
[10 30 50 70 90]

By default x_cordinates values are 0,1....(len(y_cordinates)-1)

In [23]: yp = np.array([3, 8, 1, 10, 5, 7])

plt.plot(yp)
plt.show()
marker argument : to emphasize each point with a specified
marker
In [25]: plt.plot(yp, marker = '*')
plt.show()

In [28]: plt.plot(yp, marker ='D')


plt.show()

linestyle or ls arguments: to change the style of the plotted


line
In [34]: plt.plot(yp, ls = '-.')
plt.show()

color or c argument : to set the color of the line:


In [38]: plt.plot(yp, color = '#4CAD55')
plt.show()

In [41]: plt.plot(yp, color = 'r')


plt.show()
linewidth or lw argument : to change the width of the line
In [43]: plt.plot(yp, linewidth = '10.5')
plt.show()

MULTIPLE LINES SINGLE PLOT


In [46]: y1p = np.array([3, 8, 1, 10])
y2p = np.array([6, 5, 7, 11])

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(x1p, y1p, x2p, y2p)


plt.show()

LABELS, TITLE & GRIDS


In [60]: maths = np.array([80, 85, 90, 95, 100, 83,65])
cgpa = np.array([8,9,9,10,10,9,7])

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.scatter(x, y, c=colors, cmap='viridis')

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()

In [71]: x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

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"]

plt.pie(y, labels = mylabels)


plt.show()

In [79]: y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0.1, 0, 0]

plt.pie(y, labels = mylabels, explode = myexplode)


plt.legend(title = "Four Fruits:",loc='right')
plt.show()
In [ ]:

You might also like