Plotting ratios in one figure
In this exercise, you'll plot and compute the gross margin and asset turnover ratio of Microsoft over time. But unlike the last exercise, here you will plot it in one figure. This will help visually analyze the trend of these ratios since the ratios are plotted in the same figure.
You will use the pandas .melt() function in this exercise. In the video, the value_vars argument was specified in the function. value_vars refer to the columns we want to unpivot. However if value_vars is not specified, then all the columns which are not id_vars will be taken as value_vars.
Asset turnover and gross margin ratios have been computed for you in the msft DataFrame, provided in the "asset_turnover" and "gross_margin" columns, respectively.
Questo esercizio fa parte del corso
Analyzing Financial Statements in Python
Istruzioni dell'esercizio
- Convert the
msftDataFrame from wide to long format. - Plot the asset turnover ratio and gross margin ratio in the same plot with
Yearon the x-axis and addhueover the dimension ofRatio.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Convert the DataFrame from wide to long
msft_melt = msft.melt(id_vars=____, value_vars=____, var_name="Ratio")
# Plot the data
plot = sns.lineplot(data=msft_melt, x=____, y=___, hue=____)
plt.show()
plot.xaxis.set_major_locator(MaxNLocator(integer=True))
plt.show()