Python Tutorial: Matplotlib Exercise-2

Sunday, 7 September 2025

Matplotlib Exercise-2

Practice Line Chart Questions (Class 12 IP)

1.      Plot a line chart showing the marks of a student in 5 subjects.

Ans:

import matplotlib.pyplot as plt

# Data

subjects = ["Math", "Science", "English", "History", "Computer"]

marks = [85, 78, 92, 74, 88]

# Plotting line chart

plt.plot(subjects, marks, marker='o', linestyle='-', linewidth=2)

# Adding labels and title

plt.xlabel("Subjects")

plt.ylabel("Marks")

plt.title("Marks of a Student in 5 Subjects")

# Display the chart

plt.show()

2.      Draw a line chart to show the sales of a company for 12 months.

Ans:

import matplotlib.pyplot as plt

# Data

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",

          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

sales = [12000, 15000, 18000, 22000, 21000, 25000,

         27000, 30000, 28000, 26000, 24000, 31000]

# Plotting line chart

plt.plot(months, sales, marker='o', linestyle='-', linewidth=2)

# Adding labels and title

plt.xlabel("Months")

plt.ylabel("Sales (in ₹)")

plt.title("Company Sales Over 12 Months")

# Display the chart

plt.show()

3.      Plot a line chart showing the temperature of a city for 7 days.

Ans:

import matplotlib.pyplot as plt

# Data

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

temperature = [30, 32, 31, 29, 28, 33, 34]

# Plotting line chart

plt.plot(days, temperature, marker='o', linestyle='-', linewidth=2, color='orange')

# Adding labels and title

plt.xlabel("Days of the Week")

plt.ylabel("Temperature (°C)")

plt.title("Temperature of a City Over 7 Days")

# Display the chart

plt.show()

4.      Create a line chart showing the population growth of a country from 2000 to 2020 (every 5 years).

Ans:

import matplotlib.pyplot as plt

# Data

years = [2000, 2005, 2010, 2015, 2020]

population = [50, 58, 67, 77, 90]  # in millions

# Plotting line chart

plt.plot(years, population, marker='o', ls='-', lw=2, color='green')

# Adding labels and title

plt.xlabel("Year")

plt.ylabel("Population (in millions)")

plt.title("Population Growth of a Country (2000 - 2020)")

plt.grid(True)

# Display the chart

plt.show()

5.      Draw a line chart showing the rainfall (in mm) of 6 months in a city.

Ans:

import matplotlib.pyplot as plt

# Data

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

rainfall = [120, 95, 150, 200, 170, 140]  # rainfall in mm

# Plotting line chart

plt.plot(months, rainfall, marker='o', linestyle='-', linewidth=2, color='blue')

# Adding labels and title

plt.xlabel("Months")

plt.ylabel("Rainfall (mm)")

plt.title("Rainfall in a City Over 6 Months")

plt.grid(True)

plt.savefig("Line5.jpg")

# Display the chart

plt.show()

6.      Plot a line chart showing the profit earned by a shop in 8 years.

Ans:

import matplotlib.pyplot as plt

# Data

years = ["2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020"]

profit = [20000, 25000, 27000, 30000, 35000, 37000, 40000, 45000]  # in ₹

# Plotting line chart

plt.plot(years, profit, marker='o', ls='-', lw=2, color='purple',label='Profit')

# Adding labels and title

plt.xlabel("Years")

plt.ylabel("Profit (₹)")

plt.title("Profit Earned by a Shop Over 8 Years")

plt.grid(True)

plt.legend()

plt.savefig("Line6.jpg")

# Display the chart

plt.show()

7.      Create a line chart comparing the marks of two students in 5 subjects.

Ans:

import matplotlib.pyplot as plt

# Data

subjects = ["Math", "Science", "English", "History", "Computer"]

student_A = [85, 78, 92, 74, 88]

student_B = [80, 82, 89, 70, 90]

# Plotting line chart

plt.plot(subjects, student_A, marker='o', linestyle='-', linewidth=2, label="Student A")

plt.plot(subjects, student_B, marker='s', linestyle='--', linewidth=2, label="Student B")

# Adding labels and title

plt.xlabel("Subjects")

plt.ylabel("Marks")

plt.title("Comparison of Marks Between Two Students")

plt.legend()

# Save the chart as an image

plt.savefig("students_marks_comparison.png")  # saves in the same folder as script

# Display the chart

plt.show()

8.      Draw a line chart showing petrol prices in India for 10 months.

Ans:

import matplotlib.pyplot as plt

# Data

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",

          "Jul", "Aug", "Sep", "Oct"]

petrol_prices = [95, 97, 99, 100, 102, 101, 103, 104, 106, 108]  # in ₹ per litre

# Plotting line chart

plt.plot(months, petrol_prices, marker='o', linestyle='-', linewidth=2, color='red',mfc='b')

# Adding labels, title, and grid

plt.xlabel("Months")

plt.ylabel("Petrol Price (₹ per litre)")

plt.title("Petrol Prices in India (10 Months)")

plt.grid(True, linestyle="--", alpha=0.6)

# Save the chart as an image

plt.savefig("petrol_prices_chart.png")

# Display the chart

plt.show()

9.      Plot a line chart showing the number of visitors to a park in 7 days. Add marker color blue and marker edge color is red.

Ans:

import matplotlib.pyplot as plt

# Data

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

visitors = [120, 150, 100, 180, 200, 300, 250]

# Plotting line chart with blue markers and black edges

plt.plot(days, visitors, marker='o', markersize=8,

         mfc='blue', mec='red',

         linestyle='-', linewidth=2, color='green')

# Adding labels and title

plt.xlabel("Days of the Week")

plt.ylabel("Number of Visitors")

plt.title("Visitors to a Park Over 7 Days")

plt.savefig("Visitors.png")

# Display the chart

plt.show()

10. Create a line chart showing the literacy rate of a country for 6 decades.

Ans.

import matplotlib.pyplot as plt

# Data

decades = ["1960", "1970", "1980", "1990", "2000", "2010"]

literacy_rate = [40, 50, 60, 70, 80, 90]  # in percentage

# Plotting line chart

plt.plot(decades, literacy_rate, marker='o', linestyle='-', linewidth=2, color='purple')

# Adding labels and title

plt.xlabel("Decades")

plt.ylabel("Literacy Rate (%)")

plt.title("Literacy Rate of a Country Over 6 Decades")

# Display the chart

plt.show()

Practice Bar Chart Questions

1.      Plot a bar chart showing the population of five countries.

Ans:

import matplotlib.pyplot as plt

# Data

countries = ["India", "China", "USA", "Brazil", "Russia"]

population = [1400, 1410, 331, 213, 146]  # in millions

# Plotting bar chart

plt.bar(countries, population, color='skyblue', edgecolor='black')

# Adding labels and title

plt.xlabel("Countries")

plt.ylabel("Population (in millions)")

plt.title("Population of Five Countries")

# Display the chart

plt.show()

2.      Draw a bar chart for the average rainfall (in mm) of 6 months in a city.

Ans:

import matplotlib.pyplot as plt

# Data

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

rainfall = [120, 95, 150, 200, 170, 140]  # in mm

# Plotting bar chart

plt.bar(months, rainfall, color='blue', edgecolor='black')

# Adding labels and title

plt.xlabel("Months")

plt.ylabel("Average Rainfall (mm)")

plt.title("Average Rainfall in a City Over 6 Months")

# Display the chart

plt.show()

3.      Create a bar chart for the number of students in different streams (Science, Commerce, Arts).

Ans:

import matplotlib.pyplot as plt

# Data

streams = ["Science", "Commerce", "Arts"]

students = [120, 80, 100]

# Plotting bar chart

plt.bar(streams, students, color=['green', 'orange', 'purple'], edgecolor='black')

# Adding labels and title

plt.xlabel("Streams")

plt.ylabel("Number of Students")

plt.title("Number of Students in Different Streams")

# Display the chart

plt.show()

4.      Plot a bar chart showing the sales of two companies in 5 different years.

Ans:

import matplotlib.pyplot as plt

import numpy as np

# Data

years = ["2016", "2017", "2018", "2019", "2020"]

company_A = [50, 60, 70, 65, 80]   # in lakhs

company_B = [55, 58, 72, 60, 85]   # in lakhs

# X-axis positions

x = np.arange(len(years))

width = 0.35  # width of bars

# Plotting side-by-side bars

plt.bar(x - width/2, company_A, width, label="Company A", color="skyblue", edgecolor="black")

plt.bar(x + width/2, company_B, width, label="Company B", color="orange", edgecolor="black")

# Adding labels and title

plt.xlabel("Years")

plt.ylabel("Sales (in lakhs)")

plt.title("Sales of Two Companies Over 5 Years")

plt.xticks(x, years)

plt.legend()

# Display the chart

plt.show()

5.      Draw a horizontal bar chart showing the literacy rate of 6 states in India.

Ans:

import matplotlib.pyplot as plt

# Data

states = ["Kerala", "Maharashtra", "Tamil Nadu", "Karnataka", "Gujarat", "Bihar"]

literacy_rate = [96.2, 82.3, 80.1, 75.4, 78.0, 61.8]  # in percentage

# Plotting horizontal bar chart

plt.barh(states, literacy_rate, color='teal', ec='black') #ec-> edgecolor

# Adding labels and title

plt.xlabel("Literacy Rate (%)")

plt.ylabel("States")

plt.title("Literacy Rate of 6 States in India")

# Display the chart

plt.show()

6.      Make a grouped bar chart comparing marks of three students in 4 subjects.

Ans:

import matplotlib.pyplot as plt

import numpy as np

# Data

subjects = ["Math", "Science", "English", "History"]

student_A = [85, 78, 92, 74]

student_B = [80, 82, 89, 70]

student_C = [88, 76, 85, 80]

# X-axis positions

x = np.arange(len(subjects))

width = 0.25  # width of each bar

# Plotting grouped bars

plt.bar(x - width, student_A, width, label="Student A", color="skyblue", edgecolor="black")

plt.bar(x, student_B, width, label="Student B", color="orange", edgecolor="black")

plt.bar(x + width, student_C, width, label="Student C", color="green", edgecolor="black")

# Adding labels and title

plt.xlabel("Subjects")

plt.ylabel("Marks")

plt.title("Comparison of Marks of Three Students in 4 Subjects")

plt.xticks(x, subjects)

plt.legend()

# Display the chart

plt.show()

7.      Plot a bar chart showing the number of visitors to a park in different months.

Ans:

import matplotlib.pyplot as plt

# Data

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

visitors = [120, 150, 170, 200, 250, 300, 350, 330, 280, 220, 180, 140]  # Example visitor numbers

# Plotting the bar chart

plt.bar(months, visitors, color='skyblue')

plt.title('Number of Visitors to the Park in Different Months')

plt.xlabel('Months')

plt.ylabel('Number of Visitors')

plt.xticks(rotation=45)

plt.grid(axis='y', linestyle='--', alpha=0.7)

# Show the plot

plt.show()

8.      Draw a bar chart for the production of rice in different states of India.

Ans:

import matplotlib.pyplot as plt

# Data (Example values in million tonnes)

states = ['Punjab', 'Haryana', 'Uttar Pradesh', 'Bihar', 'West Bengal', 'Andhra Pradesh']

rice_production = [14.0, 10.5, 21.0, 11.5, 17.0, 9.0]

# Plotting the bar chart

plt.figure(figsize=(10,6))

plt.bar(states, rice_production, color='green')

plt.title('Rice Production in Different States of India')

plt.xlabel('States')

plt.ylabel('Rice Production (million tonnes)')

plt.grid(True)

# Show the plot

plt.show()

9.      Create a bar chart showing profit earned by a company in 6 years.

Ans:

import matplotlib.pyplot as plt

# Data (example profits in million dollars)

years = ['2018', '2019', '2020', '2021', '2022', '2023']

profit = [5, 7, 6, 8, 10, 12]

# Plotting the bar chart

plt.figure(figsize=(10,6))

plt.bar(years, profit, color=['blue','orange','green','purple','magenta','cyan'])

plt.title('Company Profit Over 6 Years')

plt.xlabel('Years')

plt.ylabel('Profit (in million $)')

plt.grid(axis='y', linestyle='--', alpha=0.7)

# Show the plot

plt.show()

10. Plot a bar chart to compare temperatures of 5 cities in summer and winter.

Ans:

import matplotlib.pyplot as plt

# Data (example profits in million dollars)

years = ['2018', '2019', '2020', '2021', '2022', '2023']

profit = [5, 7, 6, 8, 10, 12]

# Plotting the bar chart

plt.figure(figsize=(10,6))

plt.bar(years, profit, color=['blue','orange','green','purple','magenta','cyan'])

plt.title('Company Profit Over 6 Years')

plt.xlabel('Years')

plt.ylabel('Profit (in million $)')

plt.grid(axis='y', linestyle='--', alpha=0.7)

# Show the plot

plt.show()

 

No comments:

Post a Comment