顏色
你先前寫的程式碼都已經放在腳本裡了。
下一步要讓圖形更繽紛!為此,我們已經替你建立了一個清單 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()