Unit 5
Unit 5
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:
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
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:
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.
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
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:
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: