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, [Link], 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 [Link] as plt
import seaborn as sns
# 1. Manually assign dataset using DataFrame
data = [Link]
({
'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
[Link](figsize=(8, 5))
[Link](data['Month'], data['Total Sales'], marker='o', linestyle='-', color='blue')
[Link]("Total Sales per Month")
[Link]("Month")
[Link]("Total Sales")
[Link](True)
[Link]()
# 3. Scatter Plot - Milk sales per month
[Link](figsize=(8, 5))
[Link](data['Month'], data['Milk'], color='green')
[Link]("Milk Sales per Month")
[Link]("Month")
[Link]("Milk Sales")
[Link](True)
[Link]()
# 4. Histogram - Rice sales distribution
[Link](figsize=(8, 5))
[Link](data['Rice'], bins=8, color='orange', edgecolor='black')
[Link]("Rice Sales Distribution")
[Link]("Rice Sales")
[Link]("Frequency")
[Link](True)
[Link]()
# 5. Various Visualizations
# Line Plot using Seaborn
[Link](data=data, x='Month', y='Total Sales')
[Link]("Line Plot - Total Sales")
[Link]()
# Bar Plot using seaborn
[Link](data=data, x='Month', y='Milk', palette='Blues')
[Link]("Bar Plot - Milk Sales")
[Link]()
# Histogram using Seaborn
[Link](data['Rice'], bins=8, kde=False, color='purple')
[Link]("Histogram - Rice Sales")
[Link]()
Output
Result
Thus the program for displaying different charts for the give data set was
created , executed and verified.
[Link] 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 [Link] 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 [Link]().
Program
import [Link] as plt
# Create the figure and subplots
fig,(ax1, ax2) = [Link](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)
[Link](x, y1, color='green', label='Green')
[Link](x, y2, color='orange', label='Orange')
ax1.set_title('Top Subplot with Green and Orange Lines')
[Link](loc='upper center')
# Bottom subplot (pie chart)
ax2.set_title('Bottom Subplot (pie)')
[Link](data)
# Display the figure
plt.tight_layout()
[Link]()
Output
Result
Thus the program for creating subplots with given fixed image size was
written and executed successfully.