Programming with Python Lab
(CSE-152L)
Submitted by :
Name :Armaan
Roll No:28240030
Section:C
Department of Computer Science and Engineering
Panipat Institute of Engineering and Technology (PIET),
Samalkha
(session 2024-25)
PROGRAM:17
Aim(a): Create a Box Plot using Matplotlib to visually represent the distribution of
exam scores.
Code:
import matplotlib.pyplot as plt
math_scores = [75, 88, 92, 60, 85, 95, 70, 80, 79, 91]
science_scores = [65, 70, 85, 89, 90, 72, 76, 84, 88, 92]
english_scores = [80, 85, 78, 82, 75, 89, 90, 95, 87, 93]
data = [math_scores, science_scores, english_scores]
plt.boxplot(data)
plt.title("Box Plot of Exam Scores by Subject")
plt.ylabel("Scores")
plt.xticks([1, 2, 3], ['Math', 'Science', 'English'])
plt.grid(False)
plt.show()
print("\nCode by Armaan, Roll-No: 28240030")
Result:
Aim(b): Create a Scatter Plot using Matplotlib to visualize the relationship between
height (cm) and weight (kg).
Code:
import matplotlib.pyplot as plt
height = [150, 160, 165, 170, 175, 180]
weight = [50, 55, 60, 65, 70, 75]
plt.scatter(height, weight, color='purple', marker='o', s=100)
plt.title("Height vs Weight")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.show()
print("\nCode by Armaan, Roll-No: 28240030)
Result:
Aim(c): Create a Histogram using Matplotlib to visualize the distribution of manually
defined exam scores.
Code:
import matplotlib.pyplot as plt
scores = [20, 45, 55, 60, 65, 30, 72, 75, 78, 50, 85, 88, 90,
35, 95, 98, 100]
plt.hist(scores, bins=8, color='green', edgecolor='black')
plt.title("Exam Score Distribution")
plt.xlabel("Score Ranges")
plt.ylabel("Number of Students")
plt.show()
print("\nCode by Armaan, Roll-No: 28240030")
Result:
PROGRAM:18
Aim(a): Create a Pair Plot using Seaborn to visualize relationships between multiple
variables.
Code:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Math': [78, 85, 92, 88, 76, 95, 89, 84, 90, 73],
'Science': [82, 79, 88, 91, 74, 96, 85, 87, 93, 70],
'English': [75, 80, 85, 82, 78, 88, 90, 86, 89, 77],
'Grade': ['A', 'A', 'A', 'A', 'B', 'A', 'A', 'A', 'A', 'B']
}
df = pd.DataFrame(data)
sns.pairplot(df, hue='Grade', diag_kind='kde')
plt.suptitle("Pair Plot of Student Scores", y=1.02)
plt.show()
print("\nCode by Armaan, Roll-No: 28240030")
Result:
Aim(b): Create a Heat Map using Seaborn to visualize the prices of different fruits
across various Indian states.
Code:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Fruit': ['Mango', 'Banana', 'Guava', 'Papaya'],
'Maharashtra': [80, 30, 45, 35],
'Uttar Pradesh': [70, 25, 40, 30],
'Andhra Pradesh': [75, 28, 42, 33],
'Tamil Nadu': [85, 32, 47, 36],
'West Bengal': [65, 27, 39, 29]
}
df = pd.DataFrame(data)
df.set_index('Fruit', inplace=True)
plt.figure(figsize=(9, 5))
sns.heatmap(df, annot=True, cmap='coolwarm', fmt='d')
plt.title("Fruit Prices (₹/kg) Across Indian States")
plt.xlabel("States")
plt.ylabel("Fruits")
plt.show()
print("\nCode by Armaan, Roll-No: 28240030")
Result:
Aim(c): Create a Distribution Plot using Seaborn to visualize the distribution of math
exam score.
Code:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
data = {
'student_id': range(1, 31),
'math_score': [55, 62, 78, 45, 89, 90, 76, 85, 67, 72,
91, 88, 77, 69, 73, 61, 58, 94, 87, 65,
81, 68, 79, 52, 60, 84, 66, 70, 75, 80]
}
df = pd.DataFrame(data)
sns.displot(data=df, x='math_score', kde=True, bins=10,
color='mediumseagreen')
plt.xlim(0, 100)
plt.title("Distribution of Math Exam Scores")
plt.xlabel("Math Score")
plt.ylabel("Number of Students")
plt.show()
print("\nCode by Armaan, Roll-No: 28240030")
Result: