Skip to Content
数据可视化的基础知识
book

数据可视化的基础知识

by Claus O. Wilke
May 2025
Beginner to intermediate
390 pages
5h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 数据可视化的基础知识

第 7 章 分布可视化 分布可视化:直方图和密度图

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我们经常会遇到这样的情况:我们想了解某个变量在数据集中的分布情况。 举一个具体的例子,我们将考虑泰坦尼克号的乘客,这是我们在第 6 章中遇到的一个数据集。泰坦尼克号上大约有 1300 名乘客(不包括船员),我们已经报告了其中 756 人的年龄。我们可能想知道泰坦尼克号上有多少年龄段的乘客,即有多少儿童、青年、中年人、老年人等等。我们把乘客中不同年龄的相对比例称为乘客的年龄分布

可视化单一分布

我们可以通过将所有乘客按年龄分组,然后统计每个分组中的乘客人数,来了解乘客的年龄分布情况。 这样就可以得到一个表,如表 7-1

表 7-1. 泰坦尼克号上已知年龄的乘客人数。
年龄范围 计数

0-5

36

6-10

19

11-15

18

16-20

99

21-25

139

26-30

121

年龄范围 计数

31-35

76

36-40

74

41-45

54

46-50

50

51-55

26

56-60

22

年龄范围 计数

61-65

16

66-70

3

71-75

3

我们可以通过绘制填充矩形来直观地显示这个表格,矩形的高度与计数相对应,宽度与 年龄段的宽度相对应(图 7-1)。这种直观图称为直方图。(请注意,所有分隔的宽度必须相同,这样的直方图才是有效的直方图)。

fodv 0701
图 7-1. 泰坦尼克号乘客年龄直方图。数据来源:《泰坦尼克号百科全书》:泰坦尼克百科全书。

由于直方图是通过对数据进行分档生成的,因此其确切的视觉外观取决于对分档宽度的选择。 大多数生成直方图的可视化程序都会默认选择一个二进制宽度,但该二进制宽度很可能并不是您想要制作的任何直方图的最合适宽度。因此,必须经常尝试不同的二进制宽度,以验证生成的直方图是否能准确反映基础数据。一般来说,如果二进制宽度太小,直方图就会变得峰值过大,视觉效果不佳,数据的主要趋势可能会被掩盖。另一方面,如果二进制宽度过大,则数据分布中的较小特征(如本例中 10 岁左右的凹陷)可能会消失。

对于泰坦尼克号乘客的年龄分布,我们可以看到,1 年的二进制宽度太小,15 年的二进制宽度太大,而 3 至 5 年的二进制宽度则没有问题(图 7-2)。

fodv 0702
图 7-2. 直方图取决于所选的分段宽度。这里显示的是泰坦尼克号乘客的年龄分布图,有四种不同的二进制宽度:(a) 1 岁;(b) 3 岁;(c) 5 岁;(d) 15 岁。数据来源:Encyclopedia Titanica:泰坦尼克百科全书。
提示

在绘制直方图时,一定要探索多个分隔宽度。

至少从 18 世纪开始,直方图就一直是一种流行的可视化选择, ,部分原因是直方图易于手工绘制。最近,随着笔记本电脑和手机等日常设备具备了强大的计算能力,我们发现越来越多的直方图密度图所取代。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python数据分析

Python数据分析

Posts & Telecom Press, Ivan Idris
程序设计导论:Python语言实践

程序设计导论:Python语言实践

罗伯特 塞奇威克, 凯文 韦恩, 罗伯特 唐德罗
Beginning AppleScript®

Beginning AppleScript®

Stephen G. Kochan

Publisher Resources

ISBN: 9798341657793