Data Visualization with Python – BCS358D

In this blog post, you will find solutions for the Data Visualization with Python (BCS358D) course work for the III semester of VTU university. To follow along, you will need to set up a Python programming environment. We recommend using the Anaconda Python Distribution with Spyder as the integrated development environment (IDE). You can find the lab syllabus on the university’s website or here.

Syllabus

For detailed instructions on setting up the Python programming environment on Ubuntu, please refer to my previous blog, which can be found below.

Setting up Anaconda Python Programming Environment on Ubuntu

If you are looking for step-by-step instructions on how to set up the Python programming environment on a Windows system, I have provided detailed guidance in my previous blog. You can access the blog below for all the information you need.

After getting the necessary development environment setup, Now lets focus on the solutions.

  1. Question 1
    1. Test Average
    2. Palindrome
  2. Question 2
    1. Fibonacci Sequence
    2. Base Conversion
  3. Question 3
    1. Sentence statistics
    2. String Similarity
  4. Question 4
    1. Bar Plot using Matplotlib
    2. Scatter Plot using Matplotlib
  5. Question 5
    1. Histogram Plot using Matplotlib
    2. Pie Chart using Matplotlib
  6. Question 6
    1. Linear Plotting using Matplotlib
    2. Linear Plotting with line formatting using Matplotlib
  7. Question 7
    1. Seaborn plots with Aesthetic functions
  8. Question 8
    1. Bokeh line graph using Annotations and Legends
  9. Question 9
    1. 3D Plots using Plotly Libraries
  10. Question 10
    1. Time Series using Plotly Libraries
    2. Maps using Plotly Libraries.

Question 1

Calculation of Test Average

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

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 16:27:47 2023

@author: putta
"""

m1 = int(input("Enter marks for test1 : "))
m2 = int(input("Enter marks for test2 : "))
m3 = int(input("Enter marks for test3 : "))

# if m1 <= m2 and m1 <= m3:
#     avgMarks = (m2+m3)/2
# elif m2 <= m1 and m2 <= m3:
#     avgMarks = (m1+m3)/2
# elif m3 <= m1 and m2 <= m2:
#     avgMarks = (m1+m2)/2    

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);

The provided Python program is designed to calculate the average of the best two test marks out of three. The user is prompted to input the marks for three tests (test1, test2, and test3). The program then identifies the two highest test marks using the sorted function in descending order and selects the top two values. Finally, it calculates the average of these two highest marks and prints the result. This code is a concise way to determine the average performance based on the two best test scores out of three.

Output

Enter marks for test1: 85
Enter marks for test2: 92
Enter marks for test3: 78
Average of best two test marks out of three test’s marks is 90.0

Palindrome Check & Digit Occurrence Count

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.

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 16:35:54 2023

@author: putta
"""

from collections import Counter

value = input("Enter a value : ")
if value == value[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")
    
counted_dict = Counter(value)
for key in sorted(counted_dict.keys()):
    print(f'{key} appears {counted_dict[key]} times');

"""
#Alternate way to count appearances
for i in range(10):
    if value.count(str(i)) > 0:
        print(f'{str(i)} appears {value.count(str(i))} times')
"""

The provided Python script is a versatile program that performs two key tasks: palindrome checking and character counting.

  1. Palindrome Check:
    • The user is prompted to input a value.
    • The script determines whether the entered value is a palindrome, meaning it reads the same backward as forward.
    • If the input is a palindrome, it prints “Palindrome”; otherwise, it prints “Not Palindrome.”
  2. Character Count:
    • The script utilizes the Counter class from the collections module to efficiently count the occurrences of each character in the input string.
    • It then sorts the keys of the counted dictionary and prints each character along with the number of times it appears.
  3. Alternate Character Count (commented-out):
    • The script provides an alternative method using a loop to count the occurrences of each digit (0 to 9) in the input string.

This script is not only a quick and effective way to check for palindromes but also serves as a handy tool for analyzing the frequency of characters in a given input. The inclusion of alternative methods showcases the flexibility of the script, making it a valuable resource for both palindrome detection and character frequency analysis.

Output

Enter a value: 987654
Not Palindrome
4 appears 1 times
5 appears 1 times
6 appears 1 times
7 appears 1 times
8 appears 1 times
9 appears 1 times
Enter a value: 123321
Palindrome
1 appears 2 times
2 appears 2 times
3 appears 2 times

Question 2

Fibonacci Sequence

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.

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 16:46:55 2023

@author: putta
"""

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

try:
    num = int(input("Enter a number : "))
    if num > 0:
        print(f' fn({num}) = {fn(num)}')
    else:
        print("Input should be greater than 0")
except ValueError:
    print("Try with numeric value")

The provided Python script introduces a recursive implementation to calculate terms in the Fibonacci sequence. Here’s a concise overview:

  1. Recursive Fibonacci Function:
    • The script defines a function fn that calculates the nth term in the Fibonacci sequence.
    • If the input n is 1 or 2, the function returns n - 1.
    • For n greater than 2, the function recursively calls itself with n-1 and n-2 and returns the sum of the results.
  2. User Input and Output:
    • The user is prompted to enter a number.
    • If a positive integer is provided, the script prints the result of calling the fn function with that number, representing the nth term in the Fibonacci sequence.
    • If the entered value is not a positive integer, it prompts the user to enter a value greater than 0.
    • If a non-numeric value is entered, it catches the ValueError and suggests trying with a numeric value.

This script serves as a simple yet illustrative example of a recursive function for computing Fibonacci sequence terms, offering insights into recursive algorithms and user input handling in Python.

Output

Enter a number: 6
fn(6) = 5

Enter a number: -3
Input should be greater than 0

Enter a number: abc
Try with numeric value

Binary to Decimal & Octal to Hexadecimal Conversion

Develop a python program to convert binary to decimal, octal to hexadecimal using functions.

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 20:57:52 2023

@author: putta
"""

"""
def bin2Dec(val):
    rev=val[::-1]
    dec = 0
    i = 0
    for dig in rev:
        dec += int(dig) * 2**i
        i += 1
    
    return dec


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)
    
    return hex


base = 2
num1 = input("Enter a binary number : ")    
# print(bin2Dec(num1))
print(int(num1, base))
"""

#A better implementation
def bin2Dec(val):
    return int(val, 2)
    
def oct2Hex(val):
    return int(val, 8)

try:
    num1 = input("Enter a binary number : ")    
    print(bin2Dec(num1))
except ValueError:
    print("Invalid literal in input with base 2")
    
try:
    num2 = input("Enter a octal number : ")
    print(oct2Hex(num2))
except ValueError:
    print("Invalid literal in input with base 8")

This Python script includes two functions bin2Dec and oct2Hex for converting binary to decimal and octal to hexadecimal, respectively. The script takes user input for binary and octal numbers and converts them using these functions. Here’s a brief description:

  1. Conversion Functions:
    • The bin2Dec function converts a binary number to decimal using the int() function with base 2.
    • The oct2Hex function converts an octal number to hexadecimal using the int() function with base 8.
  2. User Input Handling:
    • The script includes try-except blocks to catch ValueError in case of invalid input.
    • It prompts the user for a binary and an octal number, converts them, and prints the results.
    • If an invalid input is detected, it prints an error message.

Output

Enter a binary number: 101010
42

Enter an octal number: 755
0x1FD

Enter a binary number: 11011a
Invalid literal in input with base 2

Enter an octal number: 1298
Invalid literal in input with base 8

Question 3

Sentence Statistics

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

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 22 01:37:07 2023

@author: putta
"""

import string

sentence = input("Enter a sentence : ")

wordList = sentence.strip().split(" ")
print(f'This sentence has {len(wordList)} words', end='\n\n')

digit_count = uppercase_count = lowercase_count = 0

for character in sentence:
    if character in string.digits: 
        digit_count += 1
    elif character in string.ascii_uppercase: 
        uppercase_count += 1
    elif character in string.ascii_lowercase:
        lowercase_count += 1

print(f'This sentence has {digit_count} digits',
      f' {uppercase_count} upper case letters',
      f' {lowercase_count} lower case letters', sep='\n')

The above Python program is designed to analyse a user-inputted sentence, providing information on the number of words, digits, uppercase letters, and lowercase letters in the given text. Here’s a concise overview:

  1. Word Count:
    • The script splits the input sentence into words and prints the count of words in the sentence.
  2. Character Analysis:
    • It then iterates through each character in the sentence.
    • The script counts the number of digits, uppercase letters, and lowercase letters using the string module.
  3. Print Results:
    • Finally, it prints the counts of digits, uppercase letters, and lowercase letters in the given sentence.

This script serves as a versatile tool for quickly extracting key statistics from a user-provided sentence, offering insights into the composition of the text in terms of words and character types.

Output

Enter a sentence : Rama went to Devaraja market to pick 2 kgs of vegetable
This sentence has 11 words

This sentence has 1 digits 
2 upper case letters 
42 lower case letters
Enter a sentence: Python is Fun!
This sentence has 3 words

This sentence has 0 digits
3 uppercase letters
9 lowercase letters
Enter a sentence: Hello, World! 123
This sentence has 3 words

This sentence has 3 digits
1 uppercase letters
12 lowercase letters

String Similarity

Write a Python program to find the string similarity between two given strings.

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 22 01:48:17 2023

@author: putta
"""
"""
str1 = input("Enter String 1 \n").lower()
str2 = input("Enter String 2 \n").lower()

# if len(str2) < len(str1):
#     short = len(str2)
#     long = len(str1)
# else:
#     short = len(str1)
#     long = len(str2)

string_1_length = len(str1)
string_2_length = len(str2)

short_string_length, long_string_length = min(string_1_length, string_2_length), max(string_1_length, string_2_length) 
    

match_count = 0
for i in range(short_string_length):
    if str1[i] == str2[i]:
        match_count += 1

print("Similarity between two said strings:")
print(match_count/long_string_length)


"""
# 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)

The provided Python script is designed to compare the similarity between two user-inputted strings. The user is prompted to input two strings, and the script, after converting them to lowercase for case-insensitive comparison, calculates the similarity by counting the matching characters at corresponding positions. The similarity is then expressed as a ratio relative to the length of the longer string. Here’s a brief overview:

  1. User Input:
    • The script prompts the user to enter two strings.
  2. String Comparison:
    • The script then iterates through characters at corresponding positions and counts the matches.
  3. Similarity Ratio:
    • The similarity between the two strings is calculated as the ratio of the count of matching characters to the length of the longer string.
  4. Alternative Solution :
    • The script includes an alternative solution using the SequenceMatcher class from the difflib library, demonstrating a different approach to calculating string similarity.

This script offers a straightforward way to measure the similarity between two strings and presents an alternative solution using Python libraries for a comparative understanding. It’s a useful tool for users interested in comparing the likeness of textual data.

Output

Enter String 1 : Python Exercises
Enter String 2 : Python Exercise 
Similarity between strings "Python Exercises" and "Python Exercise" is :  0.967741935483871

Enter String 1 : Python Exercises
Enter String 2 : Python Exercises
Similarity between strings "Python Exercises" and "Python Exercises" is :  1.0

Question 4

Bar Plot using Matplotlib

Write a Python program to Demonstrate how to Draw a Bar Plot using Matplotlib.

Python Code

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()

The provided Python script utilizes the matplotlib library to create a bar plot showcasing runs scored in an ODI (One Day International) cricket match. Here’s a concise overview:

  1. Sample Data:
    • The script uses sample data representing runs scored in specific overs during an ODI cricket match.
  2. Matplotlib Plotting:
    • It utilizes the matplotlib.pyplot module to create a bar plot.
    • The bar function is used to plot the data, where categories represent overs, and values represent runs scored.
  3. Labels and Title:
    • The script adds labels to the x-axis (Overs) and y-axis (Runs).
    • It includes a title, ‘Bar Plot Showing Runs scored in an ODI Match,’ to provide context to the plot.
  4. Display:
    • The show function is called to display the generated bar plot.

This script is a straightforward example of using matplotlib to visualize data in a bar plot. It’s a valuable resource for individuals interested in creating basic data visualizations, particularly in the context of cricket statistics.

Output

Bar Graph

Scatter Plot using Matplotlib

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

Python Code

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')

# 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()

The provided Python script employs the matplotlib library, along with numpy, to create a scatter plot visualizing population against per capita income for BRICS nations. Here’s a concise overview:

  1. Sample Data:
    • The script uses hypothetical data for BRICS nations, including population and per capita income.
  2. Scaling for Visualization:
    • Circle sizes are scaled down from population values to enhance visualization.
  3. Color Assignment:
    • Different colors are assigned based on the index of each country.
  4. Scatter Plot:
    • The scatter function is used to create a scatter plot with varying circle sizes and colors.
  5. Annotations:
    • Each point on the plot is annotated with the country name for clarity.
  6. Colorbar:
    • A colorbar is added to the plot, providing a reference for the color index.
  7. Labels and Title:
    • Labels for the x-axis, y-axis, and a title are included to provide context.
  8. Display:
    • The show function is called to display the generated scatter plot.

This script serves as an excellent example of using matplotlib for creating informative and visually appealing scatter plots, especially for comparing socio-economic indicators among different countries. It can be helpful for readers interested in data visualization and analysis.

Output


Question 5

Histogram Plot using Matplotlib

Write a Python program to Demonstrate how to Draw a Histogram Plot using Matplotlib.

Python Code

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()

The provided Python script utilizes the matplotlib library and numpy to generate a histogram plot illustrating the distribution of student scores. Here’s a concise overview:

  1. Random Data Generation:
    • The script generates example data representing student scores using a normal distribution (np.random.normal).
  2. Histogram Plot:
    • It creates a histogram plot using the hist function, where student_scores is the data array, bins determines the number of bins, and color sets the fill color while edgecolor sets the color of bin edges.
  3. Labels and Title:
    • The script adds labels to the x-axis (Student Scores) and y-axis (Frequency).
    • A title, ‘Distribution of Student Scores,’ is included to provide context to the plot.
  4. Display:
    • The show function is called to display the generated histogram plot.

This script serves as a simple yet effective demonstration of using matplotlib to visualize the distribution of a dataset, making it suitable for readers interested in introductory data visualization techniques. It’s especially valuable for those learning to interpret histograms in the context of student scores or similar datasets.

Output


Pie Chart using Matplotlib

Write a Python program to Demonstrate how to Draw a Pie Chart using Matplotlib.

Python Code

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()

The provided Python script utilizes the matplotlib library to create a visually appealing pie chart representing the number of FIFA World Cup wins for different countries. Here’s a concise overview:

Data Representation:

  • The script uses hypothetical data representing the number of FIFA World Cup wins for different countries.
  1. Pie Chart Creation:
    • It creates a pie chart using the pie function, where wins is the data array, labels are country names, and autopct formats the percentage display.
  2. Colors and Styling:
    • Different colors are assigned to each country using the colors parameter.
    • The pie chart is enhanced with features such as a start angle, explode effect, and shadow.
  3. Title:
    • The script adds a title to the pie chart, enhancing the overall context.
  4. Display:
    • The show function is called to display the generated pie chart.

This script serves as a clear and concise example of using matplotlib to create visually engaging pie charts, making it suitable for readers interested in representing categorical data, such as FIFA World Cup wins, in a graphical format.

Output

Display values instead of percentages

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']

def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{v:d}'.format(v=val)
    return my_autopct
    
    
# Create a pie chart
plt.pie(wins, labels=countries, autopct=make_autopct(wins), 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()

The provided Python script, an extension of the previous pie chart example, enhances the pie chart’s autopct (automatic percentage display) to display actual win counts for each country. Here’s a concise overview:

Custom Autopct Function:

  • The script defines a custom make_autopct function that takes the values (win counts) as input.
  • Inside this function, a nested function my_autopct calculates the actual win count based on the percentage.
  1. Enhanced Autopct in Pie Chart:
    • The autopct parameter in the pie function is set to the custom autopct function, resulting in the display of actual win counts.
  2. Display:
    • The script creates and displays the pie chart with enhanced autopct for a more informative representation of the data.

This script is a valuable addition for readers seeking to customize autopct in pie charts, providing a more detailed insight into the data being visualized.

Output


Question 6

Linear Plotting using Matplotlib

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()

The provided Python script utilizes the matplotlib library to create a linear plot representing the run rate in a hypothetical T20 cricket match. Here’s a concise overview:

  1. Hypothetical Data:
    • The script uses hypothetical data representing the number of runs scored in each over of a T20 cricket match.
  2. Linear Plot:
    • It creates a linear plot using the plot function, where overs is on the x-axis and runs_scored is on the y-axis.
  3. Labels and Title:
    • The script adds labels to the x-axis (Overs) and y-axis (Runs scored).
    • A title, ‘Run Scoring in a T20 Cricket Match,’ provides context to the plot.
  4. Grid:
    • The plot includes a grid for better readability.
  5. Display:
    • The show function is called to display the generated linear plot.

This script serves as a straightforward example of using matplotlib to visualize run scoring trends in a T20 cricket match, making it suitable for readers interested in representing time-dependent data in a graphical format.

Output


Linear Plotting with line formatting using Matplotlib

Write a Python program to illustrate liner plotting with line formatting 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, 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()

The provided Python script, an extension of the previous T20 cricket match run rate plot, customizes the appearance of the plot with specific markers, line styles, colors, and label styles. Here’s a concise overview:

  1. Customized Plot Appearance:
    • The plot function is customized with parameters such as marker, linestyle, color, linewidth, markerfacecolor, and markersize to control the appearance of the plot.
  2. Labels and Title Styling:
    • The script adds labels to the x-axis (Overs) and y-axis (Runs scored) with specific color styling.
    • The title, ‘Run Scoring in a T20 Cricket Match,’ maintains clarity.
  3. Grid:
    • The plot includes a grid for better readability.
  4. Display:
    • The show function is called to display the generated customized linear plot.

This script is an excellent example for readers looking to customize plot aesthetics in matplotlib for a more visually appealing representation of data. It’s especially helpful for those interested in enhancing the clarity and style of their data visualizations.

Output


Question 7

Seaborn plots with Aesthetic functions

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

Python Code

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_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()

The provided Python script utilizes the seaborn library, in conjunction with numpy and matplotlib, to create a series of sine wave plots with customized aesthetics. Here’s a concise overview:

  1. Data Generation:
    • The script uses numpy to generate a series of sine wave plots.
  2. Seaborn Integration:
    • seaborn is imported and configured with a default theme (set_theme).
    • The context is set to “notebook” with customized font scaling and line width (set_context).
  3. Customized Aesthetics:
    • The sinplot function generates multiple sine wave plots with varying frequencies and amplitudes.
  4. Title and Display:
    • The script adds a title to the plot, ‘Seaborn Plots with Aesthetic Functions.’
    • The show function is called to display the generated plots.

This script serves as an illustrative example of how seaborn can be used to enhance the aesthetics of data visualizations, providing readers with insights into customizing plot styles and themes for more visually appealing results. It’s particularly useful for those looking to leverage seaborn for improved aesthetics in their data analysis and visualization workflows.

Output



Question 8

Bokeh line graph using Annotations and Legends

Write a Python program to explain working with Bokeh line graph using Annotations and Legends.
a) Write a Python program for plotting different types of plots using Bokeh.

Python Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 30 02:17:24 2023

@author: putta
"""

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"

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

p1.circle(x,   y, legend_label="sin(x)")
p1.circle(x, 2*y, legend_label="2*sin(x)", color="orange")
p1.circle(x, 3*y, legend_label="3*sin(x)", color="green")

p1.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", 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([p1, p2], ncols=2, width=400, height=400))

The provided Python script demonstrates the use of the Bokeh library to create interactive data visualizations with multiple plots. Here’s a concise overview:

  1. Data Generation:
    • The script generates example data (x and y) using NumPy to represent sine waves.
  2. Interactive Tools:
    • Bokeh’s interactive tools (TOOLS) are enabled for features like pan, zoom, reset, and save.
  3. Multiple Plots:
    • Two separate plots (p1 and p2) are created with different visualizations, including circles, lines, and markers.
  4. Legend and Titles:
    • Legends are added to distinguish between different elements in the plots.
    • Titles are provided for each plot.
  5. Grid Layout:
    • The gridplot function is used to arrange the plots in a grid layout.
  6. Interactive Display:
    • The show function is called to display the grid layout, enabling interactive exploration.

This script serves as an introduction to using Bokeh for creating interactive visualizations with multiple plots, making it suitable for readers interested in interactive data exploration and visualization techniques.

Output


Question 9

3D Plots using Plotly Libraries

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

Python Code

import plotly.graph_objects as go
import numpy as np

# Generate sample 3D data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

# Create a 3D surface plot
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])

# Customize layout
fig.update_layout(scene=dict(
                    xaxis_title='X Axis',
                    yaxis_title='Y Axis',
                    zaxis_title='Z Axis'),
                margin=dict(l=0, r=0, b=0, t=40),
                title='3D Surface Plot of sin(sqrt(x^2 + y^2))')

# Display the 3D surface plot
fig.show()

This program generates a 3D surface plot of the function z = sin(sqrt(x2+y2​)). You can modify the function or provide your own data to create different types of 3D plots. The visualization will be interactive, allowing you to rotate and explore the plot.

Output

Another Example

import plotly.express as px
df = px.data.gapminder().query("continent=='Asia'")
fig = px.line_3d(df, x="gdpPercap", y="pop", z="year", color='country', title='Economic Evolution of Asian Countries Over Time')
fig.show()

In this Python program, we leverage the power of Plotly Express to visualize the economic evolution of Asian countries over time. The dataset used is Gapminder, a comprehensive collection of global development indicators. The focus is specifically on the Asian continent.

  1. Import Libraries:
    • We start by importing the necessary libraries, including plotly.express for interactive visualizations.
  2. Data Loading:
    • We load the Gapminder dataset and filter it to include only Asian countries.
  3. 3D Line Plot:
    • The key visualization is a 3D line plot created using px.line_3d.
    • The x-axis represents the GDP per capita (gdpPercap), the y-axis represents the population (pop), and the z-axis represents the year (year).
    • Each line corresponds to a different country, differentiated by color.
  4. Interactive Exploration:
    • The resulting plot is interactive, allowing users to zoom, pan, and hover over data points to explore specific details.

Users can observe how GDP per capita and population have changed over the years for various Asian countries. The color-coded lines help distinguish between different nations.

Output

Question 10

Time Series using Plotly Libraries

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

Python Code -Example 1

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  2 15:23:19 2023

@author: putta
"""

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()

CUR_DLR_INR.csv

The provided Python script showcases the use of the Plotly Express library to create an interactive line plot depicting the exchange rate between the US Dollar and the Indian Rupee over time. Here’s a concise overview:

  1. Data Import:
    • The script uses the Pandas library to read currency conversion data from a CSV file (‘CUR_DLR_INR.csv’). You can download the csv file given above.
  2. Plotly Express:
    • Plotly Express (px) is employed to create an interactive line plot with the exchange rate data.
  3. Line Plot:
    • The line function from Plotly Express is used to generate a line plot.
    • The x-axis represents dates (‘DATE’), and the y-axis represents exchange rates (‘RATE’).
  4. Title:
    • The plot is given a title, ‘Dollar vs Rupee,’ for context.
  5. Interactive Display:
    • The show method is called on the figure (fig) to display the interactive plot.

This script provides a quick and effective demonstration of using Plotly Express to visualize time-series data, making it suitable for readers interested in creating interactive and visually appealing line plots for financial or currency-related datasets.

Output

Python Code -Example 2

import pandas as pd
import plotly.express as px

runs_scored = pd.read_csv('AusVsInd.csv')
fig = px.line(runs_scored, x='Overs', y=['AUS', 'IND'], markers=True)
fig.update_layout(title='Australia vs India ODI Match', xaxis_title='OVERS', yaxis_title='RUNS', legend_title='Country')

fig.show()

AusVsInd.csv File

The provided Python script utilizes the Plotly Express library to create an interactive line plot comparing the runs scored by Australia (AUS) and India (IND) over a series of overs in an ODI cricket match. Here’s a concise overview:

  1. Data Import:
    • The script uses Pandas to read runs scored data from a CSV file (‘AusVsInd.csv’).
  2. Plotly Express:
    • Plotly Express (px) is employed to create an interactive line plot.
    • The x-axis represents overs (‘Overs’), and the y-axis represents runs scored by Australia and India.
  3. Markers:
    • Markers are added to the plot for each data point to enhance visibility.
  4. Customized Layout:
    • The layout is customized with a title (‘Australia vs India ODI Match’), x-axis label (‘OVERS’), y-axis label (‘RUNS’), and legend title (‘Country’).
  5. Interactive Display:
    • The show method is called on the figure (fig) to display the interactive plot.

This script serves as an excellent example for readers interested in using Plotly Express for comparing and visualizing data, particularly in the context of sports analytics or cricket match statistics. The interactive features make it easy for users to explore the data interactively.

Output

Bar Graph for runs scored every over

#Bar Graph of Runs scored every Over
import pandas as pd
import plotly.express as px

runs_scored = pd.read_csv('AusVsInd.csv')

fig = px.bar(runs_scored, x='Overs', y=['AUS_RPO', 'IND_RPO'], barmode='group')
fig.update_layout(title='Australia vs India ODI Match', xaxis_title='OVERS', yaxis_title='RUNS', legend_title='Country')

fig.show()

The provided Python script uses the Plotly Express library to create an interactive grouped bar graph comparing the runs per over (RPO) scored by Australia (AUS) and India (IND) in an ODI cricket match. Here’s a concise overview:

  1. Data Import:
    • The script uses Pandas to read runs scored data from a CSV file (‘AusVsInd.csv’).
  2. Plotly Express:
    • Plotly Express (px) is employed to create an interactive grouped bar graph.
    • The x-axis represents overs (‘Overs’), and the y-axis represents runs per over scored by Australia and India.
  3. Grouped Bars:
    • Bars are grouped for each over, and the bar mode is set to ‘group’ to display bars side by side.
  4. Customized Layout:
    • The layout is customized with a title (‘Australia vs India ODI Match’), x-axis label (‘OVERS’), y-axis label (‘RUNS’), and legend title (‘Country’).
  5. Interactive Display:
    • The show method is called on the figure (fig) to display the interactive grouped bar graph.

This script serves as an illustrative example for readers interested in using Plotly Express to visualize and compare runs scored per over by different teams in a cricket match. The interactive nature of the graph allows users to explore the data interactively.

Output


Maps using Plotly Libraries

Write a Python program for creating Maps using Plotly Libraries.

Python Code -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/gapminder_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()

In this Python program, we utilize Plotly Express to create an interactive choropleth map visualizing GDP per Capita by country. The dataset used is sourced from Gapminder, providing a comprehensive view of economic indicators globally.

  1. Import Libraries:
    • We start by importing the necessary libraries, including plotly.express for easy and interactive visualizations.
  2. Data Loading:
    • The program fetches data from a CSV file hosted on GitHub using pd.read_csv. The dataset includes information about countries, their ISO codes, and GDP per Capita.
  3. Choropleth Map:
    • The choropleth map is created using px.choropleth.
    • Key parameters include:
      • locations: ISO codes of countries.
      • color: GDP per Capita, determining the color intensity on the map.
      • hover_name: Country names appearing on hover.
      • projection: ‘natural earth’ projection for a global view.
      • title: The title of the map.
  4. Interactive Exploration:
    • The resulting choropleth map is interactive, enabling users to hover over countries to see GDP per Capita values.

Users can explore and compare GDP per Capita across different countries. Darker colors indicate higher GDP per Capita. This program demonstrates the simplicity and power of Plotly Express for creating data-driven visualizations. The choropleth map offers an intuitive way to understand global economic disparities. Feel free to customize the description based on additional details you’d like to highlight or any specific insights you’ve gained from the visualization.

Output

Python Code -Example 2

import json
import numpy as np
import pandas as pd
import plotly.express as px

#Uncomment below lines to render map on your browser
#import plotly.io as pio
#pio.renderers.default = 'browser'

india_states = json.load(open("states_india.geojson", "r"))

df = pd.read_csv("india_census.csv")

state_id_map = {}
for feature in india_states["features"]:
    feature["id"] = feature["properties"]["state_code"]
    state_id_map[feature["properties"]["st_nm"]] = feature["id"]

df = pd.read_csv("india_census.csv")
df["Density"] = df["Density[a]"].apply(lambda x: int(x.split("/")[0].replace(",", "")))
df["id"] = df["State or union territory"].apply(lambda x: state_id_map[x])

#print(df.head())	    
fig = px.choropleth(
    df,
    locations="id",
    geojson=india_states,
    color="Population",
    hover_name="State or union territory",
    hover_data=["Density", "Sex ratio", "Population"],
    title="India Population Statewise",
)
fig.update_geos(fitbounds="locations", visible=False)
fig.show()

CSV File

JSON File

You can obtain the JSON file used in this program by clicking on the button below.

In this Python program, we leverage Plotly Express to create an insightful choropleth map that visualizes key demographic indicators across states and union territories in India. The data is sourced from India’s census, providing a comprehensive overview of population distribution, density, and sex ratio.

Program Overview:

  1. Import Libraries:
    • We begin by importing necessary libraries, including json for handling GeoJSON data, numpy for numerical operations, pandas for data manipulation, and plotly.express for creating interactive visualizations.
  2. Load GeoJSON and Census Data:
    • The GeoJSON file representing Indian states is loaded, and census data is read from a CSV file containing information about population, density, and sex ratio.
  3. Data Preparation:
    • We create a mapping between state names and their corresponding IDs for seamless integration with GeoJSON features.
    • Additional data preprocessing includes converting density values to integers and creating a unique identifier (id) for each state.
  4. Choropleth Map:
    • The choropleth map is generated using px.choropleth. Key parameters include:
      • locations: State IDs for mapping.
      • geojson: GeoJSON data for Indian states.
      • color: Population, determining color intensity on the map.
      • hover_name: State names for hover information.
      • hover_data: Additional information displayed on hover, including density, sex ratio, and population.
      • title: Title of the map.
  5. Interactive Exploration:
    • The resulting choropleth map is interactive, allowing users to hover over states to explore population demographics.

Users can explore and compare population distribution, density, and sex ratio across different states and union territories in India. This program demonstrates the power of Plotly Express for creating meaningful and interactive visualizations. The choropleth map provides valuable insights into the demographic landscape of India.

Output

If you are also looking for other Lab Manuals, head over to my following blog :


Prabodh C P is a faculty in the Dept of CSE SIT, Tumkur and also currently a Research Scholar pursuing PhD in IIT Hyderabad. He conducts online classes for C, C++, Python. For more info call +919392302100

Leave a Reply

Your email address will not be published. Required fields are marked *