開始使用免費開始

顏色

你先前寫的程式碼都已經放在腳本裡了。

下一步要讓圖形更繽紛!為此,我們已經替你建立了一個清單 col。它為每個國家指定一個顏色,依該國家所在洲別而定。

你可能會問,col 是怎麼做出來的?Gapminder 資料裡有個 continent 清單,記載每個國家所屬的洲別。我們用它建立了一個把洲別對應到顏色的字典:

dict = {
    'Asia':'red',
    'Europe':'green',
    'Africa':'blue',
    'Americas':'yellow',
    'Oceania':'black'
}

現在先不用擔心字典的細節;你會在下一章學到字典。

本練習屬於課程

Python 中級

檢視課程

練習說明

  • plt.scatter() 的參數中加入 c = col
  • 透過在 plt.scatter() 中設定 alpha 參數為 0.8 來調整氣泡的不透明度。alpha 可設為 0 到 1,0 代表完全透明,1 代表完全不透明。

動手互動練習

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

# Specify c and alpha inside plt.scatter()
plt.scatter(x = gdp_cap, y = life_exp, s = np.array(pop) * 2)

# Previous customizations
plt.xscale('log') 
plt.xlabel('GDP per Capita [in USD]')
plt.ylabel('Life Expectancy [in years]')
plt.title('World Development in 2007')
plt.xticks([1000,10000,100000], ['1k','10k','100k'])

# Show the plot
plt.show()
編輯並執行程式碼