Aim:
To generate basic visualizations (scatter plot, histogram, box plot, and bar plot) using the Iris dataset
with matplotlib in Python.
ALGORITHM:
1. Import necessary libraries: Use matplotlib for plotting and pandas for data handling.
2. Load the Iris dataset: Use pd.read_csv('iris.csv') to load the dataset.
3. Display first few rows: Use iris.head() to preview the data.
4. Create a scatter plot: Use plt.scatter() to plot Sepal Length vs Sepal Width.
5. Create a histogram: Use plt.hist() to visualize Petal Length distribution.
6. Create a bar plot: Use species_count.plot(kind='bar') to count occurrences of each species.
PROGRAM:
import matplotlib.pyplot as plt
import pandas as pd
# Load the Iris dataset
iris = pd.read_csv('iris.csv')
# Display the first few rows
print("\nFirst five rows of the Iris dataset:")
print(iris.head())
# Plot 1: Scatter Plot - Sepal Length vs Sepal Width
plt.figure(figsize=(8, 5))
plt.scatter(iris['sepal_length'], iris['sepal_width'], c='blue', alpha=0.7, edgecolor='k')
plt.title('Sepal Length vs Sepal Width', fontsize=14)
plt.xlabel('Sepal Length (cm)', fontsize=12)
plt.ylabel('Sepal Width (cm)', fontsize=12)
plt.grid(True)
plt.show()
# Plot 2: Histogram - Petal Length
plt.figure(figsize=(8, 5))
plt.hist(iris['petal_length'], bins=20, color='green', edgecolor='black', alpha=0.7)
plt.title('Histogram of Petal Length', fontsize=14)
plt.xlabel('Petal Length (cm)', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.grid(True)
plt.show()
# Plot 3: Boxplot - Sepal Width
plt.figure(figsize=(8, 5))
plt.boxplot(iris['sepal_width'], patch_artist=True, boxprops=dict(facecolor='orange'))
plt.title('Boxplot of Sepal Width', fontsize=14)
plt.ylabel('Sepal Width (cm)', fontsize=12)
plt.show()
# Plot 4: Bar Plot - Species Count
species_count = iris['species'].value_counts()
plt.figure(figsize=(8, 5))
species_count.plot(kind='bar', color=['skyblue', 'lightgreen', 'salmon'])
plt.title('Count of Each Species in the Dataset', fontsize=14)
plt.xlabel('Species', fontsize=12)
plt.ylabel('Count', fontsize=12)
plt.xticks(rotation=0)
plt.grid(axis='y')
plt.show()
OUTPUT:
SUMMA CODE:
import matplotlib.pyplot as plt
# Histogram
data = [5, 7, 8, 5, 6, 7, 8, 8, 9, 10, 6, 5, 8, 7]
plt.hist(data, bins=5, color='skyblue', edgecolor='black')
plt.title('Histogram')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
# Box Plot
data = [7, 8, 8, 9, 10, 6, 5, 5, 8, 7]
plt.boxplot(data, patch_artist=True, boxprops=dict(facecolor='orange'))
plt.title('Box Plot')
plt.ylabel('Values')
plt.show()
# Scatter Plot
x = [1, 2, 3, 4, 5]
y = [5, 7, 8, 6, 9]
plt.scatter(x, y, color='blue')
plt.title('Scatter Plot')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
# Bar Plot
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 4, 6]
plt.bar(categories, values, color='green')
plt.title('Bar Plot')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()
Result: Various plots such as a scatter plot of Sepal Length vs Sepal Width, a histogram of Petal
Length, a box plot of Sepal Width, and a bar plot of species count are displayed to visualize the
dataset's characteristics.