matplotlib programs
Program 1 ; Line Chart: Tracking Weekly
Temperatures
This program demonstrates how to plot a simple line graph with custom labels, titles,
and markers.
Python
import [Link] as plt
# Data for the week
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temp = [32, 34, 31, 29, 35, 36, 33]
[Link](days, temp, color='red', marker='o', linestyle='--')
# Adding labels and title
[Link]('Days of the week')
[Link]('Temperature in Celsius')
[Link]('Weekly Temperature Analysis')
[Link]()
Program 2 ; Bar Graph: Comparing Stream
Enrollment
Bar graphs are essential for comparing categories. This example shows how to create
a vertical bar chart with a specific width and color.
Python
import [Link] as plt
# Data for student enrollment
streams = ['Science', 'Commerce', 'Humanities', 'Vocational']
students = [120, 150, 80, 40]
[Link](streams, students, color='skyblue', width=0.5)
[Link]('Streams')
[Link]('Number of Students')
[Link]('Student Enrollment by Stream')
[Link]()
Program 3 ; Pie Chart: Monthly Household
Expenses
The Pie chart is used to show proportions of a whole. We use the explode feature to
highlight a specific section.
Python
import [Link] as plt
# Data for expenses
activities = ['Rent', 'Grocery', 'Bills', 'Leisure']
slices = [40, 30, 20, 10]
cols = ['gold', 'lightgreen', 'lightcoral', 'lightskyblue']
[Link](slices, labels=activities, colors=cols,
startangle=90, shadow=True, explode=(0, 0.1, 0, 0),
autopct='%1.1f%%')
[Link]('Monthly Expense Distribution')
[Link]()
Program 4 ; Histogram: Distribution of Marks
Histograms are used to show frequency distribution of continuous data (like marks or
ages).
Python
import [Link] as plt
# Data: Marks obtained by 20 students
marks = [45, 55, 62, 70, 75, 78, 82, 85, 88, 90, 92, 95, 48, 52, 66, 71, 77, 81,
84, 89]
# Defining bins (intervals)
bins = [40, 50, 60, 70, 80, 90, 100]
[Link](marks, bins, color='purple', edgecolor='black')
[Link]('Marks Range')
[Link]('Number of Students')
[Link]('Distribution of Student Marks')
[Link]()