首页 > 解决方案 > 如何按州绘制总人口直方图

问题描述

我有一个 .csv 文件,其中包含“总人口”、“州”、“县”等列(其他列)

我正在绘制一个直方图,其中 x 轴显示状态,Y 轴显示人口。我该怎么办?

标签: pythonplothistogram

解决方案


作为一个初学者,我发现很难使用 .csv 或 .txt,所以我决定将数据写入代码中。现在,如果您希望显示国家的 X 轴和人口的 Y 轴,这是一种方法:

import matplotlib.pyplot as plt

countries = ['Cambodia', 'Thailand']
populations = [16245729, 69183173]

x = countries
y = populations

plt.xlabel('Country')
plt.ylabel('Population (in tens of millions)')
plt.title('Populations 2018')

plt.bar(x, y)
plt.show()

推荐阅读