1.
Scenario: A survey was conducted to find out the favourite programming languages among
students in a class. The results are as follows:
Python: 40 votes
Java: 30 votes
C++: 20 votes
JavaScript: 15 votes
Ruby: 10 votes
Task:
a) Create a horizontal bar graph to display the number of votes each programming language
received.
b) Customize the graph with the title "Favourite Programming Languages", and label the x-axis as
"Number of Votes" and the y-axis as "Programming Language".
c) Add diƯerent colors for each bar and ensure that the bars are labelled with their respective vote
counts.
d) Save the plot as "favorite_languages_horizontal_bar.png".
import matplotlib.pyplot as plt
languages = ['Python', 'Java', 'C++', 'JavaScript', 'Ruby']
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']
votes = [40, 30, 20, 15, 10]
plt.barh(languages, votes,color=colors)
plt.xlabel('Number of Votes')
plt.ylabel('Programming Language')
plt.title('Favourite Programming Languages')
plt.savefig('favorite_languages_horizontal_bar.png')
plt.show()
2. Scenario: A company’s annual expenses are divided among three departments (HR, IT, and
Marketing) over four quarters. The expenses (in thousands of dollars) are:
HR: [30, 25, 28, 35]
IT: [40, 38, 42, 45]
Marketing: [25, 28, 30, 32]
Task:
a) Create a stacked bar graph that shows the distribution of expenses across all departments for
each quarter.
b) Customize the plot with the title "Quarterly Departmental Expenses", and labels for the x-axis
("Quarter") and y-axis ("Expenses (in thousands of dollars)").
c) Use diƯerent colors for each department and ensure the stacked bars correctly represent the
cumulative expenses.
d) Add a legend to distinguish between HR, IT, and Marketing.
e) Save the plot as "departmental_expenses_stacked_bar.png".
import matplotlib.pyplot as plt
import numpy as np
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
hr_expenses = [30, 25, 28, 35]
it_expenses = [40, 38, 42, 45]
marketing_expenses = [25, 28, 30, 32]
bar_width = 0.5
index = np.arange(len(quarters))
plt.bar(index, hr_expenses, bar_width, label='HR', color='#1f77b4')
plt.bar(index, it_expenses, bar_width, bottom=hr_expenses, label='IT', color='#ff7f0e')
plt.bar(index, marketing_expenses, bar_width, bottom=np.array(hr_expenses) +
np.array(it_expenses), label='Marketing', color='#2ca02c')
plt.xlabel('Quarter')
plt.ylabel('Expenses (in thousands of dollars)')
plt.title('Quarterly Departmental Expenses')
plt.xticks(index, quarters)
plt.legend()
plt.savefig('departmental_expenses_stacked_bar.png')
plt.show()
3.Scenario: You have temperature data (in °C) recorded over 10 days for three cities: City A, City B,
and City C. The data is:
City A: [22, 23, 24, 21, 20, 25, 24, 23, 22, 21]
City B: [18, 19, 20, 21, 22, 23, 24, 23, 22, 21]
City C: [28, 27, 26, 25, 24, 23, 22, 21, 20, 19]
Task:
a) Plot the temperature trends of the three cities on the same graph using diƯerent line styles and
colors.
b) Customize the plot with the title "10-Day Temperature Trends", and labels for the x-axis ("Day")
and y-axis ("Temperature (°C)").
c) Include legend to diƯerentiate between the cities.
d) Save the plot as "temperature_trends_multi_line.png"
import matplotlib.pyplot as plt
days = range(1, 11)
city_a_temps = [22, 23, 24, 21, 20, 25, 24, 23, 22, 21]
city_b_temps = [18, 19, 20, 21, 22, 23, 24, 23, 22, 21]
city_c_temps = [28, 27, 26, 25, 24, 23, 22, 21, 20, 19]
plt.figure(figsize=(10, 6))
plt.plot(days, city_a_temps, 'o-', color='blue', label='City A')
plt.plot(days, city_b_temps, 's--', color='green', label='City B')
plt.plot(days, city_c_temps, 'd-.', color='red', label='City C')
plt.xlabel('Day')
plt.ylabel('Temperature (°C)')
plt.title('10-Day Temperature Trends')
plt.legend()
plt.savefig('temperature_trends_multi_line.png')
plt.show()
4.Scenario: You have recorded your monthly expenses (in dollars) over a year. The data is:
Expenses: [1200, 1500, 1100, 1300, 1600, 1400, 1700, 1800, 1600, 1500, 1700, 1900]
Task:
a) Create a histogram to visualize the distribution of your monthly expenses.
b) Customize the plot with the title "Distribution of Monthly Expenses", and labels for the x-axis
("Expenses (in dollars)") and y-axis ("Frequency").
c) Use 6 bins to group the expenses.
d) Save the plot as "monthly_expenses_histogram.png".
import matplotlib.pyplot as plt
expenses = [1200, 1500, 1100, 1300, 1600, 1400, 1700, 1800, 1600, 1500, 1700, 1900]
plt.figure(figsize=(10, 6))
plt.hist(expenses, bins=6, alpha=0.7)
plt.title('Distribution of Monthly Expenses')
plt.xlabel('Expenses (in dollars)')
plt.ylabel('Frequency')
plt.savefig('monthly_expenses_histogram.png')
plt.show()