是什麼預測動物的壽命?
讓我們把前面所學整合起來,把一些 MATLAB 程式碼移植到 Python。
同事寄給你他們在 MATLAB 中對「AnAge: The Animal Ageing and Longevity Database」的分析。這個資料庫包含超過 4000 種脊椎動物的最長壽命資料,以及與壽命相關的其他資訊。
你已經在 Python 載入資料,現在想要了解動物的最大壽命(存在 numpy 陣列 max_longevity)與牠們每年生育胎數(在 litters_per_year)之間的關係。你也已經匯入 pyplot 並指派給變數 plt。
你手上有他們用 MATLAB 繪製這個關係的程式碼,但需要把它移植到 Python。
本練習屬於課程
給 MATLAB 使用者的 Python
練習說明
- 將註解改為 Python 語法。
- 以對應的
pyplot函式取代 MATLAB 的繪圖函式。 - 顯示你的圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
% Initialize the figure
figure
% Make the scatter plot
scatter(litters_per_year, max_longevity)
% Add axis labels
xlabel('Litters/Clutches per year')
ylabel('Maximum longevity (yrs)')
____.____()