1
00:00:08,901 --> 00:00:11,408
We're going to use matplotlib
within the Jupyter Notebook,

2
00:00:11,408 --> 00:00:13,710
just like with did with pandas.

3
00:00:13,710 --> 00:00:17,218
Jupyter has some specialized support for
matplotlib and

4
00:00:17,218 --> 00:00:21,390
this is enabled by using the IPython
magic %matplotlib notebook.

5
00:00:22,593 --> 00:00:27,064
Remember that in the Jupyter Notebook, the
IPython magics are just helper functions

6
00:00:27,064 --> 00:00:31,050
which set up the environment so that
the web based rendering can be enabled.

7
00:00:32,710 --> 00:00:36,510
matplotlib itself can work well
outside of the Jupyter environment.

8
00:00:36,510 --> 00:00:39,790
But for this course, we're going to focus
on its use within the web environment in

9
00:00:39,790 --> 00:00:43,170
part because of how popular Jupyter
has become in data science practice.

10
00:00:44,670 --> 00:00:48,420
So what actually has happened when you
run the magic function matplotlib with

11
00:00:48,420 --> 00:00:53,940
the inline parameter, is that matplotlib
is configured to render into the browser.

12
00:00:53,940 --> 00:00:56,433
This configuration is called a backend,
and

13
00:00:56,433 --> 00:00:59,721
matplotlib has a number of
different backends available.

14
00:00:59,721 --> 00:01:02,859
A backend is an abstraction layer
which knows how to interact with

15
00:01:02,859 --> 00:01:06,170
the operating environment,
whether it's an operating system, or

16
00:01:06,170 --> 00:01:10,990
an environment like the browser, and
knows how to render matplotlib commands.

17
00:01:10,990 --> 00:01:14,130
In fact, there's a number of
different interactive backends, but

18
00:01:14,130 --> 00:01:18,070
there are also backends called hard copy
backends, which support rendering to

19
00:01:18,070 --> 00:01:22,321
graphics formats, like scalable
vector graphics, SVGs, or PNGs.

20
00:01:23,390 --> 00:01:27,590
So not all backends support all features,
especially interactive features,

21
00:01:27,590 --> 00:01:29,340
which we'll touch on later in this course.

22
00:01:30,430 --> 00:01:33,730
Let's import the matplotlib library,
which we'll do as mpl, and

23
00:01:33,730 --> 00:01:37,520
call get backend to make sure
we're using the NbAgg backend.

24
00:01:37,520 --> 00:01:42,730
Great, you'll notice that matplotlib has
a lot of non-Pythonic naming conventions.

25
00:01:43,790 --> 00:01:48,150
In particular, you'll be accessing
variables using the Get and Set.

26
00:01:48,150 --> 00:01:53,030
Which is an accessor pattern which is
more common in languages like Java.

27
00:01:53,030 --> 00:01:56,630
This is just something you have to get
used to, but remember you can hit Tab for

28
00:01:56,630 --> 00:01:59,510
type hint autocompletion
in the Jupyter Notebook.

29
00:01:59,510 --> 00:02:01,930
So this can help you find
the functions that you're looking for.

30
00:02:03,590 --> 00:02:06,930
Now we're not going to spend much
time with the backend layer, but

31
00:02:06,930 --> 00:02:08,670
it's important to be aware of.

32
00:02:08,670 --> 00:02:10,583
If you're on Mac OS, for instance,

33
00:02:10,583 --> 00:02:14,020
your default backend when using
matplotlib is one system.

34
00:02:14,020 --> 00:02:16,710
While on Linux, GTK is my default backend.

35
00:02:17,930 --> 00:02:20,880
The next layer is where we'll
spend most of our time though, and

36
00:02:20,880 --> 00:02:22,740
that's called the artist layer.

37
00:02:22,740 --> 00:02:26,920
The artist layer is an abstraction
around drawing and layout primitives.

38
00:02:26,920 --> 00:02:31,090
The root of visuals is a set of containers
which includes a figure object with one or

39
00:02:31,090 --> 00:02:33,950
more subplots,
each with a series of one or more axes.

40
00:02:35,260 --> 00:02:39,859
This last object, the axes is the most
common one you'll interact with,

41
00:02:39,859 --> 00:02:43,584
changing the range of a given axis or
plotting shapes to it.

42
00:02:43,584 --> 00:02:48,190
And just a brief aside, especially since
this is a global classroom with people

43
00:02:48,190 --> 00:02:52,865
from around the world enrolled where
English might not be their first language,

44
00:02:52,865 --> 00:02:58,242
matplotlib relies heavily on the axes
object, which is the plural form of axis.

45
00:02:58,242 --> 00:03:01,450
But matplotlib also has
an axis object as well.

46
00:03:01,450 --> 00:03:04,740
And an axes is actually made
up of two axis objects.

47
00:03:04,740 --> 00:03:06,900
One for the x, horizontal dimension.

48
00:03:06,900 --> 00:03:08,970
And one for the y, or vertical dimension.

49
00:03:08,970 --> 00:03:12,350
So you might hear me pronouncing
these two words, axes and

50
00:03:12,350 --> 00:03:16,300
axis a bit more intentionally
than I would in regular speech

51
00:03:16,300 --> 00:03:18,240
in order to help make
things easier to follow.

52
00:03:19,840 --> 00:03:23,510
The artist layer also contains
primitives and collections.

53
00:03:23,510 --> 00:03:28,770
These are base drawing items, things
like a rectangle, ellipse or a line.

54
00:03:28,770 --> 00:03:29,910
And collections of items,

55
00:03:29,910 --> 00:03:34,410
such as a path, which might capture many
lines together into a polygon shape.

56
00:03:34,410 --> 00:03:38,700
Collections are easy to recognize as their
name tends to end in the word collection.

57
00:03:40,110 --> 00:03:44,183
It's worth taking a moment to look at
the kinds of artists which are available.

58
00:03:45,674 --> 00:03:49,020
Here's an image from the matplotlib
documentation on artists.

59
00:03:50,150 --> 00:03:53,140
You'll see that there's lots of
child objects of the artists object,

60
00:03:53,140 --> 00:03:56,155
and that one of them is
called patches.patch.

61
00:03:57,560 --> 00:04:00,199
This name comes from the matlab
roots of the project.

62
00:04:00,199 --> 00:04:05,996
And a patch is any two-dimensional object
which has a face color and an edge color.

63
00:04:05,996 --> 00:04:10,095
And children of the patch class
are the primitives, as we've discussed.

64
00:04:11,365 --> 00:04:14,265
Okay, so matplotlib has two layers.

65
00:04:14,265 --> 00:04:18,315
A backend layer, which knows about low
level graphics routines it can render to

66
00:04:18,315 --> 00:04:20,165
the screen or to a file.

67
00:04:20,165 --> 00:04:22,555
We'll use the inline backend for
most of our work.

68
00:04:23,825 --> 00:04:28,635
And the artist layer, which describes
primitives, collections, and containers.

69
00:04:28,635 --> 00:04:31,245
It knows how figures
are composed of subfigures and

70
00:04:31,245 --> 00:04:35,120
where objects are in a given
axes coordinate system.

71
00:04:35,120 --> 00:04:39,158
So with this, we can actually get started
building figures and rendering them.

72
00:04:39,158 --> 00:04:42,254
But there's one more layer which
is extremely important for

73
00:04:42,254 --> 00:04:46,270
us as data scientists in particular,
and this is called the scripting layer.

74
00:04:47,540 --> 00:04:50,090
You see, if we were writing
an application to use matplotlib,

75
00:04:50,090 --> 00:04:52,410
we might never care about
the scripting layer.

76
00:04:52,410 --> 00:04:56,150
But this layer helps simplify and speed
up our interaction with the environment

77
00:04:56,150 --> 00:04:58,600
in order to build plots quickly.

78
00:04:58,600 --> 00:05:02,320
It does this, frankly,
by doing a bunch of magic for us.

79
00:05:02,320 --> 00:05:05,420
And the difference between someone
who is effective with matplotlib and

80
00:05:05,420 --> 00:05:06,810
someone who isn't,

81
00:05:06,810 --> 00:05:10,550
is usually based on their understanding
of this magic of the scripting layer.

82
00:05:11,550 --> 00:05:14,630
The scripting layer we use in
this course is called pyplot.

83
00:05:15,780 --> 00:05:19,170
So that describes
the matplotlib layer cake.

84
00:05:19,170 --> 00:05:22,097
There's a backend,
which deals with actual drawing.

85
00:05:22,097 --> 00:05:26,830
A bunch of artists on top of the backend,
which describe how data is arranged.

86
00:05:26,830 --> 00:05:30,200
And a scripting layer,
which actually creates those artists and

87
00:05:30,200 --> 00:05:31,709
choreographs them all together.

88
00:05:33,190 --> 00:05:36,220
I just want to address one
more topic before we move on,

89
00:05:36,220 --> 00:05:38,760
and that's the difference
between procedural and

90
00:05:38,760 --> 00:05:43,935
declarative visualization libraries and,
well, user interfaces more generally.

91
00:05:43,935 --> 00:05:48,476
The pyplot scripting layer is a procedural
method for building a visualization,

92
00:05:48,476 --> 00:05:52,480
in that we tell the underlying software
which drawing actions we want it to take

93
00:05:52,480 --> 00:05:54,580
in order to render our data.

94
00:05:54,580 --> 00:05:57,803
There are also declarative methods for
visualizing data.

95
00:05:57,803 --> 00:06:00,300
HTML is a great example of this.

96
00:06:00,300 --> 00:06:03,900
Instead of issuing command after
command to the backend rendering agent,

97
00:06:03,900 --> 00:06:09,060
which is the browser with HTML,
HTML documents are formatted as models

98
00:06:09,060 --> 00:06:14,430
of relationships in a document, often
called the DOM, or Document Object Model.

99
00:06:14,430 --> 00:06:17,130
These are two fundamentally
different ways of creating and

100
00:06:17,130 --> 00:06:18,860
representing graphical interfaces.

101
00:06:19,970 --> 00:06:22,919
The popular JavaScript library,
for instance,

102
00:06:22,919 --> 00:06:27,422
D3.JS is an example of a declarative
information visualization method.

103
00:06:27,422 --> 00:06:30,400
While matplotlib's pyplot is
an example of a procedural

104
00:06:30,400 --> 00:06:32,390
information visualization method.

105
00:06:34,050 --> 00:06:38,020
In the next lecture, we're going to get
dirty and start making some graphs.

106
00:06:38,020 --> 00:06:42,420
But before we do that, I wanted you to
check out an article by John Hunter and

107
00:06:42,420 --> 00:06:43,175
Michael Droettboom.

108
00:06:44,520 --> 00:06:48,380
While now a bit dated, this article was
published in the second volume of the book

109
00:06:48,380 --> 00:06:52,680
Architecture of Open Source Applications,
and provides a little more context for

110
00:06:52,680 --> 00:06:55,889
the architecture of matplotlib.