Ex.
No 3d
Date: DATA VISUALIZATION ON MONTHLY SALES DATASET
Aim
To manually define a dataset and visualize sales data for Milk, Rice, and
Total Sales across months using different plot types such as Line Plot, Scatter
Plot, Histogram, Density Plot, and Bar Plot.
Algorithm
• Import necessary libraries (pandas, matplotlib.pyplot, seaborn).
• Manually create a dataset using a pandas DataFrame.
• Plot:
o Line Plot for Total Sales across months.
o Scatter Plot for Milk sales per month.
o Histogram for Rice sales.
o Additional visualizations using Seaborn.
• Display all plots with appropriate labels and titles.
Program
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 1. Manually assign dataset using DataFrame
data = pd.DataFrame
({
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'Milk': [120, 130, 115, 140, 125, 135, 150, 160, 155, 145, 138, 142],
'Rice': [250, 260, 245, 270, 255, 265, 280, 275, 268, 260, 255, 250],
'Total Sales': [370, 390, 360, 410, 380, 400, 430, 445, 423, 405, 393, 412]
})
# 2. Line Plot - Total Sales of each month
plt.figure(figsize=(8, 5))
plt.plot(data['Month'], data['Total Sales'], marker='o', linestyle='-', color='blue')
plt.title("Total Sales per Month")
plt.xlabel("Month")
plt.ylabel("Total Sales")
plt.grid(True)
plt.show()
# 3. Scatter Plot - Milk sales per month
plt.figure(figsize=(8, 5))
plt.scatter(data['Month'], data['Milk'], color='green')
plt.title("Milk Sales per Month")
plt.xlabel("Month")
plt.ylabel("Milk Sales")
plt.grid(True)
plt.show()
# 4. Histogram - Rice sales distribution
plt.figure(figsize=(8, 5))
plt.hist(data['Rice'], bins=8, color='orange', edgecolor='black')
plt.title("Rice Sales Distribution")
plt.xlabel("Rice Sales")
plt.ylabel("Frequency")
plt.grid(True)
plt.show()
# 5. Various Visualizations
# Line Plot using Seaborn
sns.lineplot(data=data, x='Month', y='Total Sales')
plt.title("Line Plot - Total Sales")
plt.show()
# Bar Plot using seaborn
sns.barplot(data=data, x='Month', y='Milk', palette='Blues')
plt.title("Bar Plot - Milk Sales")
plt.show()
# Histogram using Seaborn
sns.histplot(data['Rice'], bins=8, kde=False, color='purple')
plt.title("Histogram - Rice Sales")
plt.show()
Output
Result
Thus the program for displaying different charts for the give data set was
created , executed and verified.
Ex.No 4
Date: VISUALIZING MULTIPLE LINES WITH LEGENDS IN
SUBPLOTS USING MATPLOTLIB
Aim
To create a figure with two vertically stacked subplots using Matplotlib,
where the top subplot contains two colored lines (green and orange) along with a
legend placed at the top-center of the plot.
Algorithm
• Import the matplotlib.pyplot library.
• Create a figure of size 15 x 8 with two subplots (top and bottom).
• Define sample x, y1, and y2 data for plotting.
• Plot two lines in the top subplot:
• One in green color.
• One in orange color.
• Add a legend labeled Green and Orange.
• Set the legend's position to the top-middle using loc='upper center'.
• Leave the bottom subplot empty with title and hidden axes.
• Display the plot using plt.show().
Program
import matplotlib.pyplot as plt
# Create the figure and subplots
fig,(ax1, ax2) = plt.subplots(2, 1, figsize=(15, 8))
# Sample data
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [1, 4, 6, 8, 10]
data=[10,24,45,56]
# Top subplot: two lines (green and orange)
ax1.plot(x, y1, color='green', label='Green')
ax1.plot(x, y2, color='orange', label='Orange')
ax1.set_title('Top Subplot with Green and Orange Lines')
ax1.legend(loc='upper center')
# Bottom subplot (pie chart)
ax2.set_title('Bottom Subplot (pie)')
ax2.pie(data)
# Display the figure
plt.tight_layout()
plt.show()
Output
Result
Thus the program for creating subplots with given fixed image size was
written and executed successfully.