Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Data Analytics With Python Laboratory - Lab Manual

The document is a lab manual for a Data Analytics with Python course for B.Tech 5th semester students. It outlines the objectives, experiments, and course outcomes related to using Python, Numpy, Pandas, and Matplotlib for data manipulation and visualization. The manual includes detailed descriptions of various programming tasks and operations to be performed in the lab.

Uploaded by

yeeshandas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Data Analytics With Python Laboratory - Lab Manual

The document is a lab manual for a Data Analytics with Python course for B.Tech 5th semester students. It outlines the objectives, experiments, and course outcomes related to using Python, Numpy, Pandas, and Matplotlib for data manipulation and visualization. The manual includes detailed descriptions of various programming tasks and operations to be performed in the lab.

Uploaded by

yeeshandas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

B.

Tech 5th CSE


Data Analytics with Python Lab Manual

Lab Manual

“Data Analytics with Python


Laboratory”

C022523(022)

BTECH Vth Semester CSE

Department of Computer Science & Engineering

RSR Rungta College Of Engineering &


Technology, Kurud Rd, Kohka, Bhilai,
Chhattisgarh 490024

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Objectives

1. To be able to use Python for handling various data structures for data representation
and manipulation.
2. To be able to use Numpy for data handling.
3. To be able to use Pandas for data processing.
4. To be able to use Matplotlib for visual representation of data.

List of Experiments:
1. Write programs to understand the use ofPython Identifiers, Keywords,
Indentations, Comments in Python, Operators, Membership operator.
2. Write programs to understand the use of Python String, Tuple, List, Set, Dictionary,
File input/output.
3. Write programs to understand the use of Numpy’s Ndarray, Basic Operations,
Indexing, Slicing, and Iterating, Conditions and Boolean Arrays.
4. Write programs to understand the use of Numpy’s Shape Manipulation, Array
Manipulation, Vectorization.
5. Write programs to understand the use of Numpy’s Structured Arrays, Reading and
Writing Array Data on Files.
6. Write programs to understand the use of Pandas Series, Data Frame, Index
Objects, Reindexing, Dropping, Arithmetic and Data Alignment.
7. Write programs to understand the use of Pandas Functions by Element,
Functions by Row or Column,
Statistics Functions, Sorting and Ranking, Correlation and Covariance, “Not a
Number” Data.
8. Write programs to understand the use of Pandas for Reading and Writing Data
using CSV and Textual Files, HTML Files, XML, Microsoft Excel Files.
9. Write programs to understand the use of Matplotlib for Simple Interactive Chart,
Set the Properties of the Plot, matplotlib and NumPy.
10. Write programs to understand the use of Matplotlib for Working with Multiple
Figures and Axes, Adding Text, Adding a Grid, Adding a Legend, Saving the
Charts.
11. Write programs to understand the use of Matplotlib for Working with Line Chart,
Histogram, Bar Chart, Pie Charts.

Course Outcomes [After undergoing the course, students will be able to:]

1. Apply Python for handling various data structures for data representation and
manipulation.
2. ApplyNumpy for data handling.
3. Apply Pandas for data processing.
4. Apply Matplotlib for visual representation of data.

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Python Laboratory

TABLE OF CONTENT

S.NO. DATE NAMEOFTHEEXPERIMENT PAGE SIGNATURE


NO.
Write programs to understand the use ofPython
1 Identifiers, Keywords, Indentations, Comments in
Python, Operators, Membership operator.

Write programs to understand the use of Python String, Tuple,


2 List, Set, Dictionary, File input/output.

Write programs to understand the use of Numpy’s Ndarray,


3 Basic Operations, Indexing, Slicing, and Iterating,
Conditions and Boolean Arrays.

Write programs to understand the use of Numpy’s Shape


4 Manipulation, Array Manipulation, Vectorization.

Write programs to understand the use of Numpy’s


5 Structured Arrays, Reading and Writing Array Data on
Files.

Write programs to understand the use of Pandas Series, Data


6 Frame, Index Objects, Reindexing, Dropping, Arithmetic and
Data Alignment.

Write programs to understand the use of Pandas


7 Functions by Element, Functions by Row or Column,
Statistics Functions, Sorting and Ranking, Correlation and
Covariance, “Not a Number” Data.

Write programs to understand the use of Pandas for


8 Reading and Writing Data using CSV and Textual Files,
HTML Files, XML, Microsoft Excel Files.

Write programs to understand the use of Matplotlib for


9 Simple Interactive Chart, Set the Properties of the Plot,
matplotlib and NumPy.

Write programs to understand the use of Matplotlib for


10 Working with Multiple Figures and Axes, Adding Text,
Adding a Grid, Adding a Legend, Saving the Charts.

Write programs to understand the use of Matplotlib for


11 Working with Line Chart, Histogram, Bar Chart, Pie
Charts.

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Experiment-01
AIM: Write programs to understand the use ofPython Identifiers, Keywords, Indentations,
Comments in Python, Operators, Membership operator.

1.1: Python Identifiers

An identifier is a name used to identify a variable, function, class, or module. It can consist of
letters (A-Z, a-z), digits (0-9), and underscores (_), but it cannot begin with a digit.

# Example of Python Identifiers


# Valid identifiers
my_var = 10
myFunction = "Hello"
_var = 3.14
variable1 = [1, 2, 3]

# Invalid identifiers (will raise an error if uncommented)


# 1variable = 5 # Cannot start with a digit
# def = 10 # Cannot use reserved keywords

print("Valid Identifiers: ", my_var, myFunction, _var, variable1)

Output:

Valid Identifiers: 10 Hello 3.14 [1, 2, 3]

1.2: Python Keywords

Keywords are reserved words that cannot be used as identifiers. These words have special
meaning in Python and are predefined in the language.

# Example of Python Keywords


import keyword

# Display all the keywords in Python


print("Python Keywords: ", keyword.kwlist)

Output:

Python Keywords: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

1.3: Python Indentation

Indentation is critical in Python, as it indicates the block of code belonging to a control structure
(such as loops, functions, classes).

# Example of Python Indentation


RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
def greet(name):
if name:
print("Hello, " + name)
else:
print("Hello, World!")

greet("Alice")
greet("") # This will print "Hello, World!" because the name is empty.

Output:

Hello, Alice
Hello, World!

1.4: Python Comments

Comments are used to explain the code, and they are ignored by the Python interpreter.

# This is a single line comment


def add(a, b):
# This function adds two numbers
return a + b

# Calling the function


result = add(5, 3) # Adding 5 and 3
print("Sum: ", result)

Output:

Sum: 8

1.5: Python Operators

# Arithmetic Operators
a = 10
b=5

print("Arithmetic Operators:")
print(f"a + b = {a + b}") # Addition
print(f"a - b = {a - b}") # Subtraction
print(f"a * b = {a * b}") # Multiplication
print(f"a / b = {a / b}") # Division (float)
print(f"a // b = {a // b}") # Floor division (integer)
print(f"a % b = {a % b}") # Modulus (remainder)
print(f"a ** b = {a ** b}") # Exponentiation

print("\nComparison Operators:")
# Comparison Operators
print(f"a == b: {a == b}") # Equal to
print(f"a != b: {a != b}") # Not equal to
print(f"a > b: {a > b}") # Greater than
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
print(f"a < b: {a < b}") # Less than
print(f"a >= b: {a >= b}") # Greater than or equal to
print(f"a <= b: {a <= b}") # Less than or equal to

print("\nLogical Operators:")
# Logical Operators
x = True
y = False

print(f"x and y = {x and y}") # Logical AND


print(f"x or y = {x or y}") # Logical OR
print(f"not x = {not x}") # Logical NOT

print("\nAssignment Operators:")
# Assignment Operators
c = 20
print(f"c = {c}")
c += 5 # c = c + 5
print(f"c += 5 => c = {c}")
c -= 3 # c = c - 3
print(f"c -= 3 => c = {c}")
c *= 2 # c = c * 2
print(f"c *= 2 => c = {c}")
c /= 4 # c = c / 4
print(f"c /= 4 => c = {c}")

print("\nBitwise Operators:")
# Bitwise Operators
x = 5 # Binary: 0101
y = 3 # Binary: 0011
print(f"x & y = {x & y}") # AND
print(f"x | y = {x | y}") # OR
print(f"x ^ y = {x ^ y}") # XOR
print(f"~x = {~x}") # NOT (two's complement)
print(f"x << 1 = {x << 1}") # Left shift
print(f"x >> 1 = {x >> 1}") # Right shift

print("\nMembership Operators:")
# Membership Operators
lst = [1, 2, 3, 4, 5]
print(f"3 in lst: {3 in lst}") # True, 3 is in the list
print(f"6 not in lst: {6 not in lst}") # True, 6 is not in the list

print("\nIdentity Operators:")
# Identity Operators
a = [1, 2, 3]
b=a
c = [1, 2, 3]
print(f"a is b: {a is b}") # True, both refer to the same object
print(f"a is c: {a is c}") # False, a and c are different objects
print(f"a is not c: {a is not c}") # True, a and c are not the same object

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Output:

Arithmetic Operators:
a + b = 15
a-b=5
a * b = 50
a / b = 2.0
a // b = 2
a%b=0
a ** b = 100000

Comparison Operators:
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False

Logical Operators:
x and y = False
x or y = True
not x = False

Assignment Operators:
c = 20
c += 5 => c = 25
c -= 3 => c = 22
c *= 2 => c = 44
c /= 4 => c = 11.0

Bitwise Operators:
x&y=1
x|y=7
x^y=6
~x = -6
x << 1 = 10
x >> 1 = 2

Membership Operators:
3 in lst: True
6 not in lst: True

Identity Operators:
a is b: True
a is c: False
a is not c: True

1.6: Python Membership Operators

The membership operators in Python are in and not in, which are used to test whether a value is
in a sequence (such as a list, tuple, or string).

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
python

# Example of Membership Operators


lst = [1, 2, 3, 4, 5]

# Check if an element is in the list


print(3 in lst) # True, because 3 is in the list
print(6 not in lst) # True, because 6 is not in the list

# Check membership in a string


str = "Hello, Python!"
print("Python" in str) # True, because "Python" is part of the string
print("Java" not in str) # True, because "Java" is not part of the string

Output:

True
True
True
True

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

Experiment-02
AIM : Write programs to understand the use of Python String, Tuple, List, Set, Dictionary,
File input/output.

2.1: String Operations

# Python String Example


string = "Hello, Python!"
print("Original String:", string)

# Slicing a string
substring = string[7:13]
print("Sliced String:", substring)

# String length
length = len(string)
print("Length of String:", length)

# String reversal
reversed_string = string[::-1]
print("Reversed String:", reversed_string)

# String concatenation
new_string = string + " How are you?"
print("Concatenated String:", new_string)

# String split
split_string = string.split(",")
print("Split String:", split_string)

Output:

Original String: Hello, Python!

Sliced String: Python


Length of String: 15
Reversed String: !nohtyP ,olleH
Concatenated String: Hello, Python! How are you?
Split String: ['Hello', ' Python!']

2.2: Tuple Operations

# Python Tuple Example


my_tuple = (1, 2, 3, 4, 5)
print("Original Tuple:", my_tuple)
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual

# Accessing elements
first_element = my_tuple[0]
print("First Element:", first_element)

# Tuple slicing
sub_tuple = my_tuple[1:4]
print("Sliced Tuple:", sub_tuple)

# Tuple length
tuple_length = len(my_tuple)
print("Length of Tuple:", tuple_length)

# Concatenating tuples
new_tuple = my_tuple + (6, 7)
print("Concatenated Tuple:", new_tuple)

Output:

Original Tuple: (1, 2, 3, 4, 5)

First Element: 1
Sliced Tuple: (2, 3, 4)
Length of Tuple: 5
Concatenated Tuple: (1, 2, 3, 4, 5, 6, 7)

2.3: List Operations

# Python List Example


my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)

# Accessing elements
third_element = my_list[2]
print("Third Element:", third_element)

# Modifying a list element


my_list[1] = 25
print("Modified List:", my_list)

# List slicing
sub_list = my_list[2:4]
print("Sliced List:", sub_list)

# Adding elements
my_list.append(60)
print("List After Append:", my_list)

# Removing an element
my_list.remove(40)
print("List After Remove:", my_list)

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
# List length
list_length = len(my_list)
print("Length of List:", list_length)

Output:

Original List: [10, 20, 30, 40, 50]


Third Element: 30
Modified List: [10, 25, 30, 40, 50]
Sliced List: [30, 40]
List After Append: [10, 25, 30, 40, 50, 60]
List After Remove: [10, 25, 30, 50, 60]
Length of List: 5

2.4 Set Operations

# Python Set Example


my_set = {1, 2, 3, 4, 5}
print("Original Set:", my_set)

# Adding an element
my_set.add(6)
print("Set After Add:", my_set)

# Removing an element
my_set.remove(3)
print("Set After Remove:", my_set)

# Set length
set_length = len(my_set)
print("Length of Set:", set_length)

# Set union
set2 = {4, 5, 6, 7, 8}
union_set = my_set | set2
print("Union of Sets:", union_set)

# Set intersection
intersection_set = my_set & set2
print("Intersection of Sets:", intersection_set)

Output:

Original Set: {1, 2, 3, 4, 5}


Set After Add: {1, 2, 3, 4, 5, 6}
Set After Remove: {1, 2, 4, 5, 6}
Length of Set: 5
Union of Sets: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection of Sets: {4, 5, 6}

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
2.5 Dictionary Operations

# Python Dictionary Example


my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print("Original Dictionary:", my_dict)

# Accessing a value by key


name = my_dict["name"]
print("Name:", name)

# Modifying a value
my_dict["age"] = 26
print("Modified Dictionary:", my_dict)

# Adding a new key-value pair


my_dict["job"] = "Engineer"
print("Dictionary After Adding Job:", my_dict)

# Removing a key-value pair


del my_dict["city"]
print("Dictionary After Removing City:", my_dict)

# Dictionary keys and values


keys = my_dict.keys()
values = my_dict.values()
print("Dictionary Keys:", keys)
print("Dictionary Values:", values)

Output:

Original Dictionary: {'name': 'Alice', 'age': 25, 'city': 'New York'}


Name: Alice
Modified Dictionary: {'name': 'Alice', 'age': 26, 'city': 'New York'}
Dictionary After Adding Job: {'name': 'Alice', 'age': 26, 'city': 'New York', 'job': 'Engineer'}
Dictionary After Removing City: {'name': 'Alice', 'age': 26, 'job': 'Engineer'}
Dictionary Keys: dict_keys(['name', 'age', 'job'])
Dictionary Values: dict_values(['Alice', 26, 'Engineer'])

2.6 File Input/Output Operations

# Python File Input/Output Example

# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, this is a test file.\n")
file.write("We are writing to it using Python.\n")
print("Data written to file.")

# Reading from a file


with open("example.txt", "r") as file:
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
content = file.read()
print("Data read from file:")
print(content)

Output:

Data written to file.Data read from file:

Hello, this is a test file.


We are writing to it using Python.

Experiment-03

AIM: Write programs to understand the use of Numpy’s Ndarray, Basic Operations,
Indexing, Slicing, and Iterating, Conditions and Boolean Arrays.

3.1 Numpy's ndarray

First, let's import NumPy and create an ndarray.

import numpy as np

# Creating a simple 1D ndarray


array_1d = np.array([1, 2, 3, 4, 5])
print("1D ndarray:", array_1d)

# Creating a 2D ndarray (matrix)


array_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("2D ndarray:\n", array_2d)

# Creating a 3D ndarray
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("3D ndarray:\n", array_3d)

Output:

1D ndarray: [1 2 3 4 5]
2D ndarray:
[[1 2 3]
[4 5 6]]
3D ndarray:
[[[1 2]
[3 4]]

[[5 6]
[7 8]]]
3.2 Basic Operations with Ndarray

Now, let's perform some basic mathematical operations on NumPy arrays.


RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
python

# Basic arithmetic operations


array_a = np.array([1, 2, 3, 4])
array_b = np.array([4, 3, 2, 1])

# Addition
sum_array = array_a + array_b
print("Array addition:", sum_array)

# Subtraction
diff_array = array_a - array_b
print("Array subtraction:", diff_array)

# Multiplication
prod_array = array_a * array_b
print("Array multiplication:", prod_array)

# Division
div_array = array_a / array_b
print("Array division:", div_array)

# Scalar multiplication
scalar_prod = array_a * 3
print("Scalar multiplication:", scalar_prod)

Output:

Array addition: [5 5 5 5]
Array subtraction: [-3 -1 1 3]
Array multiplication: [4 6 6 4]
Array division: [0.25 0.66666667 1.5 4. ]
Scalar multiplication: [3 6 9 12]

3.3 Indexing and Slicing

Indexing and slicing in NumPy is similar to Python lists, but with more flexibility.

python

# Indexing in 1D ndarray
array_1d = np.array([10, 20, 30, 40, 50])
print("Element at index 2:", array_1d[2])

# Slicing in 1D ndarray
slice_1d = array_1d[1:4] # Get elements from index 1 to 3
print("Sliced 1D array:", slice_1d)

# Indexing in 2D ndarray
array_2d = np.array([[10, 20, 30], [40, 50, 60]])
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
print("Element at (1,2) in 2D array:", array_2d[1, 2])

# Slicing in 2D ndarray
slice_2d = array_2d[:, 1:3] # Select all rows, columns 1 and 2
print("Sliced 2D array:\n", slice_2d)

Output:

lua

Element at index 2: 30
Sliced 1D array: [20 30 40]
Element at (1,2) in 2D array: 60
Sliced 2D array:
[[20 30]
[50 60]]

3.4 Iterating Over Ndarray

We can iterate over elements of a NumPy array just like a list. However, NumPy provides
efficient ways to work with elements.

python

# Iterating over a 1D ndarray


array_1d = np.array([1, 2, 3, 4, 5])
for element in array_1d:
print("Element:", element)

# Iterating over a 2D ndarray


array_2d = np.array([[1, 2, 3], [4, 5, 6]])
for row in array_2d:
print("Row:", row)
for element in row:
print("Element in row:", element)

Output:

mathematica

Element: 1
Element: 2
Element: 3
Element: 4
Element: 5
Row: [1 2 3]
Element in row: 1
Element in row: 2
Element in row: 3
Row: [4 5 6]
Element in row: 4
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
Element in row: 5
Element in row: 6

3.5 Conditions and Boolean Arrays

NumPy allows efficient element-wise comparisons, and you can use Boolean arrays for filtering
and conditional operations.

python

# Creating an array
array = np.array([10, 20, 30, 40, 50])

# Condition to find elements greater than 25


condition = array > 25
print("Condition array (elements > 25):", condition)

# Using the condition to filter the original array


filtered_array = array[condition]
print("Filtered Array (elements > 25):", filtered_array)

# More complex conditions (elements between 20 and 40)


complex_condition = (array > 20) & (array < 40)
filtered_complex_array = array[complex_condition]
print("Filtered Array (elements between 20 and 40):", filtered_complex_array)

# Using `np.where` for conditional replacement


result = np.where(array > 30, 'Greater', 'Lesser or Equal')
print("Conditional Replacement:", result)

Output:

Condition array (elements > 25): [False False True True True]
Filtered Array (elements > 25): [30 40 50]
Filtered Array (elements between 20 and 40): [30]
Conditional Replacement: ['Lesser or Equal' 'Lesser or Equal' 'Greater' 'Greater' 'Great

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

Experiment-04

AIM: Write programs to understand the use of Numpy’s Shape Manipulation, Array
Manipulation, Vectorization.

4.1 Shape Manipulation in NumPy

Shape manipulation involves reshaping arrays, adding/removing dimensions, and modifying the
shape of arrays without changing their data.

python

import numpy as np

# Create a 1D array
array_1d = np.array([1, 2, 3, 4, 5, 6])
print("Original 1D array:", array_1d)

# Reshaping the 1D array into a 2D array (2 rows, 3 columns)


array_2d = array_1d.reshape(2, 3)
print("Reshaped into 2D array:\n", array_2d)

# Flattening a 2D array back to 1D


flattened_array = array_2d.flatten()
print("Flattened array:", flattened_array)

# Resizing the array (it will modify the shape in place)


array_1d.resize(3, 2) # Resizing to 3 rows and 2 columns
print("Resized array (in-place):\n", array_1d)

# Transpose a 2D array (swap rows and columns)


transposed_array = array_2d.T
print("Transposed array:\n", transposed_array)
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual

# Adding an extra dimension (1D to 2D)


expanded_array = np.expand_dims(array_1d, axis=0) # Adding a new axis at the beginning
print("Expanded array (added dimension):\n", expanded_array)

Output:

Original 1D array: [1 2 3 4 5 6]
Reshaped into 2D array:
[[1 2 3]
[4 5 6]]
Flattened array: [1 2 3 4 5 6]
Resized array (in-place):
[[1 2]
[3 4]
[5 6]]
Transposed array:
[[1 4]
[2 5]
[3 6]]
Expanded array (added dimension):
[[1 2 3 4 5 6]]

4.2 Array Manipulation in NumPy

Array manipulation includes operations like stacking, splitting, and combining arrays.

python

# Create some arrays


array_1 = np.array([1, 2, 3])
array_2 = np.array([4, 5, 6])
array_3 = np.array([7, 8, 9])

print("Array 1:", array_1)


print("Array 2:", array_2)
print("Array 3:", array_3)

# Stacking arrays vertically


vertical_stack = np.vstack((array_1, array_2, array_3))
print("Vertical Stack:\n", vertical_stack)

# Stacking arrays horizontally


horizontal_stack = np.hstack((array_1, array_2, array_3))
print("Horizontal Stack:", horizontal_stack)

# Splitting an array into multiple sub-arrays


split_array = np.split(horizontal_stack, 3) # Split into 3 parts
print("Split array:", split_array)

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
# Combining arrays along a new axis
combined = np.concatenate((array_1[:, np.newaxis], array_2[:, np.newaxis], array_3[:,
np.newaxis]), axis=1)
print("Combined array (along new axis):\n", combined)

Output:

Array 1: [1 2 3]
Array 2: [4 5 6]
Array 3: [7 8 9]
Vertical Stack:
[[1 2 3]
[4 5 6]
[7 8 9]]
Horizontal Stack: [1 2 3 4 5 6 7 8 9]
Split array: [array([1, 2]), array([3, 4]), array([5, 6, 7, 8, 9])]
Combined array (along new axis):
[[1 4 7]
[2 5 8]
[3 6 9]]

4.3 Vectorization in NumPy

Vectorization allows us to perform operations on entire arrays or large datasets without needing
explicit loops. This results in faster code.

# Example of vectorized operations (no loops needed)


array_x = np.array([1, 2, 3, 4])
array_y = np.array([5, 6, 7, 8])

# Element-wise addition (vectorization)


sum_result = array_x + array_y
print("Element-wise addition:", sum_result)

# Element-wise multiplication
prod_result = array_x * array_y
print("Element-wise multiplication:", prod_result)

# Scalar operation on an entire array


scalar_result = array_x * 10
print("Scalar multiplication (array_x * 10):", scalar_result)

# Applying a mathematical function element-wise (sin, exp)


sin_result = np.sin(array_x)
exp_result = np.exp(array_y)
print("Sine of array_x:", sin_result)
print("Exponential of array_y:", exp_result)

# Example of conditional operation using vectorization


condition = array_x > 2
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
print("Condition (array_x > 2):", condition)

# Applying conditional operations using np.where (vectorization)


result = np.where(array_x > 2, array_x * 2, array_x)
print("Conditional operation (multiply by 2 if > 2):", result)

Output

Element-wise addition: [ 6 8 10 12]


Element-wise multiplication: [ 5 12 21 32]
Scalar multiplication (array_x * 10): [10 20 30 40]
Sine of array_x: [0.84147098 0.90929743 0.14112001 -0.7568025 ]
Exponential of array_y: [148.4131591 403.42879349 1096.63315843 2980.95798704]
Condition (array_x > 2): [False False True True]
Conditional operation (multiply by 2 if > 2): [ 1 2 6 8]

Experiment-05

AIM: Write programs to understand the use of Numpy’s Structured Arrays, Reading and
Writing Array Data on Files.

Program 5.1: Understanding the use of NumPy's Structured Arrays

Structured arrays allow you to store heterogeneous data types in an efficient manner, similar to
a database table or a spreadsheet.

Example:

import numpy as np

# Define a structured array with fields: name (string), age (int), and height (float)
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]

# Create a structured array


data = np.array([('Alice', 25, 5.5), ('Bob', 30, 5.9), ('Charlie', 35, 6.1)], dtype=dtype)

# Print the structured array


print("Structured Array:")
print(data)

# Accessing fields in the structured array


print("\nNames:")
print(data['name'])

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
print("\nAges:")
print(data['age'])

print("\nHeights:")
print(data['height'])

Output:
less

Structured Array:
[('Alice', 25, 5.5) ('Bob', 30, 5.9) ('Charlie', 35, 6.1)]

Names:
['Alice' 'Bob' 'Charlie']

Ages:
[25 30 35]

Heights:
[5.5 5.9 6.1]

Program 5.2: Reading and Writing Array Data to Files

NumPy provides functions like np.save(), np.load(), np.savetxt(), and np.loadtxt() to read and
write arrays to files. We will show both binary and text file operations.

Example:

import numpy as np

# Create a simple NumPy array


array_data = np.array([1, 2, 3, 4, 5])

# Writing to a binary file using np.save


np.save('array_data.npy', array_data)

# Writing to a text file using np.savetxt


np.savetxt('array_data.txt', array_data)

# Reading from the binary file using np.load


loaded_array_bin = np.load('array_data.npy')

# Reading from the text file using np.loadtxt


loaded_array_txt = np.loadtxt('array_data.txt')

# Print the loaded data from both files


print("\nLoaded data from binary file:")
print(loaded_array_bin)

print("\nLoaded data from text file:")


print(loaded_array_txt)

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Output:
Loaded data from binary file:
[1 2 3 4 5]

Loaded data from text file:


[1. 2. 3. 4. 5.]

Experiment-06

AIM: Write programs to understand the use of Pandas Series, Data Frame, Index
Objects, Reindexing, Dropping, Arithmetic and Data Alignment.

Program 6.1: Understanding the use of Pandas Series

Pandas Series is a one-dimensional labeled array that can hold any data type. It can be created
from lists, NumPy arrays, or dictionaries.

Example:
python

import pandas as pd

# Create a Pandas Series from a list


data = [10, 20, 30, 40, 50]
series = pd.Series(data)

# Print the Series


print("Pandas Series:")
print(series)

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
# Create a Pandas Series with custom index
custom_index = ['a', 'b', 'c', 'd', 'e']
series_custom = pd.Series(data, index=custom_index)

# Print the Series with custom index


print("\nPandas Series with Custom Index:")
print(series_custom)

Output:

Pandas Series:
0 10
1 20
2 30
3 40
4 50
dtype: int64

Pandas Series with Custom Index:


a 10
b 20
c 30
d 40
e 50
dtype: int64

Program 6.2: Pandas DataFrame

A DataFrame is a two-dimensional table of data with labeled axes (rows and columns). It is
similar to a database table, Excel spreadsheet, or dictionary of Series objects.

Example:
import pandas as pd

# Create a DataFrame from a dictionary


data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Height': [5.5, 5.9, 6.1]}

df = pd.DataFrame(data)

# Print the DataFrame


print("Pandas DataFrame:")
print(df)

Output:
Pandas DataFrame:
Name Age Height
0 Alice 25 5.5
1 Bob 30 5.9
2 Charlie 35 6.1

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Program 6.3: Index Objects

Pandas Index objects are immutable and are used to label the axes of DataFrames and Series.
You can create your own Index objects and assign them to a DataFrame or Series.

Example:
import pandas as pd

# Create a DataFrame with custom Index


index = pd.Index(['a', 'b', 'c', 'd', 'e'])
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Age': [25, 30, 35, 40, 45]}

df = pd.DataFrame(data, index=index)

# Print the DataFrame with custom Index


print("DataFrame with custom Index:")
print(df)

# Accessing the Index


print("\nIndex Object:")
print(df.index)

Output:
DataFrame with custom Index:
Name Age
a Alice 25
b Bob 30
c Charlie 35
d David 40
e Eva 45

Index Object:
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

Program 6.4: Reindexing

Reindexing allows you to conform a DataFrame to a new index with optional filling logic.

Example:

import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)

# New index
new_index = ['a', 'b', 'c', 'd']

# Reindexing the DataFrame


RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
df_reindexed = df.reindex(new_index)

# Print the reindexed DataFrame


print("Reindexed DataFrame:")
print(df_reindexed)

Output:

Reindexed DataFrame:
Name Age
a Alice 25.0
b Bob 30.0
c Charlie 35.0
d NaN NaN

Program 6.5: Dropping Elements

Pandas allows you to drop rows or columns from a DataFrame using the drop() method.

Example:
import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Height': [5.5, 5.9, 6.1]}

df = pd.DataFrame(data)

# Drop the 'Height' column


df_dropped_column = df.drop('Height', axis=1)

# Drop the second row (index 1)


df_dropped_row = df.drop(1, axis=0)

# Print the DataFrame after dropping column and row


print("DataFrame after dropping 'Height' column:")
print(df_dropped_column)

print("\nDataFrame after dropping row with index 1:")


print(df_dropped_row)

Output:

DataFrame after dropping 'Height' column:


Name Age
0 Alice 25
1 Bob 30
2 Charlie 35

DataFrame after dropping row with index 1:


Name Age Height

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
0 Alice 25 5.5
2 Charlie 35 6.1

Program 6.6: Arithmetic and Data Alignment

Pandas automatically aligns data when performing arithmetic operations between Series and
DataFrames, taking care of missing values.

Example:

import pandas as pd

# Create two Series with different indexes


series1 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
series2 = pd.Series([4, 5, 6], index=['b', 'c', 'd'])

# Arithmetic operation (addition)


sum_series = series1 + series2

# Print the result of the operation


print("Result of Arithmetic Operation (Addition):")
print(sum_series)

Output:
r

Result of Arithmetic Operation (Addition):


a NaN
b 6.0
c 8.0
d NaN
dtype: float64

Experiment-07

AIM: Write programs to understand the use of Pandas Functions by Element,


Functions by Row or Column, Statistics Functions, Sorting and Ranking, Correlation and
Covariance, “Not a Number” Data.

Program 7.1: Functions by Element in Pandas

Pandas provides vectorized operations to apply functions element-wise over Series and
DataFrames.

Example: Apply Element-wise Function to a Pandas Series

import pandas as pd

# Create a Pandas Series


data = [10, 20, 30, 40, 50]
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
series = pd.Series(data)

# Apply a function element-wise (e.g., square each element)


squared_series = series.apply(lambda x: x ** 2)

# Print the result


print("Original Series:")
print(series)

print("\nSquared Series (Function by Element):")


print(squared_series)

Output:
yaml

Original Series:
0 10
1 20
2 30
3 40
4 50
dtype: int64

Squared Series (Function by Element):


0 100
1 400
2 900
3 1600
4 2500
dtype: int64

Program 7.2: Functions by Row or Column in Pandas

You can apply functions across rows or columns using the apply() method for DataFrames. This
is useful for aggregating data along a specific axis (rows or columns).

Example: Apply Function by Column (Axis=0)

import pandas as pd

# Create a DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)

# Apply a function to each column (sum)


column_sum = df.apply(lambda x: x.sum(), axis=0)

# Apply a function to each row (sum)


row_sum = df.apply(lambda x: x.sum(), axis=1)

# Print the results


print("Original DataFrame:")
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
print(df)

print("\nSum of each column:")


print(column_sum)

print("\nSum of each row:")


print(row_sum)

Output:
Original DataFrame:
A B C
0 1 4 7
1 2 5 8
2 3 6 9

Sum of each column:


A 6
B 15
C 24
dtype: int64

Sum of each row:


0 12
1 15
2 18
dtype: int64

Program 7.3: Statistics Functions in Pandas

Pandas provides built-in methods to calculate common statistical measures on DataFrames and
Series.

Example: Calculate Mean, Median, Standard Deviation, etc.

import pandas as pd
# Create a DataFrame
data = {'A': [10, 20, 30, 40, 50], 'B': [15, 25, 35, 45, 55]}
df = pd.DataFrame(data)

# Calculate statistics functions


mean_values = df.mean()
median_values = df.median()
std_dev = df.std()

# Print the results


print("Original DataFrame:")
print(df)

print("\nMean of each column:")


print(mean_values)

print("\nMedian of each column:")


RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
print(median_values)

print("\nStandard Deviation of each column:")


print(std_dev)

Output:

Original DataFrame:
A B
0 10 15
1 20 25
2 30 35
3 40 45
4 50 55

Mean of each column:


A 30.0
B 35.0
dtype: float64

Median of each column:


A 30.0
B 35.0
dtype: float64

Standard Deviation of each column:


A 15.811388
B 15.811388
dtype: float64

Program 7.4: Sorting and Ranking in Pandas

Pandas provides functions to sort and rank data by index or by column values.

Example: Sorting DataFrame and Ranking Data

import pandas as pd

# Create a DataFrame
data = {'A': [50, 30, 20, 40, 10], 'B': [55, 35, 25, 45, 15]}
df = pd.DataFrame(data)

# Sort the DataFrame by column 'A' in descending order


sorted_df = df.sort_values(by='A', ascending=False)

# Rank the DataFrame by column 'B'


ranked_df = df['B'].rank()

# Print the results


print("Original DataFrame:")
print(df)

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
print("\nDataFrame sorted by column 'A' in descending order:")
print(sorted_df)

print("\nRanking of column 'B':")


print(ranked_df)

Output:

Original DataFrame:
A B
0 50 55
1 30 35
2 20 25
3 40 45
4 10 15

DataFrame sorted by column 'A' in descending order:


A B
0 50 55
3 40 45
1 30 35
2 20 25
4 10 15

Ranking of column 'B':


0 1.0
1 3.0
2 4.0
3 2.0
4 5.0
Name: B, dtype: float64

Program 7.5: Correlation and Covariance in Pandas

Pandas allows you to calculate correlation and covariance between columns of a DataFrame.

Example: Calculate Correlation and Covariance


python

import pandas as pd

# Create a DataFrame
data = {'A': [10, 20, 30, 40, 50], 'B': [15, 25, 35, 45, 55]}
df = pd.DataFrame(data)

# Calculate correlation
correlation = df.corr()

# Calculate covariance
covariance = df.cov()

# Print the results


RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
print("Original DataFrame:")
print(df)

print("\nCorrelation Matrix:")
print(correlation)

print("\nCovariance Matrix:")
print(covariance)

Output:

Original DataFrame:
A B
0 10 15
1 20 25
2 30 35
3 40 45
4 50 55

Correlation Matrix:
A B
A 1.0 1.0
B 1.0 1.0

Covariance Matrix:
A B
A 250.0 250.0
B 250.0 250.0

Program 7.6: "Not a Number" (NaN) Data Handling in Pandas

Pandas provides support for handling missing data represented by NaN.

Example: Handle NaN Data in Pandas


python

import pandas as pd
import numpy as np

# Create a DataFrame with NaN values


data = {'A': [10, 20, np.nan, 40, 50], 'B': [np.nan, 25, 35, 45, 55]}
df = pd.DataFrame(data)

# Fill NaN with a specified value (e.g., 0)


df_filled = df.fillna(0)

# Drop rows with NaN values


df_dropped = df.dropna()

# Print the results


print("Original DataFrame with NaN values:")
print(df)
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual

print("\nDataFrame with NaN values filled with 0:")


print(df_filled)

print("\nDataFrame with rows containing NaN values dropped:")


print(df_dropped)

Output:

Original DataFrame with NaN values:


A B
0 10.0 NaN
1 20.0 25.0
2 NaN 35.0
3 40.0 45.0
4 50.0 55.0

DataFrame with NaN values filled with 0:


A B
0 10.0 0.0
1 20.0 25.0
2 0.0 35.0
3 40.0 45.0
4 50.0 55.0

DataFrame with rows containing NaN values dropped:


A B
1 20.0 25.0
3 40.0 45.0
4 50.0 55.0

Experiment-08

AIM: Write programs to understand the use of Pandas for Reading and Writing Data
using CSV and Textual Files, HTML Files, XML, Microsoft Excel Files.

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Program 8.1: Reading and Writing Data using CSV and Textual Files

Pandas provides functions like read_csv() and to_csv() to read and write CSV files. Similarly,
you can work with text files using read_csv() for textual data.

Example: Reading and Writing CSV and Textual Files

import pandas as pd

# Sample data to create a DataFrame


data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Height': [5.5, 5.9, 6.1]}
df = pd.DataFrame(data)

# Writing DataFrame to CSV file


df.to_csv('data.csv', index=False)

# Writing DataFrame to a Text file


df.to_csv('data.txt', index=False, sep='\t') # Tab-separated file

# Reading CSV file


df_csv = pd.read_csv('data.csv')

# Reading Text file


df_txt = pd.read_csv('data.txt', sep='\t')

# Print the results


print("Data read from CSV file:")
print(df_csv)

print("\nData read from Text file:")


print(df_txt)

Output:

Data read from CSV file:


Name Age Height
0 Alice 25 5.5
1 Bob 30 5.9
2 Charlie 35 6.1

Data read from Text file:


Name Age Height
0 Alice 25 5.5
1 Bob 30 5.9
2 Charlie 35 6.1
In this example:

 The to_csv() method writes the DataFrame to a CSV file.


 The read_csv() method is used to read the data back from the file.
 The sep='\t' option is used to read tab-separated text files.

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Program 8.2: Reading and Writing HTML Files

Pandas also supports reading and writing HTML files using read_html() and to_html().

Example: Reading and Writing HTML Files

import pandas as pd

# Sample data to create a DataFrame


data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Height': [5.5, 5.9, 6.1]}
df = pd.DataFrame(data)

# Writing DataFrame to HTML file


df.to_html('data.html', index=False)

# Reading HTML file (note: returns a list of DataFrames if multiple tables are present)
df_html = pd.read_html('data.html')[0]

# Print the results


print("Data read from HTML file:")
print(df_html)

Output:

Data read from HTML file:


Name Age Height
0 Alice 25 5.5
1 Bob 30 5.9
2 Charlie 35 6.1

In this example:

 The to_html() method writes the DataFrame to an HTML file.


 The read_html() method is used to read the table(s) from the HTML file. Since there is
only one table in the file, we access it using [0].

Program 8.3: Reading and Writing XML Files

Pandas can also handle XML files using the read_xml() and to_xml() functions. The
xml.etree.ElementTree library is used as a default parser.

Example: Reading and Writing XML Files


import pandas as pd

# Sample data to create a DataFrame


data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Height': [5.5, 5.9, 6.1]}
df = pd.DataFrame(data)

# Writing DataFrame to XML file


df.to_xml('data.xml', root_name='People', row_name='Person')

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
# Reading XML file
df_xml = pd.read_xml('data.xml')

# Print the results


print("Data read from XML file:")
print(df_xml)

Output:
Data read from XML file:
Name Age Height
0 Alice 25 5.5
1 Bob 30 5.9
2 Charlie 35 6.1

In this example:

 The to_xml() function writes the DataFrame to an XML file.


 The read_xml() function reads the XML file back into a DataFrame.

Program 8.4: Reading and Writing Microsoft Excel Files

Pandas supports reading and writing Excel files using read_excel() and to_excel(). You need to
install openpyxl or xlrd for handling Excel files.

Example: Reading and Writing Excel Files


import pandas as pd

# Sample data to create a DataFrame


data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Height': [5.5, 5.9, 6.1]}
df = pd.DataFrame(data)

# Writing DataFrame to an Excel file


df.to_excel('data.xlsx', index=False)

# Reading Excel file


df_excel = pd.read_excel('data.xlsx')

# Print the results


print("Data read from Excel file:")
print(df_excel)

Output:
Data read from Excel file:
Name Age Height
0 Alice 25 5.5
1 Bob 30 5.9
2 Charlie 35 6.1

Experiment-09
RSR Rungta College of Engineering & Technology Bhilai
B.Tech 5th CSE
Data Analytics with Python Lab Manual
AIM: Write programs to understand the use of Matplotlib for Simple Interactive Chart,
Set the Properties of the Plot, matplotlib and NumPy.

9.1 A Simple Interactive Chart


To start, install matplotlib if you haven't already:
pip install matplotlib
Basic Example: A Line Chart
import matplotlib.pyplot as plt #
Sample data

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Creating a basic line plot
plt.plot(x, y, marker='o') # 'marker' adds points on the line
plt.title("Simple Interactive Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

This chart allows you to plot values and provides a starting point for adding moreinteractive
elements.
9.2 Set the Properties of the Plot
You can set properties such as line style, color, and marker to make the chart morereadable.
import matplotlib.pyplot as plt
plt.plot(x, y, color='green', linestyle='--', marker='o', markersize=8, linewidth=2)
plt.title("Customized Line Chart", fontsize=14, color='blue')
plt.xlabel("X-axis", fontsize=12)
plt.ylabel("Y-axis", fontsize=12)
plt.grid(True) # Adds a grid for better readabilityplt.show()

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

Explanation:
9.3 color='green': Sets the line color.
9.4 linestyle='--': Makes the line dashed.
9.5 marker='o': Adds markers at each data point.
9.6 markersize=8: Sets marker size.
9.7 linewidth=2: Sets line thickness.
9.8 fontsize and color adjust the axis and title fonts.
9.3 Matplotlib and NumPy
NumPy integrates seamlessly with matplotlib, enabling mathematical operationsdirectly on
datasets.
import numpy as np
import matplotlib.pyplot as plt
# Generating data with numpy
x = np.linspace(0, 10, 100) # 100 points between 0 and 10y =
np.sin(x)
plt.plot(x, y, label='sin(x)')
plt.title("Sinusoidal Function")
plt.xlabel("X values")
plt.ylabel("sin(X)")
plt.legend() plt.show()

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

Experiment-10
AIM: Write programs to understand the use of Matplotlib for Working with Multiple
Figures and Axes, Adding Text, Adding a Grid, Adding a Legend, Saving the Charts.

10.1 Working with Multiple Figures and Axes


In data analysis, you may want to plot multiple charts on the same figure or inseparate
figures.
Multiple Figures

import matplotlib.pyplot as plt

#First figure
plt.figure(1)

plt.plot(x, y, 'r') # Red line for sin(x)plt.title("First


Figure")
# Second figure
plt.figure(2)
plt.plot(x, np.cos(x), 'b') # Blue line for cos(x)plt.title("Second
Figure")
plt.show()

10.2 Multiple Axes in One Figure


import matplotlib.pyplot as plt
fig, axs = plt.subplots(2) # 2 rowsaxs[0].plot(x, np.sin(x), 'r')
axs[0].set_title("Sine Wave") axs[1].plot(x, np.cos(x), 'b')
axs[1].set_title("Cosine Wave")
plt.tight_layout() # Adjust spacing between plotsplt.show()

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

10.3Adding Text
import matplotlib.pyplot as plt

Adding text annotations helps highlight key points in the data.


plt.plot(x, y, 'g')
plt.title("Annotated Sinusoidal Chart")
plt.text(5, 0, "Middle Point", fontsize=12, color='purple') # Add text at
coordinates (5,0)
plt.show()

Explanation:
10.2 plt.text(x, y, "text"): Adds a text label at the specified (x, y) coordinates.

10.4 Adding a Grid


Adding a grid improves readability, especially for larger datasets.
import matplotlib.pyplot as plt
plt.plot(x, y, 'b')
plt.title("Chart with Grid")
plt.xlabel("X-axis")

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
plt.ylabel("Y-axis")
plt.grid(True ) # Add grid to the chartplt.show()

10.5 Adding a Legend


A legend identifies different series on a chart, useful when plotting multiple lines.
import matplotlib.pyplot as plt
plt.plot(x, y, label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)", linestyle='--')
plt.title("Sine and Cosine Waves")

plt.xlabel("X values")
plt.ylabel("Y values")
plt.legend(loc="upper right") # Position the legendplt.show()

Explanation:
10.6 label="text" assigns a label to each line.
10.7 plt.legend(loc="upper right") places the legend at the top right.

11Saving the Charts


You can save plots as images using savefig.plt.
import matplotlib.pyplot as plt
plot(x, y, 'b')

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
plt.title("Saved Chart Example")
plt.savefig("saved_chart.png", dpi=300, format='png') # Save as PNG with highresolution
plt.show()

Explanation:
11.4 dpi=300 specifies the resolution.
11.5 format='png' saves as a PNG file.

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
Experiment-11
AIM: Write programs to understand the use of Matplotlib for Working with Line Chart,
Histogram, Bar Chart, Pie Charts.

11.1: Line Chart


Line charts are useful for showing trends over continuous data.
import matplotlib.pyplot as plt
plt.plot(x, y, color='blue', linestyle='-', marker='o') plt.title("Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

11.2: Histogram
Histograms are used to show the distribution of a dataset.
import matplotlib.pyplot as plt
data = np.random.randn(1000) # Random data plt.hist(data, bins=30,
color='green', alpha=0.7) plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual

Explanation:
 bins=30: Divides the data range into 30 intervals.
 alpha=0.7: Sets transparency.

11.3: Bar Chart


Bar charts are used to compare categories.

import matplotlib.pyplot as plt


categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 10] plt.bar(categories, values,
color='purple')plt.title("Bar Chart")
plt.xlabel("Categories") plt.ylabel("Values")
plt.show()

11.4: Pie Chart


Pie charts are useful for showing the proportion of categories in a dataset.

RSR Rungta College of Engineering & Technology Bhilai


B.Tech 5th CSE
Data Analytics with Python Lab Manual
import matplotlib.pyplot as plt
sizes = [15, 30, 45, 10]
labels = ['Category A', 'Category B', 'Category C', 'Category D']colors = ['gold',
'yellowgreen', 'lightcoral', 'lightskyblue']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)plt.title("Pie
Chart")
plt.show()

RSR Rungta College of Engineering & Technology Bhilai

You might also like