颜色
您之前编写的代码已在脚本中为您准备好。
接下来,让图更"多彩"一些!为此,我们已为您创建了列表 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()