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

DV Nivas

dv

Uploaded by

chandruyck42
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 views24 pages

DV Nivas

dv

Uploaded by

chandruyck42
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/ 24

SCHOOL OF ENGINEERING AND TECHNOLOGY

LAB REPORT (Data Visualization)


th
For the requirement of 4 Semester (Data Visualization 4CSDS2021)B.Tech

In Computer Science and Engineering

Submitted By
NIVAS B L (22BBTCD040)

Under the guidance of

Prof. LATHASHREE P V

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CMR University
(Lakeside Campus)
Off Hennur - Bagalur Main Road,
Near Kempegowda International Airport, Chagalahatti,Bengaluru, Karnataka-562149,
Academic Year 2023-
2024

1
TABLE OF CONTENT
SI.No Program Name Page Number

1 Write a python program to visualize the data in Line Plots


3

2 Write a python program to visualize the data in Area Plots. 4

3 Write a python program to visualize the data in Histogram Plots. 5

4 Write a python program to visualize the data in Bar Plots. 6

5 Write a python program to visualize the data in Pie Chart . 7

6 Write a python program to visualize the data in Scatter Plots. 8

7 Write a Python program to illustrate Pie plotting with line


formatting using Matplotlib. 9

8 Write a Python program which explains uses of customizing


seaborn plots with Aesthetic functions. 10

9 Write a Python program to explain working with Bokeh line


graph using Annotations and Legends. 11-12

10 Write a Python program for plotting different types of plots using


Bokeh. 13

11 Write a Python program to Demonstrate how to Draw a Bar


Plot using Matplotlib 14

12 Write a Python program to Demonstrate how to draw a Scatter Plot


using Matplotlib 15

13 Write a Python program to Demonstrate how to draw a


Histogram using Matplotlib 16

14 Write a Python program to Demonstrate how to draw a Histogram


using Matplotlib 17

15 Write a Python program to illustrate Linear Plotting using


Matplotlib 18

16 Write a Python program to illustrate liner plotting with line


formatting using Matplotlib 19

17 Write a Python program liner plotting with line formatting using


Matplotlib 20

18 Write a Python program to explain working with bokeh line graph


using Annotations an Legends. 21

19 Write a Python program for plotting different types of plots using


Bokeh 21-22

20 Write a Python program for creating Maps using Plotly Libraries. 23-24

2
1. Write a python program to visualize the data in Line Plots.

Line Plots: A line plot is used to represent quantitative values over a continuous interval or
time period. It is generally used to depict trends on how the data has changed over time.

import matplotlib.pyplot as plt


x=[1,2,3,4,5]
y=[10, 50, 90, 45, 55]
plt.plot(x,y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

3
2) Write a python program to visualize the data in Area Plots.
Area Plots: An Area Plot is also called as Area Chart which is used to display magnitude and
proportion of multiple variables.

import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5]
sleeping = [8, 7, 6, 9, 8]
eating = [3, 4, 3, 3, 4]
working = [6, 7, 8, 5, 6]
playing = [7, 8, 6, 7, 9]

plt.plot([], [], color='w', label='Sleeping', linewidth=5)


plt.plot([], [], color='b', label='Eating', linewidth=5)
plt.plot([], [], color='r', label='Working', linewidth=5)
plt.plot([], [], color='g', label='Playing', linewidth=5)

plt.stackplot(days, sleeping, eating, working, playing, colors=['w', 'b', 'r', 'g'])


plt.xlabel('Days')
plt.ylabel('Hours')
plt.title('Stack Plot')
plt.legend()
plt.show()

4
3) Write a python program to visualize the data in Histogram Plots.
Histograms: Histograms represents the frequency distribution of a dataset. It is a graph
showing the number of observations within each given interval.

import matplotlib.pyplot as plt

population_age = [18, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
bins = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

plt.hist(population_age, bins, histtype='bar', rwidth=0.8)


plt.xlabel('Age Groups')
plt.ylabel('Number of People')
plt.title('Age Distribution Histogram')

plt.show()

5
4) Write a python program to visualize the data in Bar Plots.
A Bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars
with heights or lengths proportional to the values that they represent. A bar plot is a way of
representing data where the length of the bars represents the magnitude/size of the
feature/variable.
from matplotlib import pyplot as plt
bmw_distances = [60, 70, 50, 40, 30]
audi_distances = [70, 60, 40, 50, 80]
plt.bar([0.25, 1.25, 2.25, 3.25, 4.25], bmw_distances, label="BMW", width=0.5)
plt.bar([0.75, 1.75, 2.75, 3.75, 4.75], audi_distances, label="Audi", color='m',
width=0.5)
plt.legend()
plt.xlabel('Days')
plt.ylabel('Distance (kms)')
plt.title('Information')
plt.show()

6
5) Write a python program to visualize the data in Pie Chart .

Pie Charts:
A Pie chart is a circular statistical chart, which is divided into sectors to illustrate numerical
proportion

import matplotlib.pyplot as plt


slices = [5, 6, 3, 9]
activities = ['sleeping', 'eating', 'working',
'exercising']
cols = ['b', 'y', 'r', 'g']
plt.pie(slices, labels=activities, colors=cols,
startangle=140, shadow=True, explode=(0, 0.1, 0, 0),
autopct='%1.1f%%')

7
6) Write a python program to visualize the data in Scatter Plots.

Scatter Plots:
A Scatter chart, also called a scatter plot, is a chart that shows the relationship between two
variables.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7]
y = [10, 15, 20, 25, 30, 35, 40]
x1 = [2, 3, 4, 5, 6, 7, 8]
y1 = [5, 10, 15, 20, 25, 30, 35]
plt.scatter(x, y, label='High Income Low Saving', color='r')
plt.scatter(x1, y1, label='Low Income High Saving', color='b')
plt.xlabel('Saving (in thousands)')
plt.ylabel('Income (in thousands)')
plt.title('Scatter Plot')
plt.legend()

8
7) Write a Python program to illustrate Pie plotting with line formatting using
Matplotlib.

import matplotlib.pyplot as plt


countries = ['Brazil', 'Germany', 'Italy', 'Argentina',
'Uruguay', 'France', 'England', 'Spain']
wins = [4, 6, 3, 2, 5, 1, 3, 1]
colors = ['yellow', 'black', 'blue', 'skyblue', 'lightblue',
'navy', 'red', 'brown']
plt.pie(wins, labels=countries, autopct='%1.1f%%',
colors=colors, startangle=90,
explode=[0.1, 0, 0, 0, 0, 0, 0, 0], shadow=True)
plt.title('FIFA World Cup Wins by Country')
plt.axis('equal')
plt.show()

9
8) Write a Python program which explains uses of customizing seaborn plots
with Aesthetic functions.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def sinplot(n=10, amplitude=1, phase_shift=0.5, frequency=1):
x = np.linspace(0, 14, 100)
for i in range(1, n + 1):
plt.plot(x, amplitude * np.sin(frequency * (x + i *
phase_shift)) * (n + 2 - i))
sns.set_theme()
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
sinplot(n=8, amplitude=2, phase_shift=0.3, frequency=1.5)
plt.title('Seaborn Plots with Aesthetic Functions')
plt.grid(True)
plt.show()

10
9) Write a Python program to explain working with Bokeh line graph using
Annotations
and Legends.

import matplotlib.pyplot as plt


import csv
import operator
import datetime as dt
# Function to filter data based on conditions
def filter_data(file_path):
with open(file_path, 'rt') as f:
filtered_data = [row for row in csv.reader (f) if row[3] != "0.0"
and row[3
return filtered_data

# Function to extract data for a specific country and indicator


def extract_country_data (filtered_data, country, indicator):
return [row for row in filtered_data if row[1] == country and
row[0] == indicat
# Function to plot data
def plot_data(country_data, country_name, indicator_name) :
x_values = [dt .datetime.strptime(row[2], '%Y-%m-%d'). date() for
row in countr y_values = [float(row[3]) for row in country_data]
plt. plot(x_values, y_values, label=country_name)
# Main function
def main():
file_path = r"C:\Users\ksvra\Downloads\ebola_data_db_format.csv"
# Filter data
filtered_data = filter_data(file_path)
# Extract data for specific countries and indicators
guinea_data =
extract_country_data(filtered_data,'Guinea',"Cumulative number of
sierra_data = extract_country_data(filtered_data,'Sierra
Leone',"Cumulative num liberia_data =
extract_country_data(filtered_data, "Liberia", "Cumulative numbe #
Plot data

11
plt.figure(figsize=(10, 10))
plot_data(guinea_data, "Guinea", "Confirmed Ebola Deaths")
plot_data(sierra_data, "Sierra Leone","Confirmed Ebola
Deaths") plot_data (liberia_data, "Liberia", "Confirmed
Ebola Deaths") plt.xlabel('Date', fontsize=18)
plt.ylabel( 'Number of Ebola Deaths',
fontsize=18) plt. title("Confirmed Ebola
Deaths",fontsize=20) plt.legend(loc=2)
plt.show()
if name == ' main ':
main()

12
10) Write a Python program for plotting different types of plots using Bokeh.

import numpy as np
from bokeh.layouts import gridplot
from bokeh.plotting import figure, show
import matplotlib.pyplot as
plt x = np.linspace(0,
4*np.pi, 100) y = np.sin(x)

TOOLS = "pan, wheel_zoom, box_zoom, reset, save, box_select"

pl = figure(title="Example 1", tools=TOOLS)


pl.circle(x, y, legend_label="sin(x)")
pl.circle(x, 2*y, legend_label="2*sin(x)", color="orange")
pl.circle(x, 3*y, legend_label="3*sin(x)", color="green")
pl.legend.title = "Markers"

p2 = figure(title="Example 2",
tools=TOOLS) p2.circle(x, y,
legend_label="sin(x)") p2.line(x, y,
legend_label="sin(x)")
p2.line(x, 2*y, legend_label="2*sin(x)", line_dash=(4, 4),
line_color="orange", lin p2.square(x, 3*y, legend_label="3*sin(x)",
fill_color=None, line_color="green") p2.line(x, 3*y, legend_label="3*sin(x)",
line_color="green")
p2.legend.title = 'Lines'

13
11) Write a Python program to Demonstrate how to Draw a Bar Plot using Matplotlib

import matplotlib.pyplot as plt


import pandas as pd

# Initialize the lists for X and Y


data = pd.read_csv(r"C:\Users\ksvra\Downloads\Cars.csv")
X = data.iloc[:, 0]
Y = data.iloc[:, 1]

# Plot the data using bar()


method plt.bar(X, Y, color='g')
plt.title("Used Car Sales")
plt.xlabel("Car")
plt.ylabel("Number Sold")

# Show the plot


plt.show()

14
12) Write a Python program to Demonstrate how to draw a Scatter Plot using
Matplotlib

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Importing data.
cars_data = pd.read_csv(r"C:\Users\ksvra\Downloads\Toyota.csv")

# Create scatter plot using two variables, Age and Price.


plt.scatter(cars_data['Age'], cars_data['Price'], c='blue')

# To set the title


plt.title('Scatter plot of Price vs Age of the Cars')

# To set the x and y axis


labels. plt.xlabel('Age
(months)') plt.ylabel('Price
(Euros)')

# To show the scatter plot


plt.show()

15
13) Write a Python program to Demonstrate how to draw a Histogram using Matplotlib

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

cars_data = pd.read_csv(r"C:\Users\ksvra\Downloads\Cars.csv")

plt.title('Histogram for distance travelled in Kilometer')


plt.hist(cars_data['mpg'], color='green', edgecolor='white',
bins=5) plt.xlabel('Kilometer')
plt.ylabel('Frequency')
plt.show()

16
14) Write a Python program to Demonstrate how to draw a Histogram using
Matplotlib

# Import libraries
import matplotlib.pyplot as plt
import pandas as pd

# Creating dataset
cars_data = pd.read_csv(r"C:
\Users\ksvra\Downloads\Car_sales.csv") cars =
cars_data["Manufacturer"].head(10)
data = cars_data["Sales in thousands"].head(10)

# Creating plot
fig = plt.figure(figsize =(10, 7))
plt.pie(data, labels=cars)

# show plot

17
15) Write a Python program to illustrate Linear Plotting using Matplotlib

import matplotlib.pyplot as plt

def linear_plot():
# Sample data
x = [1, 2, 3, 4, 5]
y = [3, 7, 9, 11, 14]

# Plotting the data


plt.plot(x, y, label='Linear Function: y = 2x')

# Adding labels and title


plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Linear Plot
Example') plt.legend()
plt.show()

# Call the function to generate the plot


linear_plot()

18
16) Write a Python program to illustrate liner plotting with line formatting using
Matplotlib

import matplotlib.pyplot as plt

def formatted_linear_plot():
# Sample data
x = [1, 2, 3, 4, 5, 6]
y = [3, 7, 9, 11, 14, 18]

# Plotting the data


plt.plot(x, y, marker='o', linestyle='-', color='b', label='Linear
Function: y

# Adding labels and


title plt.xlabel('X-
axis') plt.ylabel('Y-
axis')
plt.title('Formatted Linear Plot Example')
plt.legend()
plt.grid(True) # Add a grid for better readability
plt.show()

# Call the function to generate the formatted linear plot

19
17) Write a Python program liner plotting with line formatting using Matplotlib

import seaborn as sns


import matplotlib.pyplot as plt

# Load a sample dataset


tips = sns.load_dataset("tips")

# Set the aesthetic style of the plot


sns.set(style="whitegrid")

# Create a scatter plot using Seaborn


sns.scatterplot(x="total_bill", y="tip", style="time", size="size",
data=tips)

# Customize the plot further using Seaborn aesthetic functions


sns.despine() # Remove the top and right spines from the plot

# Set custom labels and


title plt.xlabel("Total
Bill ($)") plt.ylabel("Tip
($)")
plt.title("Scatter Plot of Total Bill vs Tip")

# Show the plot

20
18) Write a Python program to explain working with bokeh line graph using Annotations an
Legends.
from bokeh.plotting import figure, output_file, show
from bokeh.models import Label

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Output to static HTML file


output_file("line_graph_with_annotations.html")

# Create a figure
p = figure(title="Bokeh Line Graph with Annotations", x_axis_label='X-axis', y_axis

# Plot the line


p.line(x, y, line_width=2, line_color="blue", legend_label="Line Function: y = 2x")

# Add an annotation
annotation = Label(x=3, y=6, text="Important Point", text_font_size="10pt", text_co
p.add_layout(annotation)

# Add legend
p.legend.location = "top_left"
p.legend.click_policy = "hide"

# Show the plot


show(p)

21
19) Write a Python program for plotting different types of plots using Bokeh
import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import gridplot

# Load the tips dataset


tips = pd.read_csv(r"C:\Users\ksvra\Downloads\tip.csv")

# Output to static HTML file


output_file("bokeh_tips_plots.html")

# Histogram
hist, edges = np.histogram(tips['total_bill'], bins=8)
hist_plot = figure(title="Histogram of Total Bill",
x_axis_label='Total Bill', y_ax hist_plot.quad(top=hist, bottom=0,
left=edges[:-1], right=edges[1:], fill_color="pu

# Bar Plot
day_categories = tips['day'].unique()
average_total_bill = tips.groupby('day')['total_bill'].mean()
bar_plot = figure(title="Average Total Bill per Day",
x_axis_label='Day', y_axis_la x_range=day_categories)
bar_plot.vbar(x=day_categories, top=average_total_bill, width=0.5,
color="orange")

# Scatter Plot
scatter_plot = figure(title="Scatter Plot of Total Bill vs Tip",
x_axis_label='Tota

scatter_plot.scatter(x='total_bill', y='tip', size=8, color="green",


alpha=0.6, sou

# Combine plots into a grid


plots = gridplot([[hist_plot, bar_plot], [scatter_plot]])

# Show the combined plot


show(plots)

22
23
20) Write a Python program for creating Maps using Plotly Libraries.

import plotly.express as px

# Sample data for demonstration


data = {
'City': ['New York', 'San Francisco', 'Los Angeles', 'Chicago',
'Houston'], 'Lat': [40.7128, 37.7749, 34.0522, 41.8781, 29.7604],
'Lon': [-74.0060, -122.4194, -118.2437, -87.6298, -95.3698],
'Population': [8175133, 870887, 3971883, 2716000, 2328000]

# Create a map
fig = px.scatter_geo(data, lat='Lat', lon='Lon', text='City',
size='Population', projection='natural earth',
title='Population of Cities')

24

You might also like