如何使用Matplotlib创建100%堆积面积图?
要使用Matplotlib创建100%堆积面积图,我们可以执行以下步骤 –
- 设置图形大小并调整子图之间和周围的填充。
-
创建年份列表。
-
创建一个字典,其中包含各年人口的列表。
-
创建一个图形和一组子图。
-
绘制一个堆积面积图。
-
在图形中放置一个图例,位置为“upper left”。
-
设置标题、xlabel和ylabel。
-
使用show()方法显示图形。
示例
import matplotlib.pyplot as plt
plt.rcParams ["figure.figsize"] = [7.50,3.50]
plt.rcParams ["figure.autolayout"] = True
year = [1950,1960,1970,1980,1990,2000,2010,2018]
population_by_continent = {
'africa':[228,284,365,477,631,814,1044,1275],
'americas':[340,425,519,619,727,840,943,1006],
'asia':[1394,1686,2120,2625,3202,3714,4169,4560],
'europe':[220,253,276,295,310,303,294,293],
'oceania':[12,15,19,22,26,31,36,39],
}
fig,ax = plt.subplots()
ax.stackplot(year,population_by_continent.values(),labels =population_by_continent.keys())
ax.legend(loc='upper left')
ax.set_title('World population')
ax.set_xlabel('Year')
ax.set_ylabel('Number of people (millions)')
plt.show()