叠加图形
在本章中,我们学习了如何通过绘制图形来可视化数据。此类可视化的一个常见用途是比较两个数据集。在本节中,我们将了解如何“叠加”图形,即在共同的轴上将它们绘制在同一个图形中。
要使叠加有意义,被叠加的图形必须表示相同的变量并以相同的单位进行测量。
为了绘制叠加图形,方法 scatter、plot 和 barh 都可以以相同的方式调用。对于 scatter 和 plot,必须有一列作为所有叠加图形的公共水平轴。对于 barh,必须有一列作为公共轴,即类别集合。一般调用形式如下:
name_of_table.method(column_label_of_common_axis, array_of_labels_of_variables_to_plot)
更常见的是,我们首先只选择图形所需的列,然后仅通过指定公共轴上的变量来调用方法:
name_of_table.method(column_label_of_common_axis)
from datascience import *
import numpy as np
path_data = '../../../assets/data/'
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
叠加散点图
表格 sons_heights 是一个关于父母及其子女身高的历史数据集的一部分。具体来说,该总体由179名家庭中头胎出生的男性组成。数据包括他们自己的身高以及他们父母的身高。所有身高均以英寸为单位测量。
sons_heights = Table.read_table(path_data + 'sons_heights.csv')
sons_heights
father | mother | son
78.5 | 67 | 73.2
75.5 | 66.5 | 73.5
75 | 64 | 71
75 | 64 | 70.5
75 | 58.5 | 72
74 | 68 | 76.5
74 | 62 | 74
73 | 67 | 71
73 | 67 | 68
73 | 66.5 | 71
... (169 rows omitted)scatter 方法使我们能够可视化儿子的身高与其父母双方身高的关系。在图中,儿子的身高将构成公共水平轴。
sons_heights.scatter('son')
A scatter plot shows the relationship between the height of a 'son' on the x-axis and the heights of parents on the y-axis. Dark blue dots represent the 'father' data, which generally cluster higher on the y-axis, while gold dots represent the 'mother' data, clustering noticeably lower. Both sets of data show a slight upward trend, indicating that taller sons tend to have taller parents.注意我们仅指定了公共水平轴上的变量(儿子的身高)。Python 绘制了两个散点图:分别用于该变量与另外两个变量之间的关系。
每个点代表表格中的一行,即一个“父亲、母亲、儿子”三人组。对于所有点,水平轴代表儿子的身高。在蓝色点中,垂直轴代表父亲的身高。在金色点中,垂直轴代表母亲的身高。
金色和蓝色的散点图都向上倾斜,显示儿子的身高与父母双方身高之间存在正关联。蓝色(父亲)图通常比金色图高,因为父亲通常比母亲高。
叠加折线图
我们的下一个示例涉及更近时期的儿童数据。我们将回到人口普查数据表 us_pop,下面再次创建以供参考。从这个表中,我们将提取0到18岁每个年龄类别中所有儿童的数量。
# Read the full Census table
data = 'http://www2.census.gov/programs-surveys/popest/technical-documentation/file-layouts/2010-2019/nc-est2019-agesex-res.csv'
full_census_table = Table.read_table(data)
# Select columns from the full table and relabel some of them
partial_census_table = full_census_table.select('SEX', 'AGE', 'POPESTIMATE2014', 'POPESTIMATE2019')
us_pop = partial_census_table.relabeled('POPESTIMATE2014', '2014').relabeled('POPESTIMATE2019', '2019')
# Access the rows corresponding to all children, ages 0-18
children = us_pop.where('SEX', are.equal_to(0)).where('AGE', are.below(19)).drop('SEX')
children.show()
<IPython.core.display.HTML object>现在我们可以绘制两个叠加的折线图,显示2014年和2019年各年龄组的儿童数量。调用方式类似于上一个示例中的 scatter 调用。
children.plot('AGE')
A line graph compares population data by 'AGE' on the x-axis, ranging from 0 to 18, against a numerical scale in millions on the y-axis. Two lines track the data: a dark blue line for '2014' and a gold line for '2019'. Both lines follow a fluctuating but generally upward trend as age increases, with '2019' starting significantly lower at age 0 but rising to cross over and match or exceed '2014' values at multiple points.尽管水平轴标签包含一些半整数,但重要的是要记住我们只有年龄0、1、2等的数据。折线图“连接了之间的点”。
这两条图在几个地方相交。例如,2014年6岁儿童多于2019年,而2019年12岁儿童多于2014年。
当然,2019年的12岁儿童主要由2014年时为7岁的儿童组成。要在图上看到这一点,比较 AGE 12处的金色图和 AGE 7处的蓝色图。你会注意到金色图(2019年)看起来很像蓝色图(2014年)向右平移了5年。这种平移伴随着轻微的上升,这是因为2014年至2019年间进入该国的儿童数量超过了离开的儿童数量。幸运的是,在这些年龄段,生命损失并不大。
条形图
凯撒家庭基金会(Kaiser Family Foundation)编制了美国种族和民族分布的人口普查数据。该基金会的网站提供了2019年全美人口以及当时未满18岁的美国儿童的数据汇编。
表格 usa_ca 改编自他们关于美国和加利福尼亚州的数据。各列分别代表全美人口、加州人口、全美儿童和加州儿童。
表格主体包含不同类别的百分比。每列显示了Ethnicity/Race变量在对应列人群中的分布。因此,每列中的条目加起来为100。API 类别包括亚洲人和太平洋岛民(包括夏威夷原住民)。Other 类别包括美洲原住民、阿拉斯加原住民以及认同多个种族的人。
usa_ca = Table.read_table(path_data + 'usa_ca_2019.csv')
usa_ca
Ethnicity/Race | USA All | CA All | USA Children | CA Children
API | 5.8 | 15.1 | 4.9 | 11.5
Black | 12.2 | 5.3 | 13.4 | 4.9
Hispanic | 18.5 | 39.5 | 25.6 | 52.1
White | 60.1 | 36.4 | 50 | 25.5
Other | 3.4 | 3.7 | 6.1 | 6自然而然地想要比较这些分布。直接比较各列是有意义的,因为所有条目都是百分比,因此处于同一标度上。
方法 barh 允许我们通过在同一个轴上绘制多个条形图来可视化比较。调用方式与 scatter 和 plot 类似:我们必须指定类别的公共轴。
usa_ca.barh('Ethnicity/Race')
A horizontal bar chart compares demographic percentages across different groups labeled 'Ethnicity/Race' on the y-axis, including API, Black, Hispanic, White, and Other. Each demographic contains four bars representing different categories: 'USA All' (dark blue), 'CA All' (gold), 'USA Children' (light blue), and 'CA Children' (green). The data shows distinct regional and age variations, but generally within an Ethnicity/Race, the bars are either all on the shorter end, like for API, Black, and Other, or all on the longer end, like for Hispanic and White. 虽然绘制叠加条形图很简单,但该图形上的信息有点过多,我们无法理清各群体之间的异同。一次比较一对群体要容易得多。
让我们首先比较美国和加州的总体人口。
usa_ca.select('Ethnicity/Race', 'USA All', 'CA All').barh('Ethnicity/Race')
A horizontal bar chart compares demographic percentages across different groups labeled 'Ethnicity/Race' on the y-axis: API, Black, Hispanic, White, and Other. Each demographic contains two bars representing different categories: 'USA All' (dark blue) and 'CA All' (gold). The longest bar is for White-USA All at 60. The next longest bar is Hispanic-CA All at just below 40 and White-CA All between 30 and 40. The remaining bars are all less than 20 long.这两个分布相当不同。加州在 API 和 Hispanic 类别中的百分比更高,相应地,在 Black 和 White 类别中的百分比更低。两个群体在 Other 类别中的百分比非常相似。这些差异很大程度上是由于加州的地理位置以及历史上和近几十年的移民模式造成的。
从图中可以看出,2019年加州人口中将近40%是 Hispanic。与该州儿童人口的比较表明,Hispanic 比例在未来几年可能会更高。在2019年的加州儿童中,超过50%属于 Hispanic 类别。
usa_ca.select('Ethnicity/Race', 'CA All', 'CA Children').barh('Ethnicity/Race')
A horizontal bar chart compares demographic percentages across different groups labeled 'Ethnicity/Race' on the y-axis: API, Black, Hispanic, White, and Other. Each demographic contains two bars representing different categories: 'CA All' (dark blue) and 'CA Children' (gold). The longest bar is just above 50 for Hispanic-CA Children, followed by Hispanic-CA All around 40, and White-CA All below that. The remain bars have lengths below 30.更复杂的数据集自然会产生多样且有趣的可视化,包括不同类型的叠加图形。为了分析这样的数据,具备更多的数据操作技能会有所帮助,这样我们就能将数据转换为允许我们使用本节中类似方法的格式。在下一章中,我们将学习其中的一些技能。