4/8/22, 10:39 PM Data Visualization with Matplotlib-d
Data Visualization with matplotlib
Prepared by: [Link]
Example 1: Draw a line from the point (0,0) to (10,100)
In [3]:
import [Link] as pl
import numpy as np
x_axis= [Link]([0,10]) # create x axis using array
y_axis = [Link]([0,100])
[Link](x_axis,y_axis)
[Link]()
Plotting without line
Example 2: Draw a line with two points . First point(1,5) and second point (10,20
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 1/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [5]:
import [Link] as pl
import numpy as np
x_axis= [Link]([1,10]) # create x axis using array
y_axis = [Link]([5,20])
[Link](x_axis,y_axis,'o')
[Link]()
Plotting multiple points
Example 3: Draw a line from (1,3) (5,2),(8,9)
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 2/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [7]:
import [Link] as pl
import numpy as np
x_axis = [Link]([1,5,8])
y_axis = [Link]([3,2,9])
[Link](x_axis,y_axis)
[Link]()
Default x points
Example 3: Draw a line: y values are 5,1, 4, 8
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 3/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [8]:
import [Link] as plt
import numpy as np
yaxis = [Link]([5,1,4,8])
[Link](yaxis)
[Link]()
Use of marker size, markeredge color, markerfacecolor
Example 4: Draw a line: y values are 5,1, 4, 8
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 4/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [13]:
import [Link] as plt
import numpy as np
yaxis = [Link]([5,1,4,8])
[Link](yaxis,'p:b',mec='r',mfc='g',ms=20)
[Link]()
Use of line style, line color, linewidth
Example 5: Draw a line: y values are 5,1, 4, 8
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 5/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [17]:
import [Link] as plt
import numpy as np
yaxis = [Link]([5,1,4,8])
[Link](yaxis,ls='dashdot', c='hotpink', lw='5.5',marker='H',markeredgecolor='g')
[Link]()
Plotting Multiple Lines
Example 6: Draw a line: y values are 5,1, 4, 8 ; Draw another line with values 2,9,7,4,5
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 6/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [29]:
import [Link] as plt
import numpy as np
yaxis = [Link]([5,1,4,8]) #dimension of yaxis and yaxis1 must be same
yaxis1 = [Link]( [2,9,7,4])
[Link](yaxis)
[Link](yaxis1)
[Link]()
Example 7: Draw a line: y values are 5,1, 4, 8 ; Draw another line with values 2,9,7,4,5
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 7/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [28]:
import [Link] as plt
import numpy as np
x1 = [Link]([2,3,7,4])
x2= [Link]([2,3,7,4])
yaxis = [Link]([5,1,4,8]) #dimension of yaxis and yaxis1 must be same
yaxis1 = [Link]( [2,9,7,4])
[Link](x1,yaxis,x2,yaxis1,marker='s')
[Link]()
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 8/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [33]:
import [Link] as plt
import numpy as np
year = [Link]([1992,1993,1994,1995,1996])
Loan = [Link]([28,33,55,55,80])
[Link](year,Loan,marker="s",ms=5,mec="g", mfc='r',linestyle="dashed",c='orange')
font1 = {'family':'serif','color':'blue','fontsize':15}
[Link]("Year",fontdict=font1)
[Link]("Loan",fontdict=font1)
[Link]("Loan Disbursed",loc='left',fontdict=font1)
Out[33]:
Text(0.0, 1.0, 'Loan Disbursed')
Grid
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 9/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [3]:
import [Link] as plt
import numpy as np
a = [Link]([20,30,40,50])
b= [Link]([60.1, 45,34,67])
[Link](a,b)
[Link](axis='x',color = 'orange',linestyle='dashdot', linewidth=3.5)
[Link]()
subplot
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 10/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [5]:
import [Link] as plt
import numpy as np
a = [Link]([20,30,40,50])
b= [Link]([60.1, 45,34,67])
x1 = [Link]([2,3,7,4])
yaxis = [Link]([5,1,4,8]) #dimension of yaxis and yaxis1 must be same
[Link](1,2,1)
[Link](a,b,marker='s')
[Link](1,2,2)
[Link](x1,yaxis,marker='s')
[Link]()
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 11/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [17]:
import [Link] as plt
import pandas as pd
sales = [Link]({'Days':['Monday','Tuesday','Wednesday','Thursday','Friday','Satur
day','Sunday'],
'Sales':[12,16,8,10,14,8,18],
'profit':[100,50,200,300,40,50,10]}
)
[Link](sales["Days"],sales["Sales"],markersize=5,marker='s')
[Link]('Days')
[Link]('Sales')
[Link]('Sales')
[Link]()
Scatter plot
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 12/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [32]:
import [Link] as plt
import pandas as pd
sales = [Link]({'Days':['Monday','Tuesday','Wednesday','Thursday','Friday','Satur
day','Sunday'],
'Week1':[12,16,8,10,14,8,18],
'Week2':[10,8,14,9,8,20,22]}
)
colors = [Link](['red','green','magenta','yellow','orange','blue','cyan'])
[Link](sales['Days'],sales['Week1'],c=colors,alpha=0.5)
[Link](sales['Days'],sales['Week2'])
[Link]('Days')
[Link]('Sales')
[Link]('Sales Data')
[Link]()
Out[32]:
[]
Bar plot
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 13/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [43]:
import [Link] as plt
import pandas as pd
sales = [Link]({'Days':['Monday','Tuesday','Wednesday','Thursday','Friday','Satur
day','Sunday'],
'Week1':[12,16,8,10,14,8,18],
'Week2':[10,8,14,9,8,20,22]}
)
r = [Link](7)
[Link](r-0.2,sales['Week1'],color='red',width=0.4)
[Link](r+0.2,sales['Week2'],color='blue',width=0.4)
[Link](r,sales['Days'])
[Link]()
Histogram
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 14/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [1]:
import [Link] as plt
import pandas as pd
l1 = [250,370,470,420,520,560,225,900,430,330,315]
l2 = [12,16,8,9,10,10,14,8,18,15,13]
bulb = [Link]({'Life Time':l1,'number of bulbs':l2}
)
[Link](bulb['Life Time'],bins=[200,300,400,500,600,700,800,900,1000])
[Link]("Result")
[Link]('Life Time')
[Link]('number of bulbs')
[Link]()
Pie Chart
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 15/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [64]:
import [Link] as plt
import pandas as pd
data1 = [Link]({'Items':['Biscuit','Wheat Bread','Fruit cake','choco cake','Honey
cake'],
'Sales in Rupees':[40,20,12,9,13]})
[Link](data1['Sales in Rupees'],labels=data1['Items'],explode=[0.3,0,0,0,0], colors=[
'red','green','orange','yellow','cyan'],startangle=90, shadow=True)
[Link](loc='lower right')
[Link]()
Box Plot
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 16/17
4/8/22, 10:39 PM Data Visualization with Matplotlib-d
In [2]:
import [Link] as plt
import pandas as pd
sales = [Link]({'Days':['Monday','Tuesday','Wednesday','Thursday','Friday','Satur
day','Sunday'],
'Week1':[12,16,8,10,14,8,18],
'Week2':[10,8,14,9,8,20,22]}
)
[Link]([sales['Week1'],sales['Week2']])
[Link]("Groups")
[Link]("Summary")
[Link]()
In [ ]:
localhost:8889/nbconvert/html/Advanced Python/Module 3/Data Visualization with [Link]?download=false 17/17