Professional Documents
Culture Documents
module
import matplotlib
print(matplotlib.__version__)
3.5.2
import numpy as np
xpoints = np.array([0,6])
ypoints = np.array ([10,250])
straight_line = plt.plot(xpoints,ypoints)
straight_line
[<matplotlib.lines.Line2D at 0x1899fb56a30>]
# Here xpoints along horizontal axis and ypoints along vertical axis.
[<matplotlib.lines.Line2D at 0x189a13bc100>]
[<matplotlib.lines.Line2D at 0x189a0553a30>]
matplot marker
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
multiple_line_graph = plt.plot(xpoints,ypoints, marker = 'd')
multiple_line_graph
[<matplotlib.lines.Line2D at 0x189a19182e0>]
[<matplotlib.lines.Line2D at 0x189a29eceb0>]
Mark size
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
multiple_line_graph = plt.plot(xpoints,ypoints, marker ='*', ms= 20)
multiple_line_graph
[<matplotlib.lines.Line2D at 0x189a15baaf0>]
Mark color
# inthe example we will chnage the color of boundary and mark too.
[<matplotlib.lines.Line2D at 0x189a15eabb0>]
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
multiple_line_graph = plt.plot(xpoints,ypoints, marker="*", mfc='hotpink', ms = 20, mec='hotpink')
multiple_line_graph
[<matplotlib.lines.Line2D at 0x189a0aede80>]
line style
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
multiple_line_graph = plt.plot(xpoints,ypoints, marker="*", mec='r', ms = 20, linestyle= 'dotted')
multiple_line_graph
[<matplotlib.lines.Line2D at 0x189a3c60eb0>]
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
multiple_line_graph = plt.plot(xpoints,ypoints, marker="*", mec='r', ms = 20, ls='dashed')
multiple_line_graph
[<matplotlib.lines.Line2D at 0x189a3f69160>]
[<matplotlib.lines.Line2D at 0x189a40a44c0>]
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
multiple_line_graph = plt.plot(xpoints,ypoints, marker="*", mec='r', ms = 20, color ='yellow'
,mfc='hotpink' , linewidth=20 )
multiple_line_graph
[<matplotlib.lines.Line2D at 0x189a4104d00>]
Multiple lines
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1,2,6,8])
ypoints = np.array([3,8,1,10])
plt.plot(xpoints, color ='red')
plt.plot(ypoints)
[<matplotlib.lines.Line2D at 0x189a415d190>]
Matplotlib lables and title
import numpy as np
import matplotlib.pyplot as plt
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.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x= np.array([6,4,8,9])
y= np.array([8,6,9,3])
plt.plot(x,y, ms=25)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('My first graph')
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
plt.plot(x_cordinates,y_coordine)
plt.title('salary')
# plot two
xx_cordinates = np.array([6,5,3,9])
yy_coordine = np.array([8,3,2,9])
plt.subplot(1,2,2)
plt.plot(xx_cordinates,yy_coordine)
plt.title('income')
plt.suptitle('Total ')
<matplotlib.collections.PathCollection at 0x189a73cf370>
comparing the scatter plots
import numpy as np
import matplotlib.pyplot as plt
# for example day1 age and speed of car 13
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.xlabel('age of car')
plt.ylabel('speed of car')
plt.scatter(x, y, color ='red')
#day two, the age and speed of 15 cars:
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,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)
<matplotlib.collections.PathCollection at 0x189a539d130>
import matplotlib.pyplot as plt
import numpy as np
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","beige","brown","gray","cyan",
plt.scatter(x, y, c=colors)
plt.show()
color Map
import matplotlib.pyplot as plt
import numpy as np
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()
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()
plt.colorbar()
plt.show()
Matplotlib bars
import numpy as np
import matplotlib.pyplot as plt
x = np.array(['A',"B",'C'])
y = np.array([5,6,8])
plt.bar(x,y)
Horizontal bars
import matplotlib.pyplot as plt
import numpy as np
x = np.array(['A','G','H'])
y = np.array ([7,2,9])
plt.barh(x,y, height=0.1)
Bar width
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A","B","C","D"])
y = np.array([6,8,9,22])
plt.bar(x,y, width=0.2)
Matplotlib piechart
import matplotlib.pyplot as plt
import numpy as np
x = np.array([51,22,33,4])
plt.pie(x)
([<matplotlib.patches.Wedge at 0x189aae42a90>,
<matplotlib.patches.Wedge at 0x189aae441f0>,
<matplotlib.patches.Wedge at 0x189aae44610>,
<matplotlib.patches.Wedge at 0x189aae44b20>],
[Text(0.12539053413662962, 1.0928299108044814, ''),
Text(-1.0132360012966684, -0.42819715748278603, ''),
Text(0.42819720491578595, -1.0132359812513119, ''),
Text(1.092829924011873, -0.12539041902874393, '')])
Label on piechart
import matplotlib.pyplot as plt
import numpy as np
x = np.array([51,22,33,4])
y = ['Apples','Mango','Banana','grapes']
plt.pie(x, labels=y)
([<matplotlib.patches.Wedge at 0x189aaea53d0>,
<matplotlib.patches.Wedge at 0x189aaea5c40>,
<matplotlib.patches.Wedge at 0x189aaed7100>,
<matplotlib.patches.Wedge at 0x189aaed75e0>],
[Text(0.12539053413662962, 1.0928299108044814, 'Apples'),
Text(-1.0132360012966684, -0.42819715748278603, 'Mango'),
Text(0.42819720491578595, -1.0132359812513119, 'Banana'),
Text(1.092829924011873, -0.12539041902874393, 'grapes')])
# you also can change the angle of part in pie chart as below we will write a code.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([51,22,33,42])
y = ['Apples','Mango','Banana','grapes']
([<matplotlib.patches.Wedge at 0x189ab0151f0>,
<matplotlib.patches.Wedge at 0x189ab0156d0>,
<matplotlib.patches.Wedge at 0x189ab015bb0>,
<matplotlib.patches.Wedge at 0x189ab0210d0>],
[Text(0.8063570573449376, 0.7481900133455492, 'pakistan'),
Text(-0.8063570923702388, 0.7481899755972506, 'india'),
Text(-1.1567342014135193, -0.7886482024833557, 'Finland'),
Text(0.5500001486524352, -0.9526278583383436, 'Germany')])
([<matplotlib.patches.Wedge at 0x189ab0735b0>,
<matplotlib.patches.Wedge at 0x189ab073d00>,
<matplotlib.patches.Wedge at 0x189ab07f490>,
<matplotlib.patches.Wedge at 0x189ab07fbe0>],
[Text(0.8063570573449376, 0.7481900133455492, 'pakistan'),
Text(-0.8063570923702388, 0.7481899755972506, 'india'),
Text(-1.1567342014135193, -0.7886482024833557, 'Finland'),
Text(0.5500001486524352, -0.9526278583383436, 'Germany')])
Legend
import numpy as np
import matplotlib.pyplot as plt
x =np.array([55,66,33,77])
y = ["pakistan",'india','Finland','Germany']
coloring = ['green','blue','red','black']
exploding = [0,0,0.3,0]
plt.pie(x, labels=y , explode=exploding , colors=coloring)
plt.legend()
<matplotlib.legend.Legend at 0x189ab08d6d0>
import numpy as np
import matplotlib.pyplot as plt
x =np.array([55,66,33,77])
y = ["pakistan",'india','Finland','Germany']
coloring = ['green','blue','red','black']
exploding = [0,0,0.3,0]
plt.pie(x, labels=y , explode=exploding , colors=coloring)
plt.legend(title = 'Four countries list')
<matplotlib.legend.Legend at 0x189aab61130>
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js