- In this Intro to Data Science section, we're going
to talk about dynamic visualization.
We'll do that over the course of a couple of videos here.
You may recall from the proceeding Intro to Data Science
section where we first introduced static visualizations
that we did a die rolling simulation.
We rolled a six sided die some number of times,
and we talked about the fact that if it's truly random
each of those six values have an equal probability
or likelihood of being chosen every time we pick
a random number.
Every die face on a six sided die has a approximately
a one in sixth chance of being chosen on a given
random roll, and that equates to approximately 16.667%.
You may recall that we ran that static visualization
several times.
First we started with only 600 die rolls, and we saw
that yes all of the die faces came out close
to 100 each, which is what we would expect if we have 600
total die rolls.
But they weren't so close that the bars looked
to be about the same height.
Then we ran the example again, and we did 60000 rolls.
Now the bar heights were getting closer to being even.
Then we took it a step further and did six million rolls,
and we got nearly one million of each the different faces.
Those static visualizations were helpful for starting
to understand the law of large numbers
which shows that as we roll those dice more and more
and more that we narrow in on, or close in on,
the 16.667% we expect for each of those die faces.
As helpful as that was, to really get a sense
of the law of large numbers in action, it's helpful
to do it with an animated visualization.
A dynamic visualization that changes as we roll the dice.
THat's what we're going to focus on in this example.
In our static visualizations, we ran the simulation first
then displayed the results.
With dynamic visualizations, we're going to take advantage
of a capability from the Matplotlib visualization library
called a FuncAnimation in which we use a function
to drive an animation that updates the visualization
dynamically every period of time in our particular case.
As you'll see momentarily, every approximately
33 milliseconds we're going to update what is presented
on screen, so the bars will be jumping and dancing
and you'll really get a sense of the law of large numbers
in action.
The way FuncAnimation works is it drives
an animation one frame at a time, what we call
a frame-by-frame animation.
Every animation frame is going to specify everything
about the diagram that needs to be updated during
one individual update.
By stringing a whole bunch of these together,
we will get that animated effect that we're looking for.
In this example, every animation frame we create
is going to roll the dice a specified number of times.
As you'll see momentarily, we've created this as a script
where you can specify arguments to the script
that indicate how many dice to roll and how many
animation frames to perform.
We're going to on each frame clear
the current visualization that we had already displayed,
and create an entirely new visualization with all
of the updated die frequency information.
The more frames-per-second you can display,
the smoother your animation is going to appear.
For example, video games with fast-moving elements in them
generally are going to try to aim for at least 30
frames-per-second.
Nowadays, it's not uncommon to have 60 frames-per-second
or higher for some of these gaming devices
that are now available to us.
The amount of work, however, that you do in each
of the animation frames in a FuncAnmation can effect
your animation speed, so we do want to try to minimize
the amount of work we do in each frame to keep
that smooth animation going.
In this example, I mentioned a moment ago, we're going
to use approximately 33 milliseconds between
our animation frames, which will give us
about 30 frames-per-second.
If you divide a thousand milliseconds by 33 milliseconds,
that gives you about 30 frames-per-second being displayed.
Let's actually demonstrate the final result,
and then in the next video, we'll start talking about
the code that enabled us to build the dynamic visualization.
Let me switch over to a terminal window here.
You can see I've listed out the contents of my ch06 folder,
which contains all of the code for the lesson
we're currently talking about.
The script that we're going to look at starting in the next
video is defined in the RollDieDynanic.py file.
If we I would like to run this file, I'm going to go ahead
and type ipython followed by the name of the file.
This particular example also demonstrates for the first time
how to process command line arguments in Python.
We're going to allow this script to receive two
command line arguments.
The first one is going to represent the number
of animation frames we would like to perform in total.
This going to also control the overall duration
of my animation.
Let's say I wanted to do 6000 animation frames.
The second command line argument is going to represent
the number of dice to roll in each animation frame.
To start out, I'm going to do 6000 die...
6000 animations frames, one die roll at a time.
That's going to enable us to really see the bars changing
on a die-by-die or a roll-by-roll basis.
Let me go ahead and run that and get that on the screen here
and we'll get a chance to see it in action.
After I let this run for a little bit here,
we'll kill it off and start it up again rolling
more dice at a time-per-frame.
As you can see here, it's pretty slow in terms of updating,
but there's a couple of things that you can see
going on here.
First of all, the dynamic visualization.
So you do see the bars and the text changing
as we roll the dice.
You can see how many die rolls there are at a give time here
Also, notice that Matplotlib and Seaborn,
the two libraries that we're using for the visualization
here, are capable of dynamically adjusting
the y-axis here to represent the magnitudes
of the bars that we have.
The longest bar is effecting the total height
of the diagram at any given time.
Right now, we have more fives than we have of anything else.
Over 20% of our rolls so far have been fives.
Now it's decreasing a little bit.
But you can see the bars heights are bouncing around,
the number of each face is changing dynamically.
I'm going to go ahead and kill this off here
because it's kind of slow when we're only rolling
one at time, so we'd have to wait a pretty long time
before those bars actually balance out to demonstrate
that law of large numbers in action.
What we're going to do next is run this thing again,
but this time, let's say we want to do 10 thousand
animation frames, actually it doesn't really matter
cause I'm going to cut it off a little bit short,
but let's say we want to roll 600 dice at a time.
If I roll 600 dice at a time, that's 600 dice
and we're trying to do that 30 times-per-second.
So that's 18 thousand dice per second that we want to roll
to try to see how that effects the animation in this case.
Let's go ahead and run that.
In a moment window will pop up, and you can see
it's cranking through die roll super quickly now.
Notice right away that the bars are already starting
to even out, and that all of our percentages
are in the 16% range.
You can now really see that law of large numbers in action.
The more dice we roll, the faster we converge on 16.667%.
We're already up to 160 thousand plus rolls here,
and the bars are almost even in height based on the current
die frequency values.
This really emphasizes that law of large numbers in action.
We're going to terminate this by closing the window,
and in the next video, I'm going to show you how we
created that animation in the context
of the RollDieDynamic.py script.