matplotlip
matplotlip
Source code
# Data
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
# Plotting the line graph
plt.plot(x, y, color='green', marker='o')
# Adding titles and labels
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Display the plot
plt.show()
Output
PRACTICAL-26
Aim: To create a bar chart representing data.
Source code
import matplotlib.pyplot as plt
# Data
categories = ['Math', 'Science', 'English', 'History', 'Geography']
values = [85, 90, 75, 80, 88]
# Plotting the bar chart
plt.bar(categories, values, color='skyblue')
# Adding titles and labels
plt.title('Subject Scores')
plt.xlabel('Subjects')
plt.ylabel('Scores')
# Display the plot
plt.show()
Output
PRACTICAL-27
Aim: To create a histogram showing the distribution of
data.
Source code
import matplotlib.pyplot as plt
# Data: Age distribution of students
ages = [12, 13, 14, 15, 16, 14, 15, 16, 17, 15, 16, 17, 18, 15, 16]
# Plotting the histogram
plt.hist(ages, bins=5, color='purple', edgecolor='black')
# Adding titles and labels
plt.title('Age Distribution of Students')
plt.xlabel('Age')
plt.ylabel('Frequency')
# Display the plot
plt.show()
Output
PRACTICAL-28
Aim: To plot a scatter graph to show the relationship
between two variables.
Source code
import matplotlib.pyplot as plt
# Data: Height vs Weight
height = [150, 160, 170, 180, 190]
weight = [50, 60, 70, 80, 90]
# Plotting the scatter plot
plt.scatter(height, weight, color='red')
# Adding titles and labels
plt.title('Height vs Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
# Display the plot
plt.show()
Output
PRACTICAL-29
Aim: To create a pie chart showing the percentage
distribution of categories.
Source code
import matplotlib.pyplot as plt
# Data
labels = ['Python', 'Java', 'C++', 'JavaScript']
sizes = [40, 30, 20, 10]
# Plotting the pie chart
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90,
colors=['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'])
# Adding title
plt.title('Programming Language Popularity')
# Display the plot
plt.show()
Output
PRACTICAL-30
Aim: To create a line plot and customize it by adding
grid, title, and labels.
Source code
import matplotlib.pyplot as plt
# Data: Sales over 5 months
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [5000, 7000, 8000, 6500, 9000]
# Plotting the line graph
plt.plot(months, sales, color='blue', marker='o')
# Customizing the plot
plt.title('Monthly Sales', fontsize=16)
plt.xlabel('Months', fontsize=12)
plt.ylabel('Sales (in INR)', fontsize=12)
plt.grid(True)
# Display the plot
plt.show()
Output