0% found this document useful (0 votes)
1 views

matplotlib

The document provides a comprehensive guide on using Matplotlib for data visualization in Python, covering various plotting techniques such as line plots, scatter plots, and subplots. It includes examples of customizing markers, colors, line styles, and adding titles and labels to plots. Additionally, it demonstrates how to create multiple subplots and use color maps for scatter plots.

Uploaded by

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

matplotlib

The document provides a comprehensive guide on using Matplotlib for data visualization in Python, covering various plotting techniques such as line plots, scatter plots, and subplots. It includes examples of customizing markers, colors, line styles, and adding titles and labels to plots. Additionally, it demonstrates how to create multiple subplots and use color maps for scatter plots.

Uploaded by

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

Matplotlib

In [1]: #w3schools

In [2]: #pyplot:sub plot of matplotlib


import matplotlib.pyplot as plt
import numpy as np

In [3]: # draw a line diagram from position (0,0) to (6,250)


x=np.array([0,6])
y=np.array([0,250])
plt.plot(x,y)
plt.show()

In [4]: # draw a line diagram from position (1,3) to (8,10)


x=np.array([1,8])
y=np.array([3,10])
plt.plot(x,y)
plt.show()
In [5]: # no line only points
x=np.array([1,8])
y=np.array([3,10])
plt.plot(x,y,'o')
plt.show()

In [6]: #(1,3) to (2,8) to (6,1) then finaly (8,10)


x=np.array([1,2,6,8])
y=np.array([3,8,1,10])
plt.plot(x,y,'o')
plt.show()
In [7]: x=np.array([1,2,6,8])
y=np.array([3,8,1,10])
plt.plot(x,y)
plt.show()

In [8]: #if x is not given then it is taken by default as (1,2,3,4,5,....)


y=np.array([3,8,1,10,5,7])
plt.plot(y)
plt.show()
In [9]: #Markers

In [10]: y=np.array([3,8,1,10])
plt.plot(y,'o')
plt.show()

In [11]: y=np.array([3,8,1,10])
plt.plot(y,marker='o')
plt.show()
In [12]: #o=circle
#*=star
#.=point
#,=plane
#x=x
#X=BOLD /FILLED x
#+=+
#capital P=filled +
#d=thin diamond
#D=filled diamond
#p=pentagon
#H= hexagon
#h=hexagon
#v or 2=triangle down
#^=trianle up
#>=triangle right
#<=triangle left
#|=|
#_=_

In [13]: y=np.array([3,8,1,10])
plt.plot(y,marker='*')
plt.show()
In [14]: y=np.array([3,8,1,10])
plt.plot(y,marker='.')
plt.show()

In [15]: y=np.array([3,8,1,10])
plt.plot(y,marker='+')
plt.show()
In [16]: y=np.array([3,8,1,10])
plt.plot(y,marker='h')
plt.show()

In [17]: y=np.array([3,8,1,10])
plt.plot(y,marker='P')
plt.show()
In [18]: #colours
#r=red
#g=green
#b=blue
#c=cyan
#m=magenta
#y=yellow
#k=black
#w=white

In [19]: #solid line='-' hyphon


#dashed line='--'
#dotted line=':'
#dashed/dotted line='-.'

In [20]: y=np.array([3,8,1,10])
plt.plot(y,'*:y')
plt.show()
In [21]: y=np.array([3,8,1,10])
plt.plot(y,'*--m')
plt.show()

In [22]: y=np.array([3,8,1,10])
plt.plot(y,'+:r')
plt.show()
In [23]: y=np.array([3,8,1,10])
plt.plot(y,'+-.g')
plt.show()

In [24]: #marker size(ms=value)


#marker edge color(mec=)
#marker face color(mfc=)
y=np.array([3,8,1,10])
plt.plot(y,marker='h',ms=20,mec='r',mfc='g')
plt.show()
In [25]: y=np.array([3,8,1,10])
plt.plot(y,marker='*',ms=20,mec='r',mfc='y')
plt.show()

In [26]: y=np.array([3,8,1,10])
plt.plot(y,marker='h',ms=20,mec='r',mfc='#8ee71e')
plt.show()
In [27]: #linestyle or ls
y=np.array([3,8,1,10])
plt.plot(y,linestyle='dotted')
plt.show()

In [28]: y=np.array([3,8,1,10])
plt.plot(y,linestyle='-.')
plt.show()
In [29]: y=np.array([3,8,1,10])
plt.plot(y,ls='--')
plt.show()

In [30]: y=np.array([3,8,1,10])
plt.plot(y,ls='--',color='m')
plt.show()
In [31]: y=np.array([3,8,1,10])
plt.plot(y,ls='--',c='hotpink')#c for color
plt.show()

In [32]: #linewidth(lw)
y=np.array([3,8,1,10])
plt.plot(y,ls='--',c='g',lw=10)
plt.show()
In [33]: y=np.array([3,8,1,10])
plt.plot(y,ls=':',c='r',linewidth=5)
plt.show()

In [34]: #multiplelines
#color by default
y1=np.array([3,8,1,10])
y2=np.array([6,2,7,11])
plt.plot(y1)
plt.plot(y2)
plt.show()
In [35]: y1=np.array([3,8,1,10])
y2=np.array([6,2,7,11])
plt.plot(y1,y2)
plt.show()

In [36]: x1=np.array([0,1,2,3])
x2=np.array([3,8,1,10])
y1=np.array([3,8,1,10])
y2=np.array([6,2,7,11])
plt.plot(x1,y1,x2,y2)
plt.show()
In [37]: #labelling
x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
plt.plot(x,y)
plt.show()

In [38]: x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
plt.plot(x,y)
plt.title('Sports watch Data ')
plt.xlabel('Average pulse')
plt.ylabel('calorie')
plt.show()

In [39]: x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
plt.plot(x,y)
plt.title('Sports watch Data',fontdict=font1,loc='right')
plt.xlabel('Average pulse',fontdict=font2)
plt.ylabel('calorie',fontdict=font2)
plt.show()
In [40]: x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
plt.plot(x,y)
plt.title('Sports watch Data',fontdict=font1)
plt.xlabel('Average pulse',fontdict=font2)
plt.ylabel('calorie',fontdict=font2)
plt.grid()
plt.show()
In [41]: x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
plt.plot(x,y)
plt.title('Sports watch Data',fontdict=font1)
plt.xlabel('Average pulse',fontdict=font2)
plt.ylabel('calorie',fontdict=font2)
plt.grid(axis='x')
plt.show()
In [42]: x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
plt.plot(x,y)
plt.title('Sports watch Data',fontdict=font1)
plt.xlabel('Average pulse',fontdict=font2)
plt.ylabel('calorie',fontdict=font2)
plt.grid(axis='y')
plt.show()
In [43]: x=np.array([80,85,90,95,100,105,110,115,120,125])
y=np.array([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
plt.plot(x,y)
plt.title('Sports watch Data',fontdict=font1)
plt.xlabel('Average pulse',fontdict=font2)
plt.ylabel('calorie',fontdict=font2)
plt.grid(color='g',ls=':',lw=1)
plt.show()
13/09/2023

In [44]: #subplots:
#plot 1
x=np.array([0,1,2,3])
y=np.array([3,8,1,10])
plt.subplot(1,2,1)#row,clmn,ethramathe plot
plt.plot(x,y)

#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.show()
In [45]: #subplots:
#plot 1
x=np.array([0,1,2,3])
y=np.array([3,8,1,10])
plt.subplot(1,2,1)#row,clmn,ethramathe plot
plt.plot(x,y)
plt.title("sales")

#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('Income')

plt.suptitle("SHOP REPORT")

plt.show()
In [46]: #6plots
#subplots:
#plot 1
x=np.array([0,1,2,3])
y=np.array([3,8,1,10])
plt.subplot(2,3,1)#row,clmn,ethramathe plot
plt.plot(x,y)

#plot 2
x=np.array([0,1,2,3])
y=np.array([10,20,30,40])
plt.subplot(2,3,2)
plt.plot(x,y)

#plot3
x=np.array([0,1,2,3])
y=np.array([15,25,35,45])
plt.subplot(2,3,3)
plt.plot(x,y)

#plot4
x=np.array([0,1,2,3])
y=np.array([50,60,70,80])
plt.subplot(2,3,4)
plt.plot(x,y)

#plot5

x=np.array([0,1,2,3])
y=np.array([1,2,3,4])
plt.subplot(2,3,5)
plt.plot(x,y)

#plot6
x=np.array([0,1,2,3])
y=np.array([11,22,33,44])
plt.subplot(2,3,6)
plt.plot(x,y)

plt.show()

In [47]: #scatterplot
x=np.array([5,7,8,7,2,17,2,9,4,11,22,9,6])#age
y=np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])#speed
plt.scatter(x,y)
plt.show()
In [48]: x=np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=np.array([100,105,84,105,90,90,99,94,100,79,8,85,100,78,123])
plt.scatter(x,y)
plt.show()

In [49]: x=np.array([5,7,8,7,2,17,2,9,4,11,22,9,6])#age
y=np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])#speed
plt.scatter(x,y)

x=np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=np.array([100,105,84,105,90,90,99,94,100,79,8,85,100,78,123])
plt.scatter(x,y)

plt.show()
15/09/2023

In [50]: #changing default color


x=np.array([5,7,8,7,2,17,2,9,4,11,22,9,6])#age
y=np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])#speed
plt.scatter(x,y,color='hotpink')

x=np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=np.array([100,105,84,105,90,90,99,94,100,79,8,85,100,78,123])
plt.scatter(x,y,color='#88c999')

plt.show()
In [51]: x=np.array([5,7,8,7,2])
y=np.array([99,86,87,88,111])
colors=(['red','hotpink','green','yellow','blue'])
plt.scatter(x,y,c=colors)
plt.show()

In [52]: #cmap=viridis
x=np.array([5,7,8,7,2])
y=np.array([99,86,87,88,111])
colors=np.array([0,10,20,30,40])
plt.scatter(x,y,c=colors,cmap='viridis')
plt.show()
In [53]: 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()

In [54]: 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='Accent')

plt.colorbar()

plt.show()

In [55]: 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='Accent_r')

plt.colorbar()

plt.show()
In [56]: 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='BrBG')

plt.colorbar()

plt.show()

In [57]: #colormap lists in w3schools


In [58]: #sizes
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])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])

plt.scatter(x, y, s=sizes)

plt.show()

In [59]: #alpha:set own marker size with transparency


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])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])

plt.scatter(x, y, s=sizes, alpha=0.5)

plt.show()
In [60]: #using random
x = np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100, size=(100))

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

plt.colorbar()

plt.show()
In [61]: #BARPLOT

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


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

plt.bar(x,y)
plt.show()

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


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

plt.barh(x,y)
plt.show()
In [64]: x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)

<BarContainer object of 2 artists>


Out[64]:

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


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

plt.bar(x,y,color='green')
plt.show()
In [66]: x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, width = 0.1)


plt.show()

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


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

plt.barh(x, y, height = 0.1)


plt.show()
In [68]: #Histogram
#showing frequency distributions.
# showing the number of observations within each given interval.

In [69]: x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()

In [70]: x = np.random.normal(170, 10, 250)


plt.hist(x,color='green')
plt.show()

In [71]: #Pie Charts

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

plt.pie(y)
plt.show()

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


x= ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels =x)
plt.show()

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


x= ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = x, startangle = 90)


plt.show()

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


lab = ["Apples", "Bananas", "Cherries", "Dates"]
expl = [0.2, 0, 0, 0]

plt.pie(y, labels= lab, explode = expl)


plt.show()
In [76]: y = np.array([35, 25, 25, 15])
lab = ["Apples", "Bananas", "Cherries", "Dates"]
expl = [0.2, 0, 0, 0]

plt.pie(y, labels= lab, explode = expl,shadow=True)


plt.show()

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


x= ["Apples", "Bananas", "Cherries", "Dates"]
expl = [0, 0.2, 0, 0]

plt.pie(y, labels = x, explode = expl, shadow = True)


plt.show()
In [78]: y = np.array([35, 25, 25, 15])
mylabels= ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]#colour is given

plt.pie(y, labels = mylabels, colors = mycolors)


plt.show()

In [79]: #hexadecimal color values


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

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


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.legend()
plt.show()

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


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.legend(title = "Four Fruits:")
plt.show()
In [82]: #write a python pgm to display a barchart of popularity of programing languages
#sample data
#x=languages=python java php javascript c# c++
#y=popularity 22.2,17.6,8.8,8,7.7,6.7
#each bar with diff colour

In [84]: #pie chart


#highest value exploded&shadow

In [85]: # a py pgm scatterplot with empty circles taking a random distribution in x&y and plotte

In [86]: #4.py pgm scatterplot comparing 2 suject marks of mathematics and science
#maths=88,92,80,89,100,80,60,100,80,34
#science=35,76,79,48,100,88,32,45,20,30
#mark range(10-100)

You might also like