You are on page 1of 24

Matplotlib

module
import matplotlib

print(matplotlib.__version__)

3.5.2

Matplotlib submodule pyplot


import matplotlib.pyplot as plt

Example to understand matplotlib

# Draw a line along x and y graph

import matplotlib.pyplot as plt

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.

plotting without line


import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0,6])
ypoints = np.array([0,250])
graph_without_line = plt.plot(xpoints,ypoints,'o')
Mulitple points
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)
multiple_line_graph

[<matplotlib.lines.Line2D at 0x189a13bc100>]

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,'o')
multiple_line_graph

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

format string(changing the shape and color of the line)


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, 'o:y')
multiple_line_graph

[<matplotlib.lines.Line2D at 0x189a29eceb0>]

# in the above example (o:r)


# Here o represent the pattern of the line while r represent the color of the line.

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.

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

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

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' )
multiple_line_graph

[<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.plot(x, y, color= 'yellow')

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

Text(0.5, 1.0, 'My first graph')

Adding font in the title


import numpy as np
import matplotlib.pyplot as plt

font =font1 = {'family':'serif','color':'Red','size':20}


xpoint = np.array([5,6,7,9])
ypoint = np.array([8,4,2,8])
plt.plot(xpoint,ypoint)
plt.xlabel('x-axis', fontdict=font)
plt.ylabel('y-axis' ,fontdict=font)
plt.title("My new table", fontdict=font)

Text(0.5, 1.0, 'My new table')

Position the table


import numpy as np
import matplotlib.pyplot as plt

font =font1 = {'family':'serif','color':'Red','size':20}


xpoint = np.array([5,6,7,9])
ypoint = np.array([8,4,2,8])
plt.plot(xpoint,ypoint)
plt.xlabel('x-axis', fontdict=font)
plt.ylabel('y-axis' ,fontdict=font)
plt.title("My new table", fontdict=font, loc='left')

Text(0.0, 1.0, 'My new table')

Adding grid to the table


import numpy as np
import matplotlib.pyplot as plt

font =font1 = {'family':'serif','color':'Red','size':20}


xpoint = np.array([5,6,7,9])
ypoint = np.array([8,4,2,8])
plt.plot(xpoint,ypoint)
plt.xlabel('x-axis', fontdict=font)
plt.ylabel('y-axis' ,fontdict=font)
plt.title("My new table", fontdict=font)
plt.grid()

import numpy as np
import matplotlib.pyplot as plt

font =font1 = {'family':'serif','color':'Red','size':20}


xpoint = np.array([5,6,7,9])
ypoint = np.array([8,4,2,8])
plt.plot(xpoint,ypoint)
plt.xlabel('x-axis', fontdict=font)
plt.ylabel('y-axis' ,fontdict=font)
plt.title("My new table", fontdict=font)
plt.grid(axis='y')

import numpy as np
import matplotlib.pyplot as plt

font =font1 = {'family':'serif','color':'Red','size':20}


xpoint = np.array([5,6,7,9])
ypoint = np.array([8,4,2,8])
plt.plot(xpoint,ypoint)
plt.xlabel('x-axis', fontdict=font)
plt.ylabel('y-axis' ,fontdict=font)
plt.title("My new table", fontdict=font)
plt.grid(axis='x')

# adding the grid property

import numpy as np
import matplotlib.pyplot as plt

font =font1 = {'family':'serif','color':'Red','size':20}


xpoint = np.array([5,6,7,9])
ypoint = np.array([8,4,2,8])
plt.plot(xpoint,ypoint)
plt.xlabel('x-axis', fontdict=font)
plt.ylabel('y-axis' ,fontdict=font)
plt.title("My new table", fontdict=font)
plt.grid(color='red', linestyle=':')

Adding multiple plots(adding supertitle)


import numpy as np
import matplotlib.pyplot as plt
# plot one
x_cordinates = np.array([6,5,3,9])
y_coordine = np.array([8,5,2,9])
plt.subplot(1,2,1)

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

Text(0.5, 0.98, 'Total ')


Matplotlib scatter
import numpy as np
import matplotlib.pyplot as plt
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)

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

plt.colorbar()

plt.show()

size of scatter points


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

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

plt.show()

import matplotlib.pyplot as plt


import numpy as np
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()

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)

<BarContainer object of 3 artists>

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)

<BarContainer object of 3 artists>


bar color
import matplotlib.pyplot as plt
import numpy as np

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


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

plt.bar(x, y, color = "hotpink")


plt.show()

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)

<BarContainer object of 4 artists>


Matplot Histogram
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(185,8,222)
print(x)
plt.hist(x)
[171.54904577 187.16822238 179.63922001 169.87356501 195.05537655
189.53429768 187.31547631 180.21323584 183.60781033 173.05629888
187.3622987 177.20253636 181.87186841 193.91368438 184.93600223
194.04079381 189.15457067 188.00616129 187.07261973 172.20987569
188.39552642 187.54258042 185.02425855 180.72627019 188.3155055
174.32656007 165.27479184 197.48203891 164.90662551 184.5764237
180.72609381 174.82042695 186.81795789 197.21078273 182.27097282
183.38571487 184.70536905 173.35779719 192.15605114 173.91059515
177.69678078 169.27083406 188.76743173 190.80422546 187.5542001
194.94032837 204.80168354 185.75625514 187.23338384 179.53025547
186.89213543 183.31934051 189.95236683 172.3960667 191.5706827
178.42654998 195.48999608 182.34905997 173.78415986 181.89988555
171.28789863 173.24891799 179.25944282 177.2417677 185.06424835
192.47532283 184.10891886 186.76769256 181.57719991 166.38712311
167.85444242 192.03184758 176.48980058 192.1104602 178.12275589
185.81498205 174.43416184 185.15025032 191.08350073 175.83306036
186.68914584 184.52729533 187.50973146 187.66520359 180.28358647
189.80422867 185.60986711 192.91337716 174.47575609 180.46004473
168.39339424 188.21155885 186.15914677 181.51758437 176.9998965
181.94275759 181.17686468 188.63801389 183.21271385 182.14553975
183.98658548 178.2910235 180.9100815 194.73260368 180.90169423
186.00989405 202.10672838 178.06135745 195.89104932 181.84707773
182.26293821 184.13283587 177.2903887 169.11433269 182.52738294
188.06719486 191.78446539 195.45516944 194.54732682 176.21474076
177.69240009 188.96864173 181.85490581 177.05610463 177.45536047
192.89275887 192.22250194 186.2999929 170.57521172 196.92751763
181.62646068 184.73252824 182.04338543 173.88736209 191.0367734
185.76984675 191.21757174 181.52081949 186.0941219 192.12738727
181.50886647 196.71262005 188.40478285 166.11709217 175.92225185
187.66956163 190.10149416 184.37662182 186.11162194 176.99703077
193.48719387 178.84630768 188.13124444 178.21818071 179.15690585
188.4226547 197.79568112 187.68381781 185.34322132 195.36530161
178.76894072 179.00863339 176.59312094 176.24818467 190.20652771
183.88054672 189.09967338 180.29277898 188.48262502 179.3900057
177.48885713 186.10691238 185.69491426 196.34376224 196.0882792
192.722341 194.40465744 185.98532676 187.27891235 171.67108323
184.39177091 186.28701579 184.78960654 187.50134646 185.36948311
199.60643226 182.03992311 190.40105101 183.93831874 175.34659146
193.66333407 197.94056117 179.77074346 186.23386877 190.13170509
192.39358709 174.39130486 190.62771477 180.22379439 193.77056796
191.98049676 169.86551073 191.26446741 182.96945929 196.07075329
189.04074709 175.85750468 179.96645905 172.81780851 196.98337371
199.53455972 187.86211702 185.17339994 178.48181093 180.55193845
193.7068883 175.16051167 171.25260989 188.86234037 183.44818963
166.99345737 176.9615688 ]
(array([ 7., 12., 20., 35., 38., 50., 29., 21., 8., 2.]),
array([164.90662551, 168.89613131, 172.88563711, 176.87514292,
180.86464872, 184.85415452, 188.84366033, 192.83316613,
196.82267193, 200.81217773, 204.80168354]),
<BarContainer object of 10 artists>)

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

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


([<matplotlib.patches.Wedge at 0x189aaf543a0>,
<matplotlib.patches.Wedge at 0x189aaf54880>,
<matplotlib.patches.Wedge at 0x189aaf54d60>,
<matplotlib.patches.Wedge at 0x189aaf5f280>],
[Text(-0.9714858494905253, 0.5159605064728041, 'Apples'),
Text(-0.5364644163246997, -0.9603155366947884, 'Mango'),
Text(0.6727208239211031, -0.8703141347024719, 'Banana'),
Text(0.8558392860340517, 0.6910420511667326, 'grapes')])

Explode the pie chart


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)

([<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')])

shadow in pie chart


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, shadow=True)

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

# we can add header in the legend as below

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

You might also like