Python Practical
Python Practical
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.
plt.barh(languages, votes,color=colors)
plt.xlabel('Number of Votes')
plt.ylabel('Programming Language')
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:
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.
import numpy as np
bar_width = 0.5
index = np.arange(len(quarters))
plt.xlabel('Quarter')
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)").
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.xlabel('Day')
plt.ylabel('Temperature (°C)')
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:
b) Customize the plot with the title "Distribution of Monthly Expenses", and labels for the x-axis
("Expenses (in dollars)") and y-axis ("Frequency").
expenses = [1200, 1500, 1100, 1300, 1600, 1400, 1700, 1800, 1600, 1500, 1700, 1900]
plt.figure(figsize=(10, 6))
plt.ylabel('Frequency')
plt.savefig('monthly_expenses_histogram.png')
plt.show()