Practical.
No: 02
Data Visualization in Python
# Python program to demonstrate unary operators in numpy
Name: _____________________
Class: S.E (E&TC).
Roll. No:____________________
Matplotlib-
# Creating Simple plot with matplotlib
Example No:01
import [Link] as plt
import numpy as np
import math #needed for definition of pi
x=[Link](0, [Link]*2, 0.05)
y=[Link](x)
[Link](x,y)
[Link]("angle")
[Link]("sine")
[Link]('sine wave')
[Link]()
OUTPUT:
Example No: 2
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10, 5, 7])
[Link](ypoints)
[Link]()
OUTPUT:
Line Chart :
Example No: 01
import [Link] as plt
%matplotlib inline
x = [1,2,3,4,5,6,7,8,9,10,11,12]
y1 = [35, 40, 45, 42,55, 45, 50,48,52,60,53,55]
y2 = [15,21,15,18,21, 28, 35, 29,38,35,30,41]
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
[Link](x,y1,'ys-') # solid line with yellow colour and square marker
[Link](x,y2,'go--') # dash line with green colour and circle marker
[Link](labels=( 'Smartphone','tv'), loc='lower right') # legend at lower right
ax.set_title("Monthly Sales")
ax.set_xlabel('month')
ax.set_ylabel('sales')
[Link]()
OUTPUT :
Example No: 02
import [Link] as plt
Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
Unemployment_Rate = [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
[Link](Year, Unemployment_Rate, color='red', marker='o')
[Link]('Unemployment Rate Vs Year', fontsize=14)
[Link]('Year', fontsize=14)
[Link]('Unemployment Rate', fontsize=14)
[Link](True)
[Link]()
OUTPUT:
Bar Chart :
Example No: 01
import [Link] as plt
%matplotlib inline
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
langs=['C', 'C++', 'Java', 'Python', 'PHP']
students=[23,17,35,29,12]
[Link](langs,students)
ax.set_xlabel('Languages')
ax.set_ylabel('Number of Students')
[Link]()
OUTPUT:
Example No: 02
import numpy as np
import [Link] as plt
# creating the dataset
data = {'C':20, 'C++':15, 'Java':30, 'Python':35}
courses = list([Link]())
values = list([Link]())
fig = [Link](figsize = (10, 5))
# creating the bar plot
[Link](courses, values, color ='maroon', width = 0.4)
[Link]("Courses offered")
[Link]("No. of students enrolled")
[Link]("Students enrolled in different courses")
[Link]()
OUTPUT:
Scatter plot :
Example No: 01
import [Link] as plt
%matplotlib inline
boys_grades = [75, 78, 65, 72, 68, 80, 55, 45, 85, 87,75,77,74,71,85]
roll_no = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15]
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
[Link](roll_no,boys_grades, color='b')
ax.set_xlabel('Roll Number')
ax.set_ylabel('Grades Scored')
ax.set_title('Scatter plot')
[Link]()
OUTPUT:
Example No: 02
import numpy
import [Link] as plt
x = [Link](5.0, 1.0, 1000)
y = [Link](10.0, 2.0, 1000)
[Link](x, y)
[Link]()
Histogram :
Example No :01
import [Link] as plt
import numpy as np
%matplotlib inline
x=[Link](170,10,250)
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
[Link](x)
ax.set_xlabel('Event')
ax.set_ylabel('Frequency')
ax.set_title('Histogram')
[Link]()
OUTPUT:
Example No : 02
import numpy as np
import [Link] as mlab
import [Link] as plt
x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
num_bins = 5
n, bins, patches = [Link](x, num_bins, facecolor='blue', alpha=0.5)
[Link]()
OUTPUT:
Bar Plot-
Example No : 01
import numpy as np
import [Link] as plt
data = [[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]]
X = [Link](4)
fig = [Link]()
ax = fig.add_axes([0,0,1,1])
[Link](X + 0.00, data[0], color = 'b', width = 0.25)
[Link](X + 0.25, data[1], color = 'g', width = 0.25)
[Link](X + 0.50, data[2], color = 'r', width = 0.25)
OUTPUT:
Example No :02
import pandas as pd
from matplotlib import pyplot as plt
# Read CSV into pandas
data = pd.read_csv(r"[Link]")
[Link]()
df = [Link](data)
name = df['car'].head(12)
price = df['price'].head(12)
# Figure Size
fig = [Link](figsize =(10, 7))
# Horizontal Bar Plot
[Link](name[0:10], price[0:10])
# Show Plot
[Link]()
OUTPUT:
Box Plot :
Example No :01
# Import libraries
import [Link] as plt
import numpy as np
# Creating dataset
[Link](10)
data = [Link](100, 20, 200)
fig = [Link](figsize =(10, 7))
# Creating plot
[Link](data)
# show plot
[Link]()
OUTPUT:
Example No :02
import numpy as np
import [Link] as plt
# Fixing random state for reproducibility
[Link](19680801)
# fake up some data
spread = [Link](50) * 100
center = [Link](25) * 50
flier_high = [Link](10) * 100 + 100
flier_low = [Link](10) * -100
data = [Link]((spread, center, flier_high, flier_low))
fig1, ax1 = [Link]()
ax1.set_title('Basic Plot')
[Link](data)
OUTPUT :
FacetGrid –
Example No : 01
# importing packages
import seaborn
import [Link] as plt
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
# Form a facetgrid using columns with a hue
graph = [Link](df, row ='smoker', col ='time')
# map the above form facetgrid with some attributes
[Link]([Link], 'total_bill', bins = 15, color ='orange')
# show the object
[Link]()
OUTPUT:
Example No :02
# importing packages
import seaborn
import [Link] as plt
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
# Form a facetgrid using columns with a hue
graph = [Link](df, col ='time', hue ='smoker')
# map the above form facetgrid with some attributes
[Link]([Link], "total_bill", "tip").add_legend()
# show the object
[Link]()
OUTPUT:
Heat Map –
Example No :01
import pandas as
pd import seaborn
as sns
import [Link] as plt
%matplotlib inline
data =
pd.read_csv('[Link]',index_col='MONTH')
[Link](data,annot=True)
[Link]('Monthly Percentage Attendance')
OUTPUT:
Example No :02
import numpy as np
import matplotlib
import [Link] as plt
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
harvest = [Link]([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = [Link]()
im = [Link](harvest)
# Show all ticks and label them with the respective list entries
ax.set_xticks([Link](len(farmers)), labels=farmers)
ax.set_yticks([Link](len(vegetables)), labels=vegetables)
# Rotate the tick labels and set their alignment.
[Link](ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
for i in range(len(vegetables)):
for j in range(len(farmers)):
text = [Link](j, i, harvest[i, j], ha="center", va="center", color="w")
ax.set_title("Harvest of local farmers (in tons/year)")
fig.tight_layout()
[Link]()
OUTPUT:
Pair Plot –
Example No : 01
# importing the required libraries
import seaborn as sbn
import [Link] as plt
# loading the dataset using the seaborn library
mydata = sbn.load_dataset('penguins')
# pairplot with the hue = gender parameter
[Link](mydata, hue = 'gender')
# displaying the plot
[Link]()
OUTPUT: