Matplotlib
In [1]: #w3schools
In [2]: #pyplot:sub plot of matplotlib
import [Link] as plt
import numpy as np
In [3]: # draw a line diagram from position (0,0) to (6,250)
x=[Link]([0,6])
y=[Link]([0,250])
[Link](x,y)
[Link]()
In [4]: # draw a line diagram from position (1,3) to (8,10)
x=[Link]([1,8])
y=[Link]([3,10])
[Link](x,y)
[Link]()
In [5]: # no line only points
x=[Link]([1,8])
y=[Link]([3,10])
[Link](x,y,'o')
[Link]()
In [6]: #(1,3) to (2,8) to (6,1) then finaly (8,10)
x=[Link]([1,2,6,8])
y=[Link]([3,8,1,10])
[Link](x,y,'o')
[Link]()
In [7]: x=[Link]([1,2,6,8])
y=[Link]([3,8,1,10])
[Link](x,y)
[Link]()
In [8]: #if x is not given then it is taken by default as (1,2,3,4,5,....)
y=[Link]([3,8,1,10,5,7])
[Link](y)
[Link]()
In [9]: #Markers
In [10]: y=[Link]([3,8,1,10])
[Link](y,'o')
[Link]()
In [11]: y=[Link]([3,8,1,10])
[Link](y,marker='o')
[Link]()
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=[Link]([3,8,1,10])
[Link](y,marker='*')
[Link]()
In [14]: y=[Link]([3,8,1,10])
[Link](y,marker='.')
[Link]()
In [15]: y=[Link]([3,8,1,10])
[Link](y,marker='+')
[Link]()
In [16]: y=[Link]([3,8,1,10])
[Link](y,marker='h')
[Link]()
In [17]: y=[Link]([3,8,1,10])
[Link](y,marker='P')
[Link]()
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=[Link]([3,8,1,10])
[Link](y,'*:y')
[Link]()
In [21]: y=[Link]([3,8,1,10])
[Link](y,'*--m')
[Link]()
In [22]: y=[Link]([3,8,1,10])
[Link](y,'+:r')
[Link]()
In [23]: y=[Link]([3,8,1,10])
[Link](y,'+-.g')
[Link]()
In [24]: #marker size(ms=value)
#marker edge color(mec=)
#marker face color(mfc=)
y=[Link]([3,8,1,10])
[Link](y,marker='h',ms=20,mec='r',mfc='g')
[Link]()
In [25]: y=[Link]([3,8,1,10])
[Link](y,marker='*',ms=20,mec='r',mfc='y')
[Link]()
In [26]: y=[Link]([3,8,1,10])
[Link](y,marker='h',ms=20,mec='r',mfc='#8ee71e')
[Link]()
In [27]: #linestyle or ls
y=[Link]([3,8,1,10])
[Link](y,linestyle='dotted')
[Link]()
In [28]: y=[Link]([3,8,1,10])
[Link](y,linestyle='-.')
[Link]()
In [29]: y=[Link]([3,8,1,10])
[Link](y,ls='--')
[Link]()
In [30]: y=[Link]([3,8,1,10])
[Link](y,ls='--',color='m')
[Link]()
In [31]: y=[Link]([3,8,1,10])
[Link](y,ls='--',c='hotpink')#c for color
[Link]()
In [32]: #linewidth(lw)
y=[Link]([3,8,1,10])
[Link](y,ls='--',c='g',lw=10)
[Link]()
In [33]: y=[Link]([3,8,1,10])
[Link](y,ls=':',c='r',linewidth=5)
[Link]()
In [34]: #multiplelines
#color by default
y1=[Link]([3,8,1,10])
y2=[Link]([6,2,7,11])
[Link](y1)
[Link](y2)
[Link]()
In [35]: y1=[Link]([3,8,1,10])
y2=[Link]([6,2,7,11])
[Link](y1,y2)
[Link]()
In [36]: x1=[Link]([0,1,2,3])
x2=[Link]([3,8,1,10])
y1=[Link]([3,8,1,10])
y2=[Link]([6,2,7,11])
[Link](x1,y1,x2,y2)
[Link]()
In [37]: #labelling
x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
[Link](x,y)
[Link]()
In [38]: x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
[Link](x,y)
[Link]('Sports watch Data ')
[Link]('Average pulse')
[Link]('calorie')
[Link]()
In [39]: x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
[Link](x,y)
[Link]('Sports watch Data',fontdict=font1,loc='right')
[Link]('Average pulse',fontdict=font2)
[Link]('calorie',fontdict=font2)
[Link]()
In [40]: x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
[Link](x,y)
[Link]('Sports watch Data',fontdict=font1)
[Link]('Average pulse',fontdict=font2)
[Link]('calorie',fontdict=font2)
[Link]()
[Link]()
In [41]: x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
[Link](x,y)
[Link]('Sports watch Data',fontdict=font1)
[Link]('Average pulse',fontdict=font2)
[Link]('calorie',fontdict=font2)
[Link](axis='x')
[Link]()
In [42]: x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
[Link](x,y)
[Link]('Sports watch Data',fontdict=font1)
[Link]('Average pulse',fontdict=font2)
[Link]('calorie',fontdict=font2)
[Link](axis='y')
[Link]()
In [43]: x=[Link]([80,85,90,95,100,105,110,115,120,125])
y=[Link]([240,250,260,270,280,290,300,310,320,330])
font1={'family':'serif','color':'green','size':20}
font2={'family':'serif','color':'red','size':15}
[Link](x,y)
[Link]('Sports watch Data',fontdict=font1)
[Link]('Average pulse',fontdict=font2)
[Link]('calorie',fontdict=font2)
[Link](color='g',ls=':',lw=1)
[Link]()
13/09/2023
In [44]: #subplots:
#plot 1
x=[Link]([0,1,2,3])
y=[Link]([3,8,1,10])
[Link](1,2,1)#row,clmn,ethramathe plot
[Link](x,y)
#plot 2
x=[Link]([0,1,2,3])
y=[Link]([10,20,30,40])
[Link](1,2,2)
[Link](x,y)
[Link]()
In [45]: #subplots:
#plot 1
x=[Link]([0,1,2,3])
y=[Link]([3,8,1,10])
[Link](1,2,1)#row,clmn,ethramathe plot
[Link](x,y)
[Link]("sales")
#plot 2
x=[Link]([0,1,2,3])
y=[Link]([10,20,30,40])
[Link](1,2,2)
[Link](x,y)
[Link]('Income')
[Link]("SHOP REPORT")
[Link]()
In [46]: #6plots
#subplots:
#plot 1
x=[Link]([0,1,2,3])
y=[Link]([3,8,1,10])
[Link](2,3,1)#row,clmn,ethramathe plot
[Link](x,y)
#plot 2
x=[Link]([0,1,2,3])
y=[Link]([10,20,30,40])
[Link](2,3,2)
[Link](x,y)
#plot3
x=[Link]([0,1,2,3])
y=[Link]([15,25,35,45])
[Link](2,3,3)
[Link](x,y)
#plot4
x=[Link]([0,1,2,3])
y=[Link]([50,60,70,80])
[Link](2,3,4)
[Link](x,y)
#plot5
x=[Link]([0,1,2,3])
y=[Link]([1,2,3,4])
[Link](2,3,5)
[Link](x,y)
#plot6
x=[Link]([0,1,2,3])
y=[Link]([11,22,33,44])
[Link](2,3,6)
[Link](x,y)
[Link]()
In [47]: #scatterplot
x=[Link]([5,7,8,7,2,17,2,9,4,11,22,9,6])#age
y=[Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])#speed
[Link](x,y)
[Link]()
In [48]: x=[Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=[Link]([100,105,84,105,90,90,99,94,100,79,8,85,100,78,123])
[Link](x,y)
[Link]()
In [49]: x=[Link]([5,7,8,7,2,17,2,9,4,11,22,9,6])#age
y=[Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])#speed
[Link](x,y)
x=[Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=[Link]([100,105,84,105,90,90,99,94,100,79,8,85,100,78,123])
[Link](x,y)
[Link]()
15/09/2023
In [50]: #changing default color
x=[Link]([5,7,8,7,2,17,2,9,4,11,22,9,6])#age
y=[Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])#speed
[Link](x,y,color='hotpink')
x=[Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=[Link]([100,105,84,105,90,90,99,94,100,79,8,85,100,78,123])
[Link](x,y,color='#88c999')
[Link]()
In [51]: x=[Link]([5,7,8,7,2])
y=[Link]([99,86,87,88,111])
colors=(['red','hotpink','green','yellow','blue'])
[Link](x,y,c=colors)
[Link]()
In [52]: #cmap=viridis
x=[Link]([5,7,8,7,2])
y=[Link]([99,86,87,88,111])
colors=[Link]([0,10,20,30,40])
[Link](x,y,c=colors,cmap='viridis')
[Link]()
In [53]: x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = [Link]([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
[Link](x, y, c=colors, cmap='viridis')
[Link]()
[Link]()
In [54]: x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = [Link]([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
[Link](x, y, c=colors, cmap='Accent')
[Link]()
[Link]()
In [55]: x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = [Link]([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
[Link](x, y, c=colors, cmap='Accent_r')
[Link]()
[Link]()
In [56]: x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = [Link]([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
[Link](x, y, c=colors, cmap='BrBG')
[Link]()
[Link]()
In [57]: #colormap lists in w3schools
In [58]: #sizes
x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes = [Link]([20,50,100,200,500,1000,60,90,10,300,600,800,75])
[Link](x, y, s=sizes)
[Link]()
In [59]: #alpha:set own marker size with transparency
x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes = [Link]([20,50,100,200,500,1000,60,90,10,300,600,800,75])
[Link](x, y, s=sizes, alpha=0.5)
[Link]()
In [60]: #using random
x = [Link](100, size=(100))
y = [Link](100, size=(100))
colors = [Link](100, size=(100))
sizes = 10 * [Link](100, size=(100))
[Link](x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')
[Link]()
[Link]()
In [61]: #BARPLOT
In [62]: x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x,y)
[Link]()
In [63]: x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x,y)
[Link]()
In [64]: x = ["APPLES", "BANANAS"]
y = [400, 350]
[Link](x, y)
<BarContainer object of 2 artists>
Out[64]:
In [65]: x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x,y,color='green')
[Link]()
In [66]: x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x, y, width = 0.1)
[Link]()
In [67]: x = [Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])
[Link](x, y, height = 0.1)
[Link]()
In [68]: #Histogram
#showing frequency distributions.
# showing the number of observations within each given interval.
In [69]: x = [Link](170, 10, 250)
[Link](x)
[Link]()
In [70]: x = [Link](170, 10, 250)
[Link](x,color='green')
[Link]()
In [71]: #Pie Charts
In [72]: y = [Link]([35, 25, 25, 15])
[Link](y)
[Link]()
In [73]: y = [Link]([35, 25, 25, 15])
x= ["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels =x)
[Link]()
In [74]: y = [Link]([35, 25, 25, 15])
x= ["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels = x, startangle = 90)
[Link]()
In [75]: y = [Link]([35, 25, 25, 15])
lab = ["Apples", "Bananas", "Cherries", "Dates"]
expl = [0.2, 0, 0, 0]
[Link](y, labels= lab, explode = expl)
[Link]()
In [76]: y = [Link]([35, 25, 25, 15])
lab = ["Apples", "Bananas", "Cherries", "Dates"]
expl = [0.2, 0, 0, 0]
[Link](y, labels= lab, explode = expl,shadow=True)
[Link]()
In [77]: y = [Link]([35, 25, 25, 15])
x= ["Apples", "Bananas", "Cherries", "Dates"]
expl = [0, 0.2, 0, 0]
[Link](y, labels = x, explode = expl, shadow = True)
[Link]()
In [78]: y = [Link]([35, 25, 25, 15])
mylabels= ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]#colour is given
[Link](y, labels = mylabels, colors = mycolors)
[Link]()
In [79]: #hexadecimal color values
#'r' - Red
#'g' - Green
#'b' - Blue
#'c' - Cyan
#'m' - Magenta
#'y' - Yellow
#'k' - Black
#'w' - White
In [80]: y = [Link]([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels = mylabels)
[Link]()
[Link]()
In [81]: y = [Link]([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
[Link](y, labels = mylabels)
[Link](title = "Four Fruits:")
[Link]()
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]: #[Link] 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)