AI Lab 06 Lab Tasks
AI Lab 06 Lab Tasks
A- Outcomes:
After completion of the lab session students will be able:
a. To create a simple plot, plot sine and cosine functions
b. To use subplots and bar plots
c. To understand histograms, scatter plots, scatter plots combined with line-plot
d. To understand pie charts
1
Lab Session -06
B- Lab Tasks:
1- In this task, you will be working with a dataset to create line plots, scatter
plots, or both. Follow the instructions below:
1. Dataset:
• Use the following NumPy arrays as your dataset:
import numpy as np
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x) + 0.2 * np.random.randn(50)
2. Line Plot:
• Create a line plot with x on the x-axis and y1 on the y-axis.
• Customize the line plot with the following requirements:
Line color: Choose a color other than the default (e.g., green).
Line style: Use a dashed line style.
Add a grid to the plot.
3. Scatter Plot:
• Create a scatter plot with x on the x-axis and y2 on the y-axis.
• Customize the scatter plot with the following requirements:
Marker type: Use triangles as markers.
Marker color: Choose a color different from the line plot (e.g., orange).
Marker size: Adjust the size for better visibility.
Add labels to the x and y axes.
4. Combined Plot:
• Create a single figure with both the line plot and scatter plot side by side (in
subplots).
• Customize each subplot individually with the following requirements:
Line plot: Maintain the color, style, and grid from Step 2.
Scatter plot: Maintain the marker type, color, size, and axis labels from Step 3.
Adjust the layout for better spacing between subplots.
Write/copy your code here:
Code:
import numpy as np
import matplotlib.pyplot as plt
2
Lab Session -06
# Dataset
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x) + 0.2 * np.random.randn(50)
# Line Plot
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x, y1, color='green', linestyle='--')
plt.grid(True)
plt.title('Maaz_Line Plot')
plt.xlabel('x')
plt.ylabel('y1')
# Scatter Plot
plt.subplot(1, 2, 2)
plt.scatter(x, y2, marker='^', color='orange', s=50)
plt.title('Maaz_Scatter Plot')
plt.xlabel('x')
plt.ylabel('y2')
# Adjust layout
plt.tight_layout()
# Show plots
plt.show()
3
Lab Session -06
Output:
2- In this task, you will explore creating bar plots to analyze categorical data.
Follow the instructions below:
1. Dataset:
• Use the following data for your bar plots:
import numpy as np
categories = np.array([’Category A’, ’Category B’, ’Category
C’, ’Category
D’])
values = np.array([25, 40, 30, 35])
2. Vertical Bar Plot:
• Create a vertical bar plot with categories on the x-axis and values on the y-axis.
• Customize the vertical bar plot with the following requirements:
Bar color: Choose a color other than the default (e.g., skyblue).
Add labels to the x and y axes.
Include a title for the plot.
3. Horizontal Bar Plot:
• Create a horizontal bar plot with categories on the y-axis and values on the x-axis.
• Customize the horizontal bar plot with the following requirements:
4
Lab Session -06
Bar color: Choose a color different from the vertical bar plot (e.g.,
lightcoral).
Add labels to the x and y axes.
Include a title for the plot.
Write/copy your code here:
Code:
import numpy as np
import matplotlib.pyplot as plt
# Dataset
categories = np.array(['Category A', 'Category B', 'Category C', 'Category D'])
values = np.array([25, 40, 30, 35])
5
Lab Session -06
OUTPUT:
1-
2-
6
Lab Session -06
3- In this task, you will explore creating histograms to visualize the distribution of
a dataset. Follow the instructions below:
1. Dataset:
• Use the following NumPy array as your dataset:
import numpy as np
data = np.random.randn(1000) # 1000 random data points from a
standard
normal distribution
2. Histogram:
• Create a histogram with data.
• Customize the histogram with the following requirements:
Number of bins: Use 20 bins.
Bar color: Choose a color other than the default (e.g., lightgreen).
Edge color: Set the edge color to black.
Transparency: Set a transparency of 0.7.
Add labels to the x and y axes.
Include a title for the plot.
Write/copy your code here:
Code:
import numpy as np
import matplotlib.pyplot as plt
# Dataset
data = np.random.randn(1000) # 1000 random data points from a standard normal
distribution
# Histogram
plt.figure(figsize=(8, 6))
plt.hist(data, bins=20, color='lightgreen', edgecolor='black', alpha=0.7)
plt.xlabel('Values')
plt.ylabel('Frequency')
7
Lab Session -06
4- In this task, you will explore creating a pie chart to represent proportions of a
dataset. Follow the instructions below:
1. Dataset:
• Use the following data for your pie chart:
import matplotlib.pyplot as plt
sizes = [30, 25, 20, 15, 10] # Sizes of different parts of the
whole
labels = [’Category A’, ’Category B’, ’Category C’, ’Category
D’, ’Category
E’] # Labels for each part
8
Lab Session -06
Code:
import matplotlib.pyplot as plt
# Dataset
sizes = [30, 25, 20, 15, 10] # Sizes of different parts of the whole
labels = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E'] # Labels
for each part
colors = ['skyblue', 'lightcoral', 'lightgreen', 'gold', 'lightsalmon'] # Colors for each part
explode = (0.1, 0, 0, 0, 0) # Exploding the first slice
# Pie Chart
plt.figure(figsize=(8, 8))
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',
startangle=90)
plt.title('Maaz-Proportions of Categories')
plt.show()
9
Lab Session -06
Output:
10
Lab Session-06
11