0% found this document useful (0 votes)
12 views

Final_dvp_manual (1)

dvp

Uploaded by

Sathvik Mv
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)
12 views

Final_dvp_manual (1)

dvp

Uploaded by

Sathvik Mv
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/ 29

RV Institute of Technology and Management®

Rashtreeya Sikshana Samithi Trust


RV Institute of Technology and Management®
(Affiliated to VTU, Belagavi) JP Nagar, Bengaluru - 560076

Department of Computer Science and Engineering

Lab Course Name: Data Visualization using Python

Course Code: BCS358D

Semester: 3

Scheme: 2022

Prepared By:

Mrs. Rachana M S
Assistant Professor,
Department of Computer Science and Engineering
Email: [email protected]

III-Semester, Data Visualization using Python (BCS358D) P a g e 1 | 22


RV Institute of Technology and Management®

1a) Write a python program to find the best of two test average marks out of three
test’s marks accepted from the user.

m1 = int(input("Enter marks for test1 : "))


m2 = int(input("Enter marks for test2 : "))
m3 = int(input("Enter marks for test3 : "))
best_of_two = sorted([m1, m2, m3], reverse=True)[:2]
average_best_of_two = sum(best_of_two)/2

print("Average of best two test marks out of three test’s marks is",
average_best_of_two)

Output:

1b) Develop a Python program to check whether a given number is palindrome or not
and also count the number of occurrences of each digit in the input number.

num=input("enter the number")


num_list=list(num)
rev_num=num_list[::-1]
if num_list == rev_num:
print("the number is palindrome")
else:
print("the number is nt palindrome")
counter = dict()
for i in num_list:
if counter.get(i)==None:
counter[i]=1
else:
counter[i]=counter[i]+1
print(counter)

III-Semester, Data Visualization using Python (BCS358D) P a g e 2 | 22


RV Institute of Technology and Management®

Output : 1

Output2:

Q2. a) Defined as a function F as Fn = Fn-1 + Fn-2. Write a Python program which


accepts a value for N (where N >0) as input and pass this value to the function. Display
suitable error message if the condition for input value is not followed.

def fn(n):
if n == 1:
return 0
elif n == 2:
return 1
else:
return fn(n - 1) + fn(n - 2)

num = int(input("Enter a number : "))


if num>0:
fib = fn(num)
print("Fibonacci value is:",fib)
else:
print("Incorrect value of N")

Output

III-Semester, Data Visualization using Python (BCS358D) P a g e 3 | 22


RV Institute of Technology and Management®

2b) Develop a python program to convert binary to decimal, octal to hexadecimal


using functions.

import sys
def bin2dec(val):
rev = val[::-1]
# print(rev)
i=0
dec = 0
for dig in rev:
dec = dec + int(dig) * 2 ** i
i=i+1
return dec

num1 = input("Enter a binary number : ")


num = [int(i) for i in num1]
for dig in num:
if dig == 0 or dig == 1:
pass
else:
print("Enter proper binary number")
sys.exit(0)
print(bin2dec(num1))
def oct2Hex(val):
rev = val[::-1]
dec = 0
i=0
for dig in rev:
dec += int(dig) * 8 ** i
i += 1
list = []
while dec != 0:
list.append(dec % 16)
dec = dec // 16
nl = []
for elem in list[::-1]:
if elem <= 9:
nl.append(str(elem))

else:
nl.append(chr(ord('A') + (elem - 10)))
hex = "".join(nl)
III-Semester, Data Visualization using Python (BCS358D) P a g e 4 | 22
RV Institute of Technology and Management®

return hex
try:
num2 = input("Enter a octal number : ")
print(oct2Hex(num2))
except ValueError:
print("Invalid literal in input with base 8")

Output :

III-Semester, Data Visualization using Python (BCS358D) P a g e 5 | 22


RV Institute of Technology and Management®

Q3. Write a Python program that accepts a sentence and find the number of
words, digits, uppercase letters and lowercase letters.

sentence = input("Enter a sentence: ")


words = digits = upper = lower = 0

#Splitting the sentence using split() method , by default split is by spaces


# Return value - list of strings

split_sentence = sentence.split()
print("The result of split() on input sentence is : \n"+str(split_sentence)+"\n")

words =len(split_sentence)

for c in sentence:
if c.isdigit():
digits = digits + 1
elif c.isupper():
upper = upper + 1
elif c.islower():
lower = lower + 1
print ("No of Words: ", words)
print ("No of Digits: ", digits)
print ("No of Uppercase letters: ", upper)
print ("No of Lowercase letters: ", lower)

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 6 | 22


RV Institute of Technology and Management®

3b) Write a Python program to find the string similarity between two given
strings

str1 = input("Enter String 1:\n")


str2 = input("Enter String 2:\n")
if len(str2) < len(str1):
short = len(str2)
long = len(str1)
else:
short = len(str1)
long = len(str2)

matchCnt = 0
for i in range(short):
if str1[i] == str2[i]:
matchCnt += 1
print("Similarity between two said strings:")
print(matchCnt / long)

or
# An alternative solution to the same problem using Python libraries

from difflib import SequenceMatcher


str1 = input("Enter String 1 : ")
str2 = input("Enter String 2 : ")

sim = SequenceMatcher(None, str1, str2).ratio()


print("Similarity between strings \"" + str1 + "\" and \"" + str2 + "\" is : ",sim)

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 7 | 22


RV Institute of Technology and Management®

Data Visualization
Matplotlib

Matplotlib is a popular Python library for creating static, animated, and interactive
visualizations in a variety of formats. It is widely used for producing high-quality plots
and charts in scientific computing, data analysis, and machine learning. Matplotlib
provides a range of functions for creating different types of plots, including line plots,
scatter plots, bar plots, histograms, and more. Different visualizations plots are as
follows:

Scatter plots: Scatter plots are particularly useful when exploring the relationship
between two continuous variables. They excel at revealing patterns, trends, and
correlations between data points. These visualizations are adept at identifying outliers,
showcasing them as points deviating from the main cluster. By providing a clear picture
of the distribution of data points along two axes, scatter plots aid in understanding the
spread and density of values. Moreover, they are valuable for comparing different
datasets, recognizing similarities or differences.

Histogram: A histogram is a graphical representation of the distribution of a dataset,


typically used for continuous or discrete data. It provides a way to visualize the
frequency or count of data points within specific intervals or bins. In a histogram, the
data is divided into contiguous, non-overlapping intervals, and the height of each bar in
the chart represents the frequency or count of data points falling within that interval.
To create a histogram, you divide the range of the data into bins or intervals and then
count the number of data points that fall into each bin. The resulting bar chart, with the
bars representing these counts, provides a visual summary of the data's distribution.

III-Semester, Data Visualization using Python (BCS358D) P a g e 8 | 22


RV Institute of Technology and Management®

Bar chart: A bar chart is a graphical representation of data in which rectangular bars
are used to represent the values of different categories. Each bar's length is proportional
to the value it represents. Bar charts are effective for comparing discrete categories or
groups and are particularly useful for showing the distribution of categorical data.

Pie chart: Pie charts are a type of data visualization that is commonly used to represent
the proportions of different parts of a whole. The primary purpose of a pie chart is to
show the relationship of parts to a whole and to illustrate how each part contributes to
the total.

Seaborn

Seaborn is a statistical data visualization library built on top of Matplotlib in Python. It


provides an interface for creating informative and attractive statistical graphics. Seaborn
comes with several built-in themes and color palettes to make it easy to create
aesthetically pleasing visualizations. It is particularly useful for exploring complex
datasets and understanding relationships between variables.

Bokeh

Bokeh is a Python interactive visualization library that targets modern web browsers for
presentation. It allows you to create interactive, web-ready visualizations in Python.
Bokeh generates HTML and JavaScript code that can be embedded into web pages.
This allows you to create interactive visualizations that can be easily shared on the web.

III-Semester, Data Visualization using Python (BCS358D) P a g e 9 | 22


RV Institute of Technology and Management®

Plotly

Plotly is a versatile Python library for creating interactive and publication-quality plots
and dashboards. It supports a wide range of chart types. Plotly excels at creating
interactive plots. Users can zoom, pan, hover over data points for additional
information, and perform other interactive actions directly within the plot. Its ability to
create web-based dashboards makes it a powerful tool for building data-driven
applications.

III-Semester, Data Visualization using Python (BCS358D) P a g e 10 | 22


RV Institute of Technology and Management®

4a) Write a Python program to demonstrate how to draw a Bar Plot using
Matplotlib.

import matplotlib.pyplot as plt

# Sample data for demonstration


categories = ['0-10', '10-20', '20-30', '30-40', '40-50']
values = [55, 48, 25, 68, 90]

# Create a bar plot


plt.bar(categories, values, color='skyblue')

# Add labels and title plt.xlabel('Overs') plt.ylabel('Runs')


plt.title('Bar Plot Showing Runs scored in an ODI Match')

# Display the plot


plt.show()

Output

III-Semester, Data Visualization using Python (BCS358D) P a g e 11 | 22


RV Institute of Technology and Management®

4b) Write a Python program to Demonstrate how to Draw a Scatter Plot using
Matplotlib.

import matplotlib.pyplot as plt


import numpy as np

# BRICS nations data (hypothetical)


countries = ['Brazil', 'Russia', 'India', 'China', 'South Africa']
population = [213993437, 145912025, 1393409038, 1444216107, 61608912]
# Population in 2021
per_capita_income = [9600, 11600, 2300, 11000, 6500] # Per capita income in
USD

# Scale the population for circle size


circle_size = [pop / 1000000 for pop in population]

# Scaling down for better visualization

# Assign different colors based on index


colors = np.arange(len(countries))

# Create a scatter plot with varying circle sizes and colors

scatter = plt.scatter(population, per_capita_income, s=circle_size, c=colors,


cmap='viridis', alpha=0.7, label='BRICS Nations')

# Annotate each point with the country name

for i, country in enumerate(countries):


plt.annotate(country, (population[i], per_capita_income[i]), textcoords="offset
points", xytext=(0,5), ha='center')

# Add colorbar
plt.colorbar(scatter,
label='Index')

III-Semester, Data Visualization using Python (BCS358D) P a g e 12 | 22


RV Institute of Technology and Management®

# Add labels and title


plt.xlabel('Population')
plt.ylabel('Per Capita Income
(USD)')
plt.title('Population vs Per Capita Income of BRICS Nations')

# Display the plot


plt.show()

Output

III-Semester, Data Visualization using Python (BCS358D) P a g e 13 | 22


RV Institute of Technology and Management®

Q5. a) Write a Python program to demonstrate how to draw a Histogram Plot


using Matplotlib.

import matplotlib.pyplot as plt


import numpy as np

# Generate random student scores (example data)


np.random.seed(42)
student_scores = np.random.normal(loc=70, scale=15, size=100)

# Create a histogram plot


plt.hist(student_scores, bins=20, color='skyblue', edgecolor='black')

# Add labels and title


plt.xlabel('Student Scores')
plt.ylabel('Frequency')
plt.title('Distribution of Student Scores')

# Display the plot


plt.show()

Output

III-Semester, Data Visualization using Python (BCS358D) P a g e 14 | 22


RV Institute of Technology and Management®

5 b) Write a Python program to Demonstrate how to Draw a Pie Chart using


Matplotlib.

import matplotlib.pyplot as plt

#Number of FIFA World Cup wins for different countries


countries = ['Brazil', 'Germany', 'Italy', 'Argentina', 'Uruguay', 'France', 'England',
'Spain']

wins = [5, 4, 4, 3, 2, 2, 1, 1]
# Replace with actual data

# Colors for each country


colors = ['yellow', 'magenta', 'green', 'blue', 'lightblue', 'blue', 'red', 'cyan']

plt.pie(wins, labels=countries, autopct='%1.1f%%', colors=colors, startangle=90,


explode=[0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2], shadow=True)

# Add title
plt.title('FIFA World Cup Wins by Country')

# Display the plot


plt.axis('equal') # Equal aspect ratio ensures that the pie chart is circular.
plt.show()

III-Semester, Data Visualization using Python (BCS358D) P a g e 15 | 22


RV Institute of Technology and Management®

Q6. a) Write a Python program to illustrate Linear Plotting using Matplotlib.

Python Code:

import matplotlib.pyplot as plt


# Hypothetical data: Run rate in an T20 cricket match
overs = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
runs_scored=[0,7,12,20,39,49,61,83,86,97,113,116,123,137,145,163,172,192,198,198,
203]

# Create a linear plot


plt.plot(overs, runs_scored)

# Add labels and title


plt.xlabel('Overs')
plt.ylabel('Runs scored')
plt.title('Run scoring in an T20 Cricket Match')
# Display the plot
plt.grid(True)
plt.show()

Output

III-Semester, Data Visualization using Python (BCS358D) P a g e 16 | 22


RV Institute of Technology and Management®

6 b) Write a Python program to illustrate liner plotting with line formatting using
Matplotlib.

import matplotlib.pyplot as plt

# Hypothetical data: Run rate in an T20 cricket match

overs =
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
runs_scored =
[0,7,12,20,39,49,61,83,86,97,113,116,123,137,145,163,172,192,198,198,203]

# Create a linear plot


plt.plot(overs, runs_scored, marker='X', linestyle='dashed',color='red', linewidth=2,
markerfacecolor='blue', markersize=8)

# Add labels and title


plt.xlabel('Overs', color = 'green')
plt.ylabel('Runs scored')
plt.title('Run scoring in an T20 Cricket Match')

# Display the plot


plt.grid(True)

plt.show()

III-Semester, Data Visualization using Python (BCS358D) P a g e 17 | 22


RV Institute of Technology and Management®

Q7. 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):
x = np.linspace(0, 14, 100)
for i in range(1, n + 1):
plt.plot(x, np.sin(x + i * .5) * (n + 2 - i))
sns.set() # to set the theme
#sns.set_context("talk")
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})

sinplot()
plt.title('Seaborn plots with Aesthetic functions')
plt.show()

Output:

OR

III-Semester, Data Visualization using Python (BCS358D) P a g e 18 | 22


RV Institute of Technology and Management®

Dataset used: Tips csv file

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
plt.show()

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 19 | 22


RV Institute of Technology and Management®

Q8. a Write a Python program to explain working with bokeh line graph using
Annotations and Legends.

import numpy as np
from bokeh.layouts import gridplot
from bokeh.plotting import figure, show

x = np.linspace(0, 4*np.pi,
100) y = np.sin(x)

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

p2= figure(title="Example 2", tools=TOOLS, width=400, height=400)

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", line_width=2)

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'

show(gridplot([ p2], ncols=2))

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 20 | 22


RV Institute of Technology and Management®

Q8.b 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("tips.csv")

# Output to static HTML file


output_file("bokeh_tips_plots.html")

III-Semester, Data Visualization using Python (BCS358D) P a g e 21 | 22


RV Institute of Technology and Management®

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

# 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_label='Average Total Bill', 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='Total Bill',
y_axis_label='Tip')
scatter_plot.scatter(x='total_bill', y='tip', size=8, color="green", alpha=0.6,
source=tips)

# Combine plots into a grid


plots = gridplot([[hist_plot, bar_plot], [scatter_plot]])
# Show the combined plot
show(plots)

III-Semester, Data Visualization using Python (BCS358D) P a g e 22 | 22


RV Institute of Technology and Management®

III-Semester, Data Visualization using Python (BCS358D) P a g e 23 | 22


RV Institute of Technology and Management®

Q9. Write a Python program to draw 3D Plots using Plotly Libraries

import plotly.graph_objects as go
import pandas as pd

# Load the tips dataset


tips = pd.read_csv("tips.csv")

# Create a 3D scatter plot


fig = go.Figure()
scatter = go.Scatter3d(
x=tips['total_bill'],
y=tips['tip'],
z=tips['size'],
mode='markers',
marker=dict(size=8, color=tips['size'], colorscale='Viridis', opacity=0.8) )

fig.add_trace(scatter)
# Set axis labels and title
fig.update_layout(scene=dict(xaxis_title='Total Bill',yaxis_title='Tip',
zaxis_title='Size'))
fig.update_layout(title='3D Scatter Plot with Tips Dataset')

# Save the plot as an HTML file


fig.write_html("3d_scatter_plot_tips.html")

III-Semester, Data Visualization using Python (BCS358D) P a g e 24 | 22


RV Institute of Technology and Management®

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 25 | 22


RV Institute of Technology and Management®

Q10. a) Write a Python program to draw Time Series using Plotly Libraries.

Python Code: Dataset to be downloaded here- https://moodle.sit.ac.in/blog/wp-


content/uploads/2023/10/CUR_DLR_INR.csv

import pandas as pd
import plotly.express as
px

dollar_conv = pd.read_csv('CUR_DLR_INR.csv')

fig = px.line(dollar_conv, x='DATE', y='RATE', title='Dollar vs Rupee')


fig.show()

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 26 | 22


RV Institute of Technology and Management®

10 b. Write a Python program for creating Maps using Plotly Libraries.

Python Code: Download Gapminder dataset Example 1

import plotly.express as px

import pandas as pd

# Import data from


GitHub

data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapmind
er_with_codes.csv')

# Create basic choropleth map

fig = px.choropleth(data, locations='iso_alpha', color='gdpPercap',


hover_name='country', projection='natural earth', title='GDP per Capita by Country')

fig.show()

Output:

III-Semester, Data Visualization using Python (BCS358D) P a g e 27 | 22


RV Institute of Technology and Management®

Viva Questions

1. What are some important features of a good data visualization?

2. What is a scatter plot? For what type of data is scatter plot usually used for?

3. What features might be visible in scatterplots?

4. What type of plot would you use if you need to demonstrate “relationship” between
variables/parameters?

5. When will you use a histogram and when will you use a bar chart? Explain with an example.

6. What type of data is box-plots usually used for? Why?

7. When analyzing a histogram, what are some of the features to look for?

8. What type of data is histograms usually used for?

9. What is the difference between count histogram, relative frequency histogram, cumulative
frequency histogram and density histogram?

10. What are some advantages of using cleveland dot plot versus bar chart?

11. How do you determine the color palette in your plots?

12. What is the difference between plt.show() and plt.savefig() in Matplotlib?

13. How can you create a histogram in Matplotlib?

14. How can you add a legend to a plot in Matplotlib?

15. What is the purpose of the plt.subplots() function in Matplotlib?

16. How can you set the font size of a plot in Matplotlib?

17. What is the difference between a scatter plot and a line plot in Matplotlib?

18. How can you add text to a plot in Matplotlib?

19. What is the difference between a bar plot and a histogram in Matplotlib?

20. What is the purpose of the plt.subplot() function in Matplotlib?

21. How can you create a pie chart in Matplotlib?

III-Semester, Data Visualization using Python (BCS358D) P a g e 28 | 22


RV Institute of Technology and Management®

22. How can you create a heat map in Matplotlib?


23. What is Seaborn?

24. Does Seaborn need Matplotlib?

25. What is CMAP in Seaborn?

26. How do you plot a histogram in Seaborn?

27. How do I make all of the lines in seaborn.lineplot black?

28. How to change the legend font size of FacetGrid plot in Seaborn?

29. What is Plotly?

30. What are some of the main features offered by Plotly?

31. When should we use Plotly instead of Seaborn?

32. What’s the significance of x0, y0, dx, dy, angle, color, alpha, align, and font size parameters
in Plotly?

33. What types of plots can be created using Plotly?

34. What is the difference between express and graph_objects in Plotly?

35. What is the difference between Plotly and Matplotlib?

III-Semester, Data Visualization using Python (BCS358D) P a g e 29 | 22

You might also like