LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Analyzing Financial Statements in Python

Kurs anzeigen

Anleitung zur Übung

  • Convert the msft DataFrame from wide to long format.
  • Plot the asset turnover ratio and gross margin ratio in the same plot with Year on the x-axis and add hue over the dimension of Ratio.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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()
Code bearbeiten und ausführen