Experiment No 4 : Simple Data Visualization

 Objectives

  • Draw stem plots, line plots, box plots, bar plots and scatter plots with random data.

  • plot the histogram of a random data.

  • create legends in plots.

  • Realize a vector t = [-10, 10] with increment 0:01 as an array.

  • Implement and plot the functions

    •  f(t) = cos t

    • f(t) = cos t cos 5t + cos 5t


Theory

Intro to pyplot

matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.


In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes (please note that "axes" here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).


Programe

import matplotlib.pyplot as plt

x = [1, 4, 2, 5, 8, 6]

plt.plot(x)

plt.xlabel("X - axis")

plt.ylabel('Y - axis')

plt.show()

 

Output

Progame

import matplotlib.pyplot as plt

X = [1, 4, 2, 5, 8, 6]

plt.stem(X,use_line_collection=True)

plt.xlabel("X - axis")

plt.ylabel('Y - axis')

plt.show()

Output

Program for Box plot

import numpy as np

import matplotlib.pyplot as plt

 

# Fixing random state for reproducibility

np.random.seed(19680801)

 

# fake up some data

spread = np.random.rand(50) * 100

center = np.ones(25) * 50

flier_high = np.random.rand(10) * 100 + 100

flier_low = np.random.rand(10) * -100

data = np.concatenate((spread, center, flier_high, flier_low))

fig1, ax1 = plt.subplots()

ax1.set_title('Basic Plot')

ax1.boxplot(data)

 

Output

 

Programe

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_axes([0,0,1,1])

langs = ['C', 'C++', 'Java', 'Python', 'PHP']

students = [18,17,20,29,12]

ax.bar(langs,students)

plt.show()


Output

Program

import numpy as np

import matplotlib.pyplot as plt

# Fixing random state for reproducibility

np.random.seed(19680801)

N = 50

x = np.random.rand(N)

y = np.random.rand(N)

colors = np.random.rand(N)

area = (30 * np.random.rand(N))**2  # 0 to 15 point radii

 

plt.scatter(x, y)

plt.title("Scatter plot")

plt.show()

Output

Histogram

A histogram is basically used to represent data provided in a form of some groups.It is an accurate method for the graphical representation of numerical data distribution.It is a type of bar plot where X-axis represents the bin ranges while Y-axis gives information about frequency.

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and the count the values which fall into each of the intervals.Bins are clearly identified as consecutive, non-overlapping intervals of variables.The matplotlib.pyplot.hist() function is used to compute and create histogram of x.

The following table shows the parameters accepted by matplotlib.pyplot.hist() function :

Program

from matplotlib import pyplot as plt 

import numpy as np   

# Creating dataset 

a = np.array([22, 87, 5, 43, 56,  

              73, 55, 54, 11

              20, 51, 5, 79, 31

              27]) 

# Creating histogram 

fig, ax = plt.subplots(figsize =(10, 7)) 

ax.hist(a, bins = [0, 25, 50, 75, 100]) 

# Show plot 

plt.show() 

Output

Program

import matplotlib.pyplot as plt

import numpy as np

 

x = np.linspace(-10,10,1000)

plt.plot(x)

plt.xlabel("X - axis")

plt.ylabel('Y - axis')

plt.show()

Output

Program

from numpy import *

from matplotlib.pyplot import *

# Get x values of the sine wave

t = arange(0, 10, 0.01);

# calculating the amplitude

y = cos(t)

# Plot a sine wave using time and amplitude obtained for the sine wave

plot(t,y)

title('Cose wave') # Give a title for the sine wave plot

xlabel('Time') # Give x axis label for the sine wave plot

ylabel('Amplitude = Cos(t)') # Give y axis label for the sine wave plot

# grid(True, which='both') # enabling the grid

show() # showing the figure

Output


Program

from numpy import *

from matplotlib.pyplot import *

# Get x values of the sine wave

t = arange(0, 10, 0.01);

# calculating the amplitude

y1 = cos(t)

y2 = cos(5*t)

y = y1*y2+y2

# plotting cos signals in subplot

rcParams["figure.figsize"] = (10,10) # changing the figure size 

fig, axs = subplots(3)

axs[0].plot(t,y1) 

axs[0].set_title("cos(t) signal ")

axs[1].plot(t,y2)

axs[1].set_title("cos(5*t) Signal")

axs[2].plot(t,y)  

axs[2].set_title("cos(t)*cos(5*t)+cos(5*t)")

show() # showing the figure

Output

Inference 

  • Drawn stem plots, line plots, box plots, bar plots and scatter plots with random data.

  • plotted the histogram of a random data.

  • created legends in plots.

  • Realized a vector t = [-10, 10] with increment 0:01 as an array.

  • Implement and plotted the functions

    •  f(t) = cos t

    • f(t) = cos t cos 5t + cos 5t

No comments:

Post a Comment

Signals & System Lect 15 | Stable & Unstable Systems | Solved examples

  ECT 204 SIGNALS AND SYSTEMS Topics covered  00:00 - Introduction to Stable & unstable Systems 01:08 - BIBO criteria 03:38 Problem No 1...