0% found this document useful (0 votes)
10 views20 pages

Pyt

The document provides a comprehensive guide on using Matplotlib for various types of data visualization, including line plots, bar graphs, histograms, and pie charts. It includes code snippets demonstrating how to create these visualizations, customize their appearance, and interpret the data represented. Additionally, it explains concepts such as data distribution and the use of labels and colors in pie charts.

Uploaded by

ahenas04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Pyt

The document provides a comprehensive guide on using Matplotlib for various types of data visualization, including line plots, bar graphs, histograms, and pie charts. It includes code snippets demonstrating how to create these visualizations, customize their appearance, and interpret the data represented. Additionally, it explains concepts such as data distribution and the use of labels and colors in pie charts.

Uploaded by

ahenas04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Matplotlib

import matplotlib.pyplot as plt


x = [1,2,3,4,5]
y = [50,40,70,80,20]
y2 = [80,20,20,50,60]
y3 = [70,20,60,40,60]
y4 = [80,20,20,50,60]
plt.plot(x,y,’g’,label=’Enfield’,
linewidth=5)
plt.plot(x,y2,’c’,label=’Honda’,linewidth
=5)
plt.plot(x,y3,’k’,label=’Yahama’,linewidt
h=5)
plt.plot(x,y4,’y’,label=’KTM’,linewidth=
5)
plt.title(‘bike details in line plot’)
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, 'o')


plt.show()
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1)
and finally to position (8, 10):

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, ypoints)
plt.show()
Plotting without x-points:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints, marker = 'o')


plt.show()

import matplotlib.pyplot as plt


import numpy as np

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

plt.plot(ypoints, 'o:r')
plt.show()
import numpy as np
import matplotlib.pyplot as plt

x =
np.array([80, 85, 90, 95, 100, 105, 110, 11
5, 120, 125])
y =
np.array([240, 250, 260, 270, 280, 290, 300
, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid()

plt.show()
Display only grid lines for the x-axis:

import numpy as np
import matplotlib.pyplot as plt

x =
np.array([80, 85, 90, 95, 100, 105, 110, 11
5, 120, 125])
y =
np.array([240, 250, 260, 270, 280, 290, 300
, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid(axis = 'x')

plt.show()
Bar Graph Histogram
No space between two
Equal space between
consecutive bars. They
every two consecutive
should be attached to each
bars.
other.
The x-axis should represent
The x-axis can represent
only continuous data that is
anything.
in terms of numbers.
Draw 4 bars:

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)
plt.show()

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 = "red")


plt.show()
import matplotlib.pyplot as plt
import numpy as np

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


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

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

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, width = 0.1)


plt.show()
import matplotlib.pyplot as plt
import numpy as np

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


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

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


plt.show()
• A histogram is a graph showing frequency distributions.
• It is a graph showing the number of observations within
each given interval.

You can read from the histogram that there


are approximately:
2 people from 140 to 145cm
5 people from 145 to 150cm
15 people from 151 to 156cm
31 people from 157 to 162cm
46 people from 163 to 168cm
53 people from 168 to 173cm
45 people from 173 to 178cm
28 people from 179 to 184cm
21 people from 185 to 190cm
4 people from 190 to 195cm
A Normal Data Distribution by NumPy:

import numpy as np

x = np.random.normal(170, 10, 250)

print(x)

import matplotlib.pyplot as plt


import numpy as np

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

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

plt.pie(y)
plt.show()

By default the plotting of the first wedge starts from the x-axis and
moves counterclockwise:

The size of each wedge is determined by comparing the value with all the other values, by using this formula:
The value divided by the sum of all values: x/sum(x)
Labels
Add labels to the pie chart with the label parameter.
The label parameter must be an array with one label for each wedge:

import matplotlib.pyplot as plt


import numpy as np

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


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

plt.pie(y, labels = mylabels)


plt.show()
Start Angle
As mentioned the default start angle is at the x-axis, but you can change the start angle by specifying a startangle parameter.
The startangle parameter is defined with an angle in degrees, default angle is 0:

import matplotlib.pyplot as plt


import numpy as np

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


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

plt.pie(y, labels = mylabels, startangle


= 90)
plt.show()
Pull the "Apples" wedge 0.2 from the center of the
pie:
Explode
Maybe you want one of the wedges to stand out? The explode parameter allows you to do that.
The explode parameter, if specified, and not None, must be an array with one value for each wedge.
Each value represents how far from the center each wedge is displayed:

import matplotlib.pyplot as plt


import numpy as np

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


mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels = mylabels, explode =


myexplode)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

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


mylabels =
["Apples", "Bananas", "Cherries", "Dates"]
mycolors =
["black", "hotpink", "b", "#4CAF50"]

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


mycolors)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

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


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

plt.pie(y, labels = mylabels)


plt.legend()
plt.show()

You might also like