0% found this document useful (0 votes)
47 views14 pages

Lab 3

The document describes experiments to familiarize students with various data visualization techniques including scatter plots, box plots, bar plots, histograms, stem plots, and line plots using matplotlib and random data. It includes code samples to generate each type of plot and discusses the algorithms and parameters used. The overall aim is to introduce common data visualization methods using Python plotting libraries.

Uploaded by

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

Lab 3

The document describes experiments to familiarize students with various data visualization techniques including scatter plots, box plots, bar plots, histograms, stem plots, and line plots using matplotlib and random data. It includes code samples to generate each type of plot and discusses the algorithms and parameters used. The overall aim is to introduce common data visualization methods using Python plotting libraries.

Uploaded by

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

LAB 3: SIMPLE DATA VISUALIZATION

AIM: To familiarize stem plot, line plot, box plot, bar plots, scatter plots and
histogram with random data.
Scatter plot and Box plot
(i)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 Matplotlib module has a method for drawing scatter plots, it needs two
arrays of the same length, one for the values of the x-axis, and one for the values
of the y-axis.
Each row in the data table is represented by a marker the position depends on
its values in the columns set on the X and Y axes. A third variable can be set to
correspond to the color or size of the markers, thus adding yet another
dimension to the plot.
Syntax matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None,
marker=None, cmap=None, vmin=None, vmax=None, alpha=None,
linewidths=None, edgecolors=None)
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
cmap- cmap name
linewidths- width of marker border
edgecolor- marker border color
alpha- blending value, between 0 (transparent) and 1 (opaque)
Except x_axis_data and y_axis_data all other parameters are optional and their
default value is None
(ii)Box Plot:
A Box Plot is also known as Whisker plot is created to display the summary of
the set of data values having properties like minimum, first quartile, median,
third quartile and maximum. In the box plot, a box is created from the first
quartile to the third quartile, a verticle line is also there which goes through the
box at the median. Here xaxis denotes the data to be plotted while the y-axis
shows the frequency distribution.The matplotlib.pyplot module of matplotlib
library provides boxplot() function with the help of which we can create box
plots.
Syntax matplotlib.pyplot.boxplot(data,notch=None,vert=None,patch_artist=
None, widths=None)
Parameters
Data - array or sequence of array to be plotted
Notch- optional parameter accepts boolean values
Bootstrap- optional parameter accepts int specifies intervals around notched
boxplots
Usermedians- optional parameter accepts array or sequnce of array
dimension compatible with data
Positions- optional parameter accepts array and sets the position of boxes
Widths- optional parameter accepts array and sets the width of boxes
patch_artist- optional parameter having boolean values
labels- sequence of strings sets label for each dataset
meanline- optinal having boolean value try to render meanline as full width
of box
zorder- optional parameter sets the zorder of the boxplot

Experiment 1
AIM: To implement and plot the function f(t) = cos(t), f(t) = cos(t)cos(5t)+
cos(5t)
ALGORITHM

1.START
2.Import numpy
3.Import matplot.pyplot
4.Declare the time points
5.Declare plot function to plot the required function
6.Declare show function to display the plot
7.STOP

CODE
import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(0,10,50)
co=np.cos(t)
plt.plot(t,co)
plt.xlabel('t')
plt.ylabel("amplitude")
plt.show()
plt.xlabel('t')
plt.ylabel("amplitude")
f=co*np.cos(5*t)+np.cos(5*t)
plt.plot(t,f)
plt.show()

OUTPUT
Experiment 2
AIM: To plot the histogram of a random data

ALGORITHM

1.START
2.Import numpy
3.Import matplot.pyplot
4.Use numpy.random.normal() to generate some random data
5.Declare plot function to plot the histogram[plt.hist()]
6.Declare show function to display the plot
7.STOP

CODE
import numpy as np
import matplotlib.pyplot as plt
num = np.random.normal(size=10000)
print(num)
plt.hist(num)
plt.title("Histogram")
plt.xlabel("value")
plt.ylabel("Frequency")
plt.show()

OUTPUT
Experiment 3
AIM: To draw the stem plot of sin(t)

ALGORITHM

1.START
2.Import numpy
3.Import matplot.pyplot
4.Declare the time points
5.Declare plot function to obtain the stem plot[plt.stem()]
6.Declare show function to display the plot
7.STOP

CODE
import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(0,10,50)
si=np.sin(t)
plt.stem(t,si)
plt.xlabel('t')
plt.ylabel('sin t')
plt.title('sin(t) stem plot')
plt.show()

OUTPUT

Experiment 4
AIM: To obtain a bar plot for the given data. The number of students admitted
in various departments of an Engineering college is, EC: - 130,CS:- 70, ME:- 190,
CIVIL :- 130, B.Arch :- 40.
ALGORITHM
1: START
2: import numpy as np and matplotlib.pyplot as plt
3: Use python dictionary to store the given data
4: Store keys in courses and values in students variable.
5: Plot the bar graph using plt.bar(courses,students,color=”blue”,width=0.5)
6: STOP
CODE
import numpy as np
import matplotlib.pyplot as plt
data={"EC":130,"CS":70,"ME":190,"CIVIL":130,"B.Arch":40}
courses=list(data.keys())
students=list(data.values())
plt.bar(courses,students,color="orange",width=0.5)
plt.xlabel("Branch")
plt.ylabel("Students")
plt.title("Bar plot")

OUTPUT
Experiment 5
AIM: To create 200 random values with mean 100 and standard deviation 200
and generate its box plot
ALGORITHM
1: START
2: import matplotlib.pyplot as plt
3: Initialize the given mean and standard deviation values.
4: Use s=np.random.normal(mean,standard deviation,200)to get normalized
data distribution
5: Use plt.boxplot(s) to obtain the boxplot
6: STOP
CODE
import matplotlib.pyplot as plt
mean=100
sd=20
x= np.random.normal(mean,sd,200)
plt.boxplot(x)
plt.title("boxplot")
plt.show()

OUTPUT

Experiment 6
AIM: To draw stem plots, line plots, box plots, bar plots and scatter plots with
random data.
ALGORITHM
1: START
2: import numpy as np and matplotlib.pyplot as plt
3: Use x=np.random.normal(size=10)to obtain random values.
4: Use plt.stem(x,use_line_collection=true)to obtain stem plot
5:Use plt.plot(x) for line plot
6:Use plt.bar(x,y,color,width)for bar plot,provided x and y are random
numbers
7:Initialize data =np.random.normal(100,20,200) and plt.boxplot(data) for
boxplot
8: use plt.scatter()for scatter plot
9: STOP
CODE
import random
import matplotlib.pyplot as plt
import numpy as np
#stem plot
num_ = np.random.normal(size=10)
plt.stem(num_)
plt.show()
#line plot
num = np.random.normal(size=10)
plt.plot(num)
plt.show()
#bar plot
num_1 = np.random.randint(0,100,10)
num_2 = np.random.randint(0,100,10)
plt.bar(num_1,num_2,width = 0.8, color = 'green')
plt.show()
#box plot
data = np.random.normal(100, 20, 200)
plt.boxplot(data)
plt.show()
#scatter plot
num_1 = np.random.normal(size=10)
num_2=np.random.normal(size=10)
plt.scatter(num_1,num_2)
plt.show()
OUTPUT
RESULT: Understood simple data visualisation

You might also like