1
00:00:08,746 --> 00:00:12,770
Interactivity and
animation are very similar in Matplotlib.

2
00:00:12,770 --> 00:00:16,850
For interactivity though, we have to head
down to the artist layer a bit more.

3
00:00:16,850 --> 00:00:20,699
In particular, we have to reference
the canvas object of the current figure.

4
00:00:21,740 --> 00:00:24,550
The canvas object handles all
of the drawing events and

5
00:00:24,550 --> 00:00:26,800
it's tightly connected
with a given back end.

6
00:00:27,970 --> 00:00:30,230
If event listening is something
you're not familiar with,

7
00:00:30,230 --> 00:00:33,200
it can be a bit of
a tough concept to grasp.

8
00:00:33,200 --> 00:00:36,200
For decades,
computers have had abstract methods for

9
00:00:36,200 --> 00:00:40,470
doing multiple things at once and now with
multiprocessing and multi-core machines,

10
00:00:40,470 --> 00:00:42,540
there's actually physical ways
Is to do many thing at once.

11
00:00:43,635 --> 00:00:44,780
But even before that,

12
00:00:44,780 --> 00:00:48,480
the abstraction news was largely
focused on the notion of events.

13
00:00:50,050 --> 00:00:53,753
Moving a mouse pointer for instance would
create an event, clicking will create

14
00:00:53,753 --> 00:00:56,816
an event, pressing on keys on
the keyboard would create an event.

15
00:00:56,816 --> 00:01:02,529
And this didn't only happen at the
hardware level such as IRQ interrupts but

16
00:01:02,529 --> 00:01:04,857
at the software level as well.

17
00:01:04,857 --> 00:01:09,199
In fact, event driven programming has
infiltrated most of the ways computer

18
00:01:09,199 --> 00:01:12,730
programmers regularly
engage with software.

19
00:01:12,730 --> 00:01:16,150
From HTML and JavaScript,
down to lower level C code.

20
00:01:17,195 --> 00:01:20,865
You can think of an event as a piece
of data which is associated with

21
00:01:20,865 --> 00:01:22,305
a function call.

22
00:01:22,305 --> 00:01:25,875
And when the event happens, the software
environment, in our case this is

23
00:01:25,875 --> 00:01:29,715
Matplotlibs backend, will call
the function with the relevant data.

24
00:01:30,815 --> 00:01:32,265
Let's look at a trivial example.

25
00:01:33,675 --> 00:01:36,305
We'll create a new figure and
plot some random data to it.

26
00:01:37,330 --> 00:01:39,440
Then we'll create a new
function called onclick.

27
00:01:39,440 --> 00:01:43,670
And this takes one parameter,
which is the event object.

28
00:01:43,670 --> 00:01:45,560
So what's in an event object?

29
00:01:45,560 --> 00:01:48,610
Well, that depends on
the type of the event.

30
00:01:48,610 --> 00:01:50,920
Here we're going to
deal with mouse events.

31
00:01:50,920 --> 00:01:52,220
And they have both an x and

32
00:01:52,220 --> 00:01:56,780
a y value as far as the location of
the mouse in pixels on the canvas.

33
00:01:56,780 --> 00:01:57,930
As well as an x and

34
00:01:57,930 --> 00:02:02,070
a y values first location of the mouse
relative to our data and axis.

35
00:02:03,170 --> 00:02:07,380
So for our onclick we'll clear
the current axis then plot our data

36
00:02:07,380 --> 00:02:10,560
then set the title of the plot to
be variance location of the mouse.

37
00:02:12,430 --> 00:02:16,269
Finally, we have to connect this
events to an event listener and

38
00:02:16,269 --> 00:02:18,984
this process is usually
called wiring it up.

39
00:02:18,984 --> 00:02:22,308
In this case very easy,
get the current figure and

40
00:02:22,308 --> 00:02:26,210
its canvas subject then call
the mpl_connect function.

41
00:02:26,210 --> 00:02:29,050
Passing in the string for
button_press_event as well as

42
00:02:29,050 --> 00:02:33,100
reference to the function onclick, which
will be call when the event is detected.

43
00:02:35,110 --> 00:02:38,960
Now when we click on our plot we see
the most information printed to the title.

44
00:02:41,300 --> 00:02:45,590
The Matplotlib documentation describes
the kinds of events you can listen from.

45
00:02:45,590 --> 00:02:48,680
But whether they work or not,
depends on the backend you're using, and

46
00:02:48,680 --> 00:02:51,380
some backends are not interactive.

47
00:02:51,380 --> 00:02:55,200
Button presses, key presses,
scroll events and figuring axis enter and

48
00:02:55,200 --> 00:02:56,650
leave events are almost common.

49
00:02:56,650 --> 00:03:00,520
But the most important event for
us is the pick event.

50
00:03:01,868 --> 00:03:04,630
The pick event allows you
to respond when the user is

51
00:03:04,630 --> 00:03:07,130
actually clicked on a visual
element in the figure.

52
00:03:08,570 --> 00:03:09,490
Let's look at an example.

53
00:03:10,730 --> 00:03:14,020
For this, I want to create a list
of countries in randomized order.

54
00:03:14,020 --> 00:03:16,939
We can do this with the shuffle
function from the random module.

55
00:03:33,229 --> 00:03:37,294
Then I'll create a new DataFrame with
random values for height and weight, and

56
00:03:37,294 --> 00:03:38,760
another column for origin.

57
00:03:48,808 --> 00:03:50,630
Great, now we have some
C data to play with.

58
00:03:51,970 --> 00:03:56,000
Now we can create a scatterplot with
the x and y values as the height and

59
00:03:56,000 --> 00:03:58,420
weight respectively,
pulling from the data frame.

60
00:03:59,420 --> 00:04:03,840
Note that we've added a new argument
here called picker and set it to 5.

61
00:04:03,840 --> 00:04:08,460
This informs the Matplotlib backend that
the mouse doesn't have to click directly

62
00:04:08,460 --> 00:04:12,560
on a rendered object that can
be up to 5 pixels away and

63
00:04:12,560 --> 00:04:15,030
it should find the closest object.

64
00:04:15,030 --> 00:04:18,000
What works for you depends on how
you're rendering your charts.

65
00:04:18,000 --> 00:04:19,980
With axis labeled, let's go render this.

66
00:04:21,360 --> 00:04:24,460
Okay, nothing special with this chart yet
but let's wired it up.

67
00:04:25,690 --> 00:04:29,680
We'll create a function called onpick,
which takes on an event.

68
00:04:29,680 --> 00:04:33,820
This event is a pick event which has
different data than the most event.

69
00:04:33,820 --> 00:04:34,810
In particular,

70
00:04:34,810 --> 00:04:38,789
it's got an index value which happens to
correspond to our index and the dataframe.

71
00:04:39,790 --> 00:04:42,400
Now Matplotlib isn't aware
of the data frame but

72
00:04:42,400 --> 00:04:44,580
it renders the data in the same order.

73
00:04:44,580 --> 00:04:49,650
So we can use the data frame iloc indexer
to pull out the origin information.

74
00:04:51,340 --> 00:04:53,660
Now you can see that when
you click on an item,

75
00:04:53,660 --> 00:04:55,720
it renders the origin as we would expect.

76
00:04:57,300 --> 00:05:00,350
And there we have it,
a fairly straight forward example of

77
00:05:00,350 --> 00:05:03,440
adding interactivity to your graphs and
plots.

78
00:05:03,440 --> 00:05:07,043
Now I'll be honest, this looks
like very little code to write and

79
00:05:07,043 --> 00:05:11,694
this lecture was pretty short but there is
a lot of hunting through documentation and

80
00:05:11,694 --> 00:05:16,084
forms that you have to do it in order to
understand the details of the events which

81
00:05:16,084 --> 00:05:17,546
are being passed around.

82
00:05:17,546 --> 00:05:21,434
Pythons lack of static typing has
an unfortunate side effect and

83
00:05:21,434 --> 00:05:26,120
it matches the documentation for
features like events is buried or missing.