如何使用Matplotlib创建100%堆积面积图?

如何使用Matplotlib创建100%堆积面积图?

要使用Matplotlib创建100%堆积面积图,我们可以执行以下步骤 –

  • 设置图形大小并调整子图之间和周围的填充。

  • 创建年份列表。

  • 创建一个字典,其中包含各年人口的列表。

  • 创建一个图形和一组子图。

  • 绘制一个堆积面积图。

  • 在图形中放置一个图例,位置为“upper left”。

  • 设置标题、xlabel和ylabel。

  • 使用show()方法显示图形。

示例

import matplotlib.pyplot as plt

plt.rcParams ["figure.figsize"] = [7.503.50]
plt.rcParams ["figure.autolayout"] = True

year = [19501960197019801990200020102018]

population_by_continent = {
 'africa'[22828436547763181410441275]
 'americas'[3404255196197278409431006]
 'asia'[13941686212026253202371441694560]
 'europe'[220253276295310303294293]
 'oceania'[1215192226313639]
}

figax = plt.subplots()
ax.stackplotyearpopulation_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()
Python

输出

如何使用Matplotlib创建100%堆积面积图?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册