Experiment 3
Experiment 3
Experiment 3
Data visualization
Plotting
• Install ‘matplotlib’- graph plotting library
• For plotting import matplotlib
• 2D and 3D graphs
• pip install matplotlib
• Most of the Matplotlib utilities lies under the pyplot submodule, and
are usually imported under the plt alias
• import matplotlib.pyplot as plt
Types of Plots
Common plots in matplotlib
The plot() function
• Used to draw points (markers) in a diagram
• By default, the plot() function draws a line from point to point
• To plot a line from (x1,y1) to (x2,y2), we have to pass two arrays [x1,y1]
and [x2,y2] to the plot function.
• Need numpy
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()
• Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1)
and finally to position (8, 10)
The plot() function
• plot(x, y, color=’green’, marker=’o’,linestyle=’dashed’, linewidth=2,
markersize=12): x and y co-ordinates are marked using circular
markers of size 12 and green color line with — style of width 2
• marker - *, +, v, s…….
• color –c, m, b, g,r
• If the points in the x-axis is not specified, they will get the default
values 0, 1, 2, 3, (etc. depending on the length of the y-points.)
ypoints = np.array([3, 8, 1, 10, 5, 7])
• Use the xlabel() and ylabel() functions to set a label for the x-
and y-axis, plt.title('title', loc='left')
plot.xlabel(‘X-axis’): names x-axis and plot.ylabel(‘Y-axis’): names y-axis
The stem() function
• Plots vertical lines at each x location from the baseline to y, and
places a marker there.
• Almost similar to plot()
• linefmt='-' , '.', etc
Box plot
• Whisker plot or box and whisker plot
• Display the summary of the set of data values having properties like
minimum, first quartile, median, third quartile and maximum
Box plot
import matplotlib.pyplot as plt
import numpy as np
from numpy import random
M=10
x = np.arange(0, M, 1)
ypoints =random.randint(11, size=(M))
plt.boxplot(ypoints)
plt.show()
Scatter Plots
• The scatter() function plots one dot for each observation.
• It needs two arrays of the same length, one for the values of the x-
axis, and one for values on the y-axis:
• Used to observe relationship between variables.
• Uses dots to represent the relationship between them
Scatter Plots
• import matplotlib.pyplot as plt
• import numpy as np
• #Data set 1
• 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, color = 'red')
• #Data set 2
• 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, color = 'green')
• plt.show()
Bar plots
• Use the bar() function to draw bar graphs
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["Apple", "Banana", "Cucumber", "Tomato"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
plt.show()
Subplots
• With the subplots() function multiple plots in one figure
Subplots
• import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
• Try other possibilities
Legends
• A legend can be placed inside or outside the chart and at
various positions
• The legend() is used to add legend to plots
• The attribute loc in legend() is used to specify the location of the
legend.
• Default value of loc is loc=”best” (upper left)
• The strings ‘upper left’, ‘upper right’, ‘lower left’, ‘lower right’ place the
legend at the corresponding corner of the axes/figure
Legends
• import numpy as np
• import matplotlib.pyplot as plt
• x=[2,3,4,5]
• y1 = [8, 27, 64, 125]
• y2 = [4,9, 16, 25]
• plt.plot(x,y1)
• plt.plot(x,y2)
• # Function add a legend
• plt.legend(["cube", "Square"], loc ="lower right")
• plt.show()
• Try to change position of legend
Histogram
• A histogram is a graph showing frequency distributions of numerical data or
image data
• It is a graph showing the number of observations within each given interval.
• X- no of people, y- height
• 2 people - height 140-145
• 30 people - 10-165
• Maximum 50 – 175-180
• X-axis represents bin
• Y-axis – frequency
• first step is to create bin of the ranges
• Setting the bin size of a Matplotlib histogram specifies the size of
• the groups into which the data is sorted.
• then count the values which fall
into each of the intervals
Histogram
• Histogram of data about the age of 100 individuals:
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=10)
plt.show()
Saving plots
• plt.savefig("filename.jpg")
Experiment 3 (to record)
• 3.1 Draw stem plots, line plots and bar plots with random data.
• 3.2 Draw box plots with random data.
• 3.3 Draw scatter plots with random data
• 3.4 Plot the histogram of a random data.
• 3.5 Create legends in plots
• 3.6 Realize a vector t = [-10, +10] with increment 0.01 as an array.
Implement and plot the functions:
f(t) = cost
f(t) = cost cos 5t + cos 5t