Aan de slagGa gratis aan de slag

Adding a legend

A legend can be useful when plotting multiple datasets to identify which plot is associated with a specific dataset. To add a legend, you can use the label argument. To display the legend on the plot, you can use the function plt.legend().

matplotlib.pyplot is imported as plt and lists stock_A and stock_B are available in your workspace.

Deze oefening maakt deel uit van de cursus

Introduction to Python for Finance

Cursus bekijken

Oefeninstructies

  • Plot histograms for stock_A and stock_B and add labels to each plot ('Stock A' and 'Stock B').
  • Display the legend and the plot.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Plot stock_A and stock_B histograms
plt.hist(stock_A, bins=100, alpha=0.4, label=____)
plt.hist(stock_B, bins=100, alpha=0.4, label=____)

# Add the legend
____

# Display the plot
plt.show()
Code bewerken en uitvoeren