Cycle 04
Lab Cycle 4 — Scientific Computing & Data Visualization (NumPy & Matplotlib)
Company Annual Profit Line Plot
Problem Statement
Take as input the months and profits made by Company ABC over a year. Represent this data using a line plot. Generated line plot must include X axis labelname = 'Month Number' and Y axis label name = 'Total profit'.
import matplotlib.pyplot as plt
def plot_company_profits(months, profits):
"""
Generates a line plot for company annual profits.
X-axis label: Month Number
Y-axis label: Total profit
"""
plt.figure(figsize=(10, 6))
# Plot line chart with markers
plt.plot(
months,
profits,
marker='o',
color='#1B6B5C',
linewidth=2.5,
markersize=7,
markerfacecolor='#D4AF37',
markeredgewidth=1.5,
label='Profit (Company ABC)'
)
# Explicit required axis labels
plt.xlabel('Month Number', fontsize=12, fontweight='bold', labelpad=10)
plt.ylabel('Total profit', fontsize=12, fontweight='bold', labelpad=10)
plt.title('Company ABC — Annual Profit Performance', fontsize=14, fontweight='bold', pad=15)
plt.xticks(months)
plt.grid(True, linestyle='--', alpha=0.6)
plt.legend(loc='upper left', fontsize=11)
# Annotate peak profit
max_idx = profits.index(max(profits))
plt.annotate(
f'Max: ${profits[max_idx]:,}',
xy=(months[max_idx], profits[max_idx]),
xytext=(months[max_idx] - 1.5, profits[max_idx] + 2000),
arrowprops=dict(facecolor='black', arrowstyle='->'),
fontweight='bold'
)
plt.tight_layout()
plt.show()
def main():
# 12 Months numbers
months = list(range(1, 13))
# Sample profit dataset (or read interactively)
default_profits = [25000, 28000, 32000, 30000, 36000, 42000, 39000, 45000, 48000, 52000, 58000, 65000]
print("Default monthly profits for 12 months:", default_profits)
user_choice = input("Use default dataset? (Y/N): ").strip().upper()
if user_choice == 'N':
profits = []
for m in months:
val = float(input(f"Enter profit for Month {m}: "))
profits.append(val)
else:
profits = default_profits
plot_company_profits(months, profits)
if __name__ == "__main__":
main()
Question 3 of 3