In the excel file called ’profits.xlsx’ holds the reported profits for a company in millions of dollars. The first row of the excel file has the months of the year, the second row has each month profits for the year 2018 and the third row has the profits for the year 2019. Create a program called exam2 yourname profit that: a) Directly reads the .xlsx data file. b) In one plot display the 2018 and 2019 profits vs the months. c) Enhance the plot with title, axis labels and legends.
The excel file 'profits.xlsx':
Python code to read excel file into a dataframe:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_excel("profit.xlsx")
print(df)
The Dataframe df:
Python code to plot the graphs:
plt.figure(figsize = (14,8))
plt.plot(df.iloc[0],label = "2018") # iloc is used to refer to a row in the dataframe
plt.plot(df.iloc[1], label = "2019")
plt.legend() # this will show the legend
plt.title("Company's profit for 2018 and 2019") # set the title
plt.ylabel("Profit in millions of dollars") # set the y label
plt.xlabel("Month") # set the x label
The plot:
Complete Python Code:
Get Answers For Free
Most questions answered within 1 hours.