開始使用免費開始

加入圖例

當你同時繪製多個資料集時,圖例有助於分辨各圖對應到哪個資料集。你可以使用 label 參數來加入圖例標籤。若要在圖上顯示圖例,使用函式 plt.legend()

matplotlib.pyplot 已以 plt 匯入,且清單 stock_Astock_B 已經在你的工作環境中可用。

本練習屬於課程

Python 金融入門

檢視課程

練習說明

  • stock_Astock_B 繪製直方圖,並為各圖加入標籤('Stock A''Stock B')。
  • 顯示圖例與圖形。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼