Data Science using Python Lab
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
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
# Define X and Y variable data
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("Any suitable title") # add title
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Any suitable title")
plt.show() # show first chart
# The figure() function helps in creating a
# new figure that can hold a new chart in it.
plt.figure()
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
plt.plot(x1, y1, '-.')
# Show another chart with '-' dotted line
plt.show()
Data Science using Python Lab
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
# first plot with X and Y data
plt.plot(x, y)
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
# second plot with x1 and y1 data
plt.plot(x1, y1, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
plt.plot(x, y1, '-.')
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linestyle = 'dotted') #dashed
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, color = 'r')
Data Science using Python Lab
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linewidth = '20.5')
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])
plt.plot(x1, y1, x2, y2)
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()
----------------------------------------------------------------
import matplotlib.pyplot as plt
# dataset-1
x1 = [89, 43, 36, 36, 95, 10,66, 34, 38, 20]
y1 = [21, 46, 3, 35, 67, 95,53, 72, 58, 10]
# dataset2
x2 = [26, 29, 48, 64, 6, 5,36, 66, 72, 40]
y2 = [26, 34, 90, 33, 38,20, 56, 2, 47, 15]
plt.scatter(x1, y1, c ="pink",linewidths = 2,marker ="s",edgecolor ="green",s = 50)
Data Science using Python Lab
plt.scatter(x2, y2, c ="yellow",linewidths = 2,marker ="^",edgecolor ="red",s = 200)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
girls_grades = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]
boys_grades = [30, 29, 49, 48, 100, 48, 38, 45, 20, 30]
grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.scatter(grades_range, girls_grades, color='r')
ax.scatter(grades_range, boys_grades, color='b')
ax.set_xlabel('Grades Range')
ax.set_ylabel('Grades Scored')
ax.set_title('scatter 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)
----------------------------------------------------------------
# Example Python program to draw an overlapped area plot
# for a pandas DataFrame
import pandas as pd
import matplotlib.pyplot as plot
# Peak Temperature data for two cities
tempData = {"City1":[99, 106, 102, 78],
"City2":[77, 84, 80, 85]};
Data Science using Python Lab
# 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);
----------------------------------------------------------------
# Example Python program that draws a overlapping area
# plot for a pandas DataFrame instance
import pandas as pd
import matplotlib.pyplot as plot
# Number of observations
data = [(25, 1),
(43, 1),
(35, 2),
(34, 4)];
# Years
index = ["2016", "2017", "2018", "2019"]; # (X Axis)
# Name of the Quantities corresponding to the
#observations(Y Axis)
columns = ["Meteors", "Meteorites"];
# Create DataFrame instance
df = pd.DataFrame(data=data, index = index, columns =
columns);
# Draw an area plot that overlaps
ax = df.plot.area(stacked=True);
plot.show(block=True);
4.Bubble Plots.
import matplotlib.pyplot as plt
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)
# use the scatter function
plt.scatter(x, y, s=z*1000,c=colors)
plt.show()
----------------------------------------------------------------
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()
N = 13
colors = np.random.rand(N)
area = (25 * np.random.rand(N))**2
df = pd.DataFrame({ 'X': x, 'Y': y, 'Colors': colors, "bubble_size":area})
plt.scatter('X', 'Y',s='bubble_size',alpha=0.5, data=df,c=colors)
plt.xlabel("X", size=16)
plt.ylabel("y", size=16)
plt.title("Bubble Plot with Matplotlib", size=18)
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 matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.barh(x, y)
plt.show()
----------------------------------------------------------------
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, color = "red")
plt.show()
----------------------------------------------------------------
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, width = 0.1)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.barh(x, y, height = 0.1)
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
# 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')
plt.bar(br2, ECE, color ='g', width = barWidth, edgecolor ='grey', label ='ECE')
plt.bar(br3, CSE, color ='b', width = barWidth,edgecolor ='grey', label ='CSE')
# Adding Xticks
plt.xlabel('Branch', fontweight ='bold', fontsize = 15)
plt.ylabel('Students passed', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(IT))],['2015', '2016', '2017', '2018', '2019'])
plt.legend()
plt.show()
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 matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels, startangle = 90)
plt.show()
----------------------------------------------------------------
Data Science using Python Lab
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels = mylabels, explode = myexplode)
plt.show()
----------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]
plt.pie(y, labels = mylabels, colors = mycolors)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.legend()
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 matplotlib.pyplot as plt
import numpy as np
# Creating dataset
data = [1,2,3,4,]
print(data)
fig = plt.figure(figsize =(10, 5))
Data Science using Python Lab
# Creating plot
plt.boxplot(data)
# show plot
plt.show()
----------------------------------------------------------------
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 6), columns=['A', 'B', 'C', 'D', 'E','F'])
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
np.random.seed(10)
data_1 = np.random.normal(100, 10, 200)
data_2 = np.random.normal(90, 20, 200)
data_3 = np.random.normal(80, 30, 200)
data_4 = np.random.normal(70, 40, 200)
data = [data_1, data_2, data_3, data_4]
fig = plt.figure(figsize =(10, 7))
----------------------------------------------------------------
import matplotlib.pyplot as plt
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()
----------------------------------------------------------------
import matplotlib.pyplot as plt
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]
import matplotlib.pyplot as plt
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()
----------------------------------------------------------------
import matplotlib.pyplot as plt
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
colors = ['cyan', 'lightblue', 'lightgreen', 'tan']
for patch, color in zip(box['boxes'], colors):patch.set_facecolor(color)
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()
----------------------------------------------------------------
from matplotlib import pyplot as plt
import numpy as np
# Creating dataset
a = np.array([1,12,22,21,20,21])
# Creating histogram
plt.hist(a,bins=5,ec='red',cumulative='True')
#plt.hist(a,bins=5,ec='red',cumulative='-1')
# Show plot
plt.xlabel('age')
plt.ylabel('count')
plt.title('Histogram Example')
plt.show()
----------------------------------------------------------------
from matplotlib import pyplot as plt
import numpy as np
# Creating dataset
a = np.array([1,12,22,21,20,21])
# Creating histogram
# bins{int,sequence.string)
plt.hist(a,bins=5,ec='red',weights=[2,2,2,2,3,3])
Data Science using Python Lab
# Show plot
plt.xlabel('age')
plt.ylabel('count')
plt.title('Histogram Example')
plt.show()
----------------------------------------------------------------
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
# bins{int,sequence.string)
plt.hist(a,bins=5,ec='red',density='True') #binn=int , ec=edge colour
# Show plot
plt.xlabel('age')
plt.ylabel('count')
plt.title('Histogram Example')
plt.show()
----------------------------------------------------------------
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
# bins{int,sequence.string)
plt.hist(a,bins=5,ec='red') #binn=int , ec=edge colour
# Show plot
plt.xlabel('age')
plt.ylabel('count')
plt.title('Histogram Example')
plt.show()
----------------------------------------------------------------
from matplotlib import pyplot as plt
import numpy as np
# Creating dataset
Data Science using Python Lab
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()