0% found this document useful (0 votes)
18 views10 pages

Unit 5

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

Unit 5

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

Unit 5

importing matplotlib.pyplot and plotting: ( only two dimensional Plots)

What is Matplotlib?

There are thousands of libraries in Python, and Matplotlib is one of the most
powerful tools for data visualization in Python.

Matplotlib tries to make easy things easy and hard things possible. You can
generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots,
etc., with just a few lines of code.
Importing the library

If you have a later version of Python installed, you should be able to open
cmd.exe/terminal and then run:

pip install matplotlib

To get matplotlib up and running in our environment, we need to import it.


import matplotlib.pyplot as plt
The following tables explain different graphs along with functions defined for
these graphs in matplotlib library.
1)scatter plot
2)line chart
3)Histogram chart
4)Bar chart

1)Scatter plot
Scatter plots are used to observe relationship between variables and uses dots
to represent the relationship between them. The scatter() method in the
matplotlib library is used to draw a scatter plot. Scatter plots are widely used
to represent relation among variables and how change in one affects the other.
The scatter() method takes in the following parameters:
 x_axis_data- An array containing x-axis data
 y_axis_data- An array containing y-axis data
 s- marker size (can be scalar or array of size equal to size of x or y)
 c- color of sequence of colors for markers
 marker- marker style

 linewidths- width of marker border


 edgecolor- marker border color
Except x_axis_data and y_axis_data all other parameters are optional and their
default value is None

For example:
import matplotlib.pyplot as plt
x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]
y =[99, 86, 87, 88, 100, 86,
103, 87, 94, 78, 77, 85, 86]
plt.scatter(x, y, c ="blue")
# To show the plot
plt.show()
output:
For example:

import matplotlib.pyplot as plt


# dataset-1
x1 = [89, 43, 36, 36, 95, 10,
66, 34, 38, 20]
y1 = [21, 46, 3, 35, 67, 95,
53, 72, 58, 10]
# dataset2
x2 = [26, 29, 48, 64, 6, 5,
36, 66, 72, 40]
y2 = [26, 34, 90, 33, 38,
20, 56, 2, 47, 15]
plt.scatter(x1, y1, c ="pink",
linewidths = 2,
marker ="s",
edgecolor ="green",
s = 50)
plt.scatter(x2, y2, c ="yellow",
linewidths = 2,
marker ="^",
edgecolor ="red",
s = 200)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
2)Bar Chart
Bar Graph is used to make comparison between different categories or groups.
Suppose you want to show comparison between cities in terms of average
annual income.
You can style your graph using the following functions -
 plt.title( ) for specifying title of your plot.
 plt.xlabel( ) for labeling x-axis.
 plt.ylabel( ) for labeling y-axis.
 plt.bar( ) for defining color of bars.

For example:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 7, 3, 8, 4]
plt.bar(x,y)
plt.title("Simple Bar graph") # Name title of the graph
plt.xlabel('Students') # Assign the name of the x axis
plt.ylabel("Math Score") # Assign the name of the y axis
plt.bar(x, y, color='red') # Change bar color
plt.show()
output:
3)Histogram Chart
Alternatively, you may derive the bins using the following formulas:

 n = number of observations
 Range = maximum value – minimum value
 # of intervals = √n
 Width of intervals = Range / (# of intervals)
These formulas can then be used to create the frequency table followed by the
histogram.

Recall that our dataset contained the following 100 observations:

Age

1,1,2,3,3,5,7,8,9,10,
10,11,11,13,13,15,16,17,18,18,
18,19,20,21,21,23,24,24,25,25,
25,25,26,26,26,27,27,27,27,27,
29,30,30,31,33,34,34,34,35,36,
36,37,37,38,38,39,40,41,41,42,
43,44,45,45,46,47,48,48,49,50,
51,52,53,54,55,55,56,57,58,60,
61,63,64,65,66,68,70,71,72,74,
75,77,81,83,84,87,89,90,90,91

Using our formulas:

 n = number of observations = 100


 Range = maximum value – minimum value = 91 – 1 = 90
 # of intervals = √n = √100 = 10
 Width of intervals = Range / (# of intervals) = 90/10 = 9
Based on this information, the frequency table would look like this:

Intervals (bins) Frequency

0-9 9

10-19 13

20-29 19

30-39 15

40-49 13

50-59 10

60-69 7

70-79 6
80-89 5

90–99 3

For example:
import matplotlib.pyplot as plt
x = [1,1,2,3,3,5,7,8,9,10,
10,11,11,13,13,15,16,17,18,18,
18,19,20,21,21,23,24,24,25,25,
25,25,26,26,26,27,27,27,27,27,
29,30,30,31,33,34,34,34,35,36,
36,37,37,38,38,39,40,41,41,42,
43,44,45,45,46,47,48,48,49,50,
51,52,53,54,55,55,56,57,58,60,
61,63,64,65,66,68,70,71,72,74,
75,77,81,83,84,87,89,90,90,91
]

plt.hist(x, bins=[0,10,20,30,40,50,60,70,80,90,99])
plt.show()
output:
4)line chart
Line charts are great to show trends in data by plotting data points connected
with a line. In matplotlib, you can plot a line chart using pyplot’s plot()
function. The following is the syntax to plot a line chart:

import matplotlib.pyplot as plt


plt.plot(x_values, y_values)
Here, x_values are the values to be plotted on the x-axis and y_values are the
values to be plotted on the y-axis.

For example:
import matplotlib.pyplot as plt
# number of employees of A
emp_count = [3, 20, 50, 200, 350, 400]
year = [2014, 2015, 2016, 2017, 2018, 2019]
plt.plot(year, emp_count)
plt.xlabel("Year")
plt.ylabel("Employees")
plt.title("Employee Growth at A")
# plot a line chart
plt.show()

output:

You might also like