1
00:00:09,279 --> 00:00:12,448
Heatmaps are a way to visualize
three-dimensional data and

2
00:00:12,448 --> 00:00:15,690
to take advantage of spatial
proximity of those dimensions.

3
00:00:16,990 --> 00:00:20,770
Now, I honestly didn't think I would
include heatmaps in this course.

4
00:00:20,770 --> 00:00:23,832
I've seen enough bad heatmaps
to last me a lifetime.

5
00:00:23,832 --> 00:00:28,650
The problem is heatmaps are really quite
powerful when you have the right data.

6
00:00:28,650 --> 00:00:30,370
Weather data is a great example.

7
00:00:30,370 --> 00:00:32,230
You have two dimensions,
latitude and longitude,

8
00:00:32,230 --> 00:00:35,100
and then we can overlay on top
of this a third dimension, say,

9
00:00:35,100 --> 00:00:39,329
temperature or rainfall amounts and
use color to indicate its intensity.

10
00:00:40,370 --> 00:00:42,190
Here's another good example.

11
00:00:42,190 --> 00:00:45,510
This time from the Australian
Transportation Safety Bureau.

12
00:00:45,510 --> 00:00:50,754
And actually on the Wikipedia page for
Heatmaps, it shows the path of an airline,

13
00:00:50,754 --> 00:00:54,795
Malaysian Airlines 370,
which went missing in 2014.

14
00:00:54,795 --> 00:00:59,318
On top of the flight path is a heatmap
showing the probability of the plane if it

15
00:00:59,318 --> 00:01:01,010
had crashed.

16
00:01:01,010 --> 00:01:05,490
This probability is also displayed as
a continuous value, with cool colors,

17
00:01:05,490 --> 00:01:08,430
blues, being used for
low probabilities and

18
00:01:08,430 --> 00:01:12,420
hot or warm colors, reds,
being used for high probability.

19
00:01:13,845 --> 00:01:16,585
So heatmaps aren't all bad.

20
00:01:16,585 --> 00:01:20,005
But where they break down is when
there's no continuous relationship

21
00:01:20,005 --> 00:01:21,795
between dimensions.

22
00:01:21,795 --> 00:01:26,435
Using a heatmap for categorical data,
for instance, is just plain wrong.

23
00:01:26,435 --> 00:01:29,115
It misleads the viewer into looking for
patterns and

24
00:01:29,115 --> 00:01:31,505
ordering through spatial proximity.

25
00:01:31,505 --> 00:01:33,935
And any such patterns
would be purely spurious.

26
00:01:35,260 --> 00:01:38,926
Okay, now you've have the lecture,
let's talk about the techniques.

27
00:01:38,926 --> 00:01:43,630
In matplotlib, a heatmap is simply a
two-dimensional histogram where the x and

28
00:01:43,630 --> 00:01:46,310
the y values indicate potential points and

29
00:01:46,310 --> 00:01:49,190
the color plotted is
the frequency of the observation.

30
00:01:50,280 --> 00:01:53,770
Earlier, we talked about different
statistical distributions and

31
00:01:53,770 --> 00:01:56,160
they work well for this demonstration.

32
00:01:56,160 --> 00:01:59,030
In fact, let's redo our subplots
where we showed a scatter plot and

33
00:01:59,030 --> 00:02:01,130
two histograms, one for each dimension.

34
00:02:02,782 --> 00:02:06,310
In that we define Y values to come
from the normal distribution and

35
00:02:06,310 --> 00:02:08,519
X values to come from
a random distribution.

36
00:02:09,540 --> 00:02:11,981
To plot this as
a two-dimensional histogram,

37
00:02:11,981 --> 00:02:14,061
we just pass it to the hist2d function.

38
00:02:22,572 --> 00:02:26,055
See how the edge colors on the top and
the bottom are very dark purple,

39
00:02:26,055 --> 00:02:29,620
while there are greens and yellows
in the middle horizon of the image.

40
00:02:30,660 --> 00:02:33,609
Now we talked a bit about what
happens when you increase or

41
00:02:33,609 --> 00:02:36,444
decrease the number of bins
with regular histograms.

42
00:02:36,444 --> 00:02:39,493
But how do you think this
changes when using heatmaps?

43
00:02:43,264 --> 00:02:46,690
You see, as you increase
the bins you get the same effect.

44
00:02:46,690 --> 00:02:49,329
Every data point starts to
fall into its own category.

45
00:02:50,380 --> 00:02:52,013
So you get a more clear picture.

46
00:02:52,013 --> 00:02:56,549
But everything eventually is just
one color since every data point

47
00:02:56,549 --> 00:02:58,094
ends up being unique.

48
00:02:59,573 --> 00:03:03,510
Now a legend is important and
heatmaps are no different.

49
00:03:03,510 --> 00:03:05,837
When using the pyplot scripting layer,

50
00:03:05,837 --> 00:03:10,010
this is as easy as calling
the pyplot colorbar function.

51
00:03:10,010 --> 00:03:10,780
Be warned though,

52
00:03:10,780 --> 00:03:14,420
there's lots of accounting and bookkeeping
happening there under the hood.

53
00:03:15,910 --> 00:03:19,446
In fact, pyplot is finding
the current rendered image,

54
00:03:19,446 --> 00:03:23,058
generating the colorbar,
resizing the existing axes and

55
00:03:23,058 --> 00:03:27,170
using gridspec to add a new
axes the size of the colorbar.

56
00:03:27,170 --> 00:03:29,270
It's quite a bit of bookkeeping.

57
00:03:29,270 --> 00:03:31,030
And that's all there is to this though.

58
00:03:31,030 --> 00:03:33,600
Now that you've seen this data in
two different ways, a heatmap,

59
00:03:33,600 --> 00:03:36,330
where you have an X, a Y,
and a color dimension, and

60
00:03:36,330 --> 00:03:39,150
a scatterplot, where you have two
histograms, one for each axis.

61
00:03:40,270 --> 00:03:43,370
So which of these approaches
do you see yourself using

62
00:03:43,370 --> 00:03:45,970
when you have density graphs that
you want to share with others?