DSP LAB-3 (Part-A)
DSP LAB-3 (Part-A)
1. Line plots.
# importing the required libraries
import matplotlib.pyplot as plt
import numpy as np
# define data values
x = np.array([1, 2, 3, 4]) # X-axis points
y = x*2 # Y-axis points
plt.plot(x, y) # Plot the chart
plt.show() # display
----------------------------------------------------------------
----------------------------------------------------------------
plt.show()
----------------------------------------------------------------
2.Scatter Plots.
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()
----------------------------------------------------------------
3.Area Plots.
# library
import numpy as np
import matplotlib.pyplot as plt
# Create data
x=[1,3,2,5,6]
y=[1,4,6,8,4]
print(x)
----------------------------------------------------------------
# Seasons
seasons = ("Spring", "Summer", "Fall", "Winter");
# Create a DataFrame instance
dataFrame = pd.DataFrame(tempData, index=seasons);
#Draw an area plot for the DataFrame data
dataFrame.plot(kind='area', stacked=False)
plot.show(block=True);
----------------------------------------------------------------
4.Bubble Plots.
import numpy as np
# create data
x = np.random.rand(40)
Data Science using Python Lab
y = np.random.rand(40)
z = np.random.rand(40)
colors = np.random.rand(40)
plt.scatter(x, y, s=z*1000,c=colors)
plt.show()
----------------------------------------------------------------
y =[99, 86, 87, 88, 100, 86,103, 87, 94, 78, 77, 85, 86]plt.scatter(x, y, c ="blue")
plt.show()
N = 13
colors = np.random.rand(N)
plt.xlabel("X", size=16)
plt.ylabel("y", size=16)
5.Bar Plots.
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x,y)
Data Science using Python Lab
plt.show()
----------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
# creating the dataset
data = {'C':20, 'C++':15, 'Java':30, 'Python':35}
courses = list(data.keys())
Data Science using Python Lab
values = list(data.values())
fig = plt.figure(figsize = (10, 5))
# creating the bar plot
plt.bar(courses, values, color ='maroon', width = 0.4)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different courses")
plt.show()
----------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
# set height of bar
IT = [12, 30, 1, 8, 22]
ECE = [28, 6, 16, 5, 10]
CSE = [29, 3, 24, 25, 17]
# Set position of bar on X axis
br1 = np.arange(len(IT))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
# Make the plot
plt.bar(br1, IT, color ='r', width = barWidth,edgecolor ='grey', label ='IT')
----------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
# set height of bar
IT = [12, 30, 1, 8, 22]
ECE = [28, 6, 16, 5, 10]
CSE = [29, 3, 24, 25, 17]
Data Science using Python Lab
6.Pie Charts.
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35,25,25,15])
plt.pie(y)
plt.show()
----------------------------------------------------------------
----------------------------------------------------------------
# Import libraries
from matplotlib import pyplot as plt
import numpy as np
# Creating dataset
cars = ['AUDI', 'BMW', 'FORD','TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]
# Creating plot
fig = plt.figure(figsize =(10, 7))
plt.pie(data, labels = cars)
# show plot
plt.show()
----------------------------------------------------------------
Data Science using Python Lab
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Creating dataset
cars = ['AUDI', 'BMW', 'FORD', 'TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]
# Creating explode data
explode = (0.1, 0.0, 0.2, 0.3, 0.0, 0.0)
# Creating color parameters
colors = ( "orange", "cyan", "brown","grey", "indigo", "beige")
# Wedge properties
wp = { 'linewidth' : 1, 'edgecolor' : "green" }
# Creating autocpt arguments
def func(pct, allvalues):
absolute = int(pct / 100.*np.sum(allvalues))
return "{:.1f}%\n({:d} g)".format(pct, absolute)
# Creating plot
fig, ax = plt.subplots(figsize =(10, 7))
wedges, texts, autotexts = ax.pie(data,autopct = lambda pct: func(pct, data),explode = explode, labels = cars, shadow =
True,colors = colors,startangle = 90,wedgeprops = wp, textprops = dict(color ="magenta"))
# Adding legend
ax.legend(wedges, cars, title ="Cars",loc ="center left", bbox_to_anchor =(1, 0, 0.5, 1))
plt.setp(autotexts, size = 8, weight ="bold")
ax.set_title("Customizing pie chart")
# show plot
plt.show()
7.Box Plots.
# Import libraries Output:[1, 2, 3, 4]
import numpy as np
# Creating dataset
data = [1,2,3,4,]
print(data)
# Creating plot
plt.boxplot(data)
# show plot
plt.show()
----------------------------------------------------------------
import pandas as pd
import numpy as np
# Import libraries
import numpy as np
# Creating dataset
np.random.seed(10)
----------------------------------------------------------------
value1 =[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52]
Data Science using Python Lab
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data)
plt.show()
----------------------------------------------------------------
value1 =[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21]
value1 =[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data,notch='True',patch_artist=True,labels=['course1','course2','course3','course4'])
plt.show()
----------------------------------------------------------------
value1 =[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]
box_plot_data=[value1,value2,value3,value4]
box=plt.boxplot(box_plot_data,vert=0,patch_artist=True,labels=['course1','course2','course3','course4'],)
Data Science using Python Lab
plt.show()
8.Histogram.
import pandas as pd
import matplotlib.pyplot as plt
data = {'Name':['Arnav', 'Sheela', 'Azhar','Bincy','Yash','Nazar'],
'Height' : [60,61,63,65,61,60],
'Weight' : [47,89,52,58,50,47]}
df=pd.DataFrame(data)
df.plot(kind='hist',edgecolor='Green',linewidth=2,linestyle=':',fill=False,hatch='o')
plt.show()
----------------------------------------------------------------
# Show plot
plt.xlabel('age')
plt.ylabel('count')
plt.title('Histogram Example')
plt.show()
----------------------------------------------------------------
a = np.array([22, 87, 5, 43, 56,73, 55, 54, 11,20, 51, 5, 79, 31,27])
# Creating histogram
# bins{int,sequence.string)
plt.hist(a,bins=5,ec='red') #binn=int , ec=edge colour
plt.hist(a,bins=[0,25,50,75,100],ec='red') #binn=sequence , ec=edgecolour
plt.hist(a,bins=[0,25,50,75,100],ec='red') #binn=string , ec=edgecolour
# Show plot
plt.xlabel('age')
plt.ylabel('count')
plt.title('Histogram Example')
plt.show()