bokeh Static Plots Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 16:13:49 Hi! Everybody! Welcome back in this video. We're continue to learn about Boca and talk about how we can make static plots using this package. 16:13:59 So in this notebook we're gonna see what it what you're gonna need to import from Boca in order to make a plot within a Jupiter notebook will then go over the standard procedure for producing plots with Boca and we'll talk about the figure objects in 16:14:14 Boca, and then we'll also now learn how to do scatter in line plots along the way. 16:14:18 We will also define various terms in Boca, including a glyph so normally, in these notebooks we do all of our importing at the very top of the notebook after our little intro section. 16:14:31 But for Boka I wanted to just review what things you're going to need to import specifically before I just start putting it at the top. 16:14:39 So the things that we're going to need to import in order for this packages, figures to display within a Jupiter notebook is, there are 3 things the first thing is we need a function that's going to go ahead and tell the package that are output is being 16:14:55 Displayed within a Jupiter notebook. So that is this output notebook function. 16:15:01 So we're going to import this. And then eventually, we will call it. 16:15:03 And once we use run this function. Boca now knows. 16:15:07 Okay, my plots are being displayed within a Jupiter notebook environment. 16:15:11 The next thing we need to import is the show function. So we don't have to necessarily import this. 16:15:18 But it'll make our lives easier if we do import it. 16:15:21 So the show function is going to be what we call when we would like to display our figure. 16:15:26 So just like with Matt. Plot lib. We have to do plot show in Boca. 16:15:32 You're just going to call the function show directly. And then finally, we're going to import the figure class. 16:15:39 This would be as similar to if we imported that classroom at Plat live. 16:15:44 But to make our lives easier with Boco. We just import it, and this is stored in Boca. 16:15:48 The plotting module of Boca. So what's go ahead? 16:15:52 And code up the importation of these modules, classes, functions, etc. 16:15:57 So we're going to do from Boca dot I/O. 16:16:01 We'll import output notebook and show, and then we're gonna do from Boca Dot, plotting 16:16:12 Import, figure. Now, we're gonna import pandas and numpy for data handling we've seen this before. 16:16:20 And then the last thing we have to do is call output, output notebooks we actually have to run this function. 16:16:27 So output notebook, and now you'll see if you're running all the code along with me, or if you're just watching my screen, you should see like Boca. 16:16:38 Js, whatever version you're using successfully loaded. 16:16:41 Along with a little rainbow wheel on top of that statement so this means that you've run output notebook. 16:16:48 The Javascript running in the background, has been loaded into your Jupiter environment, and we're ready to start making some figures. 16:16:57 So let's go ahead and first make our very first Boca figure object. 16:17:01 So, in order to make a book of figure objects you just have to call figure. 16:17:07 And it is common to store this. If a variable and per the Boca documentation, the standard is just the letter P, so we're going to store. 16:17:16 P. This figure within the variable P, and at the same time we're gonna set both the width in the height of it. 16:17:24 And when you create a figure with Boca in order to set the width in the height, you just have to set the width and the height arguments of the figure class. 16:17:32 So I'm gonna set my wits to be, let's say 400 and my height to be 300 16:17:44 Now you'll see that we're displaying the figure and it's entirely empty. 16:17:48 So this white square here that I'm gonna zoom in on. 16:17:51 This is our figure object, but it's entirely empty, because we haven't put anything on it. 16:17:56 And so when you do this, you should see a warning, and then it's just warning you that, hey? 16:18:02 Wait a minute. You made a figure, and you asked me to show it. 16:18:05 But there's nothing on this figure. So we're gonna worry. 16:18:09 We're gonna fix that warning. It's no longer gonna happen because our figures are gonna start to have stuff on it. 16:18:16 And what is the stuff that our figures are? Gonna hold? 16:18:18 They're known as glyphs. So in Boca we refer to the graphical elements of our figures as glyphs so like a scatter point marker would be a glimpse, a line that's been plotted would be a glyph if we did a 16:18:31 Bar, chart, the bars would be glyphs, and so on. 16:18:35 So different glyphs use different functions in order to be added to the plot. 16:18:41 So the first one that we're gonna learn is gonna enable us to make a scatter plot. 16:18:46 So the first glyph? The first glyph type we're gonna learn about is the circle. 16:18:51 Who's documentation has been linked to here. So this circle Glyph, is going to allow us to add various circles to our plot that correspond to the X and Y coordinates that we give it so the horizontal and vertical axes coordinates so 16:19:10 i'm gonna make some data using numpy. So here's X, here's y. 16:19:13 So y is just actually x squared. And then what I'm gonna do is I'm going to plot these on a figure by using the circle command. 16:19:24 So first I make a figure. It's 400 us wide. 16:19:28 300 units tall, and then, in order to add a circle to the figure, you start with the variable you use to store the figure which for us is the letter P. 16:19:37 You're gonna do. P dot circle. Now for the X. 16:19:41 Argument. You're gonna call X equals. And for us the horizontal coordinates are stored in a variable called X 16:19:49 For the why arguments, the the coordinates. 16:19:53 We want are stored in a variable called Y, and now we're just gonna go ahead and show off a number of the arguments that you can use for the Circle Glyph command. 16:20:05 So first, you can call size, and it will set the size of the circle this is the diameter of the circle that's getting set. 16:20:12 So if I set 20, my circles are going to be of diameter. 20. 16:20:15 And whatever units the documentation specifies, we're going to set the color of the interior of the circle with, I believe, face color. 16:20:25 So the face of the circle is the interior and I'm gonna set it to be. 16:20:29 Let's say, red 16:20:32 I can set the edge of the color edge line color with line color. 16:20:37 And let's set this to be black and just like with Matt. 16:20:42 Plot lib. I can set the opacity of my glyph with the alpha argument. 16:20:47 So, Alpha, and then let's say it's point 5, and I forgot a comma. 16:20:52 There we go! 16:20:58 But but not face color. Sorry about that. Fill color, so replace face color with Phil color, and then we're good to go. 16:21:08 So now through the circle, command, I've plotted these points that correspond to X comma x squared, and each point has a red circle. 16:21:18 That's 50% opaque with a black outline. 16:21:23 Okay, so there are a lot of other aesthetic parameters that you might be interested in. 16:21:31 So you can do that by checking out this documentation, and you can see all these different arguments here and these provide some of that. 16:21:39 So like. For instance, one of them, if we go through one of them was hatch pattern, so I could do something like 16:21:51 Hatch pattern. And we're gonna I'm just gonna take a shot in the dark that this works and if it doesn't, then there we go. 16:21:58 Now, I've added a hatch pattern to my circle. 16:22:01 Okay, so. 16:22:04 This adds a hatch pattern to the circle 16:22:12 So there are other scatter marker types. So we added a circle. 16:22:17 But maybe we, instead of circle markers, we want square markers. 16:22:21 So this is an example. So instead of circle, you can call square, and then put in the various arguments that you're interested in. 16:22:29 So here are gray squares and if you're interested in knowing what are all the various arguments to square for the different plotting marker types for the scatter plot, all I have to do is replace the word circle with the word square and now i'm at the 16:22:47 Documentation page for the square. Scatter plot marker. 16:22:51 So Boka provides this very nice diagram, showing off the various markers that they have available. 16:22:58 So we've seen circle, and we've seen square. 16:23:01 But they also have ones like the asterisk, a circle with a cross through it, a circle with a dot, a dash across a circle with a Y dens, a plus sign triangle, hexagons. 16:23:16 Etc. And so just like with the example that I just showed where we got the documentation for the square. 16:23:24 We could. For example, get the documentation for inverted triangles by replacing the word square with inverted triangle. Okay? 16:23:34 And you can see now I have the different arguments that are available for inverted triangle 16:23:41 So we saw some examples up above where I provided a string or a number as input to one of the aesthetic options. 16:23:50 But we can also input a vector or a a list and tuple an array. 16:23:56 So here's going to be an example where I generate some random sizes and some random colors, and then I'm going to use those as arguments to size and fill color. 16:24:05 So now, when I call size instead of specifying a unify, a uniform size, I'm going to provide this rate array of sizes which we can look at here. 16:24:14 So here was sizes, and instead of providing a single phase color for all the circles I can provide an array of randomly selected colors between red and gray and we can see how this alters the appearance. 16:24:28 So all the circles now have different sizes and different fill colors 16:24:35 So this means that Boca is a little bit closer to Matt plot Lib than Seborn in the sense that if I wanted to color points by the columns value. 16:24:45 So in this instance the colors were provided as a list, so if it comes time where I'm working with a data frame, and I'd like, for instance, in the Penguin examples, right? 16:24:54 If I wanted to color my scatter plot markers by the species of penguin, I couldn't just provide an argument like species. 16:25:04 I'm going to have to provide an argument or an array of color options that I generate. 16:25:09 But don't worry. We are going to learn some nice, easy ways to generate those in a later notebook. 16:25:17 In addition to just calling the glyph on its own like circle or square, you can use the scatter function and then just provide a marker argument. 16:25:26 So here I am with P. Dot, scatter so this will create a scatterplot, and then at the end, if I want a different marker. 16:25:33 So, for instance, I'm going to make one with diamonds, and instead of circles, I'm just going to call Marker equals to Diamond. 16:25:43 And now I've produced a similar, the same exact plot here. 16:25:47 But now my markers are diamonds instead of circles. 16:25:52 So that's really, you know, a good foundation for how to make scatter plots with Boca before we move on to line plots. 16:26:01 I also want to show off some functionality of Boca. 16:26:04 So you might notice this little series of tool tips over here on the right. 16:26:10 So this is something that's nice about Botho within the Jupiter notebook. 16:26:14 The way we've run it is we can interact with these plots. 16:26:17 So the little arrows right here the the cross arrows allow us to move the axes around. 16:26:24 We can also do a zoom to a specific box. 16:26:28 So here I am creating a box, and now we'll zoom in 16:26:35 I don't remember what this one does. So this one, I think I don't know what wheel zoom is. 16:26:40 I think I just okay. We all zoom. You just kind of zoom in on your like you would on your computer by wheeling around on your mouse pad. 16:26:51 This will save the figure which we're not gonna do right now and then. 16:26:55 This button right here. Reset we'll just go ahead and put the figure back to what it was before. 16:27:02 So there are other little tool tip options that we'll see, maybe in later notebooks, or you can find out about in the document. 16:27:10 So I said, we learned we were gonna learn scatter plots and line plots. 16:27:15 We've already learned line or scatter plots. So it's about time we learned line plots so a line graph can be created in Boca with the line gift glyph function. 16:27:24 So here's that documentation page so all I have to do is, do my figure object. 16:27:31 Period, and then the word line. And then I'm gonna call my X arguments, followed by 16:27:41 My! Why are you? And so now I've plotted the x's and y's which, remember, x is negative. 16:27:51 5. Negative 4 all the way up to 5, and then y is just x squared. 16:27:56 So just like with Matt. Plot lib the default behavior is to plot these points and then connect them with straight line segments and so that's why this plot of the parabola looks a little bit blocky, right? 16:28:11 Because we're just doing the integers. So there's a lot of data in between one and 2, for instance, that isn't included, but because we're connecting with straight lines. 16:28:20 It doesn't look smooth. Okay. 16:28:25 So, line if you check out the documentation also has a bunch of aesthetic arguments that we can look at. 16:28:33 So, for instance, we can set the width of our line to be 5 with the line width argument we can set the color of our line with the line underscore color argument. 16:28:45 So here, I say, I want to make it orange, and we can set the dash pattern of the line much like the dash pattern setting and Matt plot lib where I can just do a list or a tuple of the length of the dash portion and the length of the empty 16:29:01 portion. So, according to my comments here, I want to do one with 30 and 4. 16:29:06 Okay. 16:29:11 It it's possible to add multiple gliffs to the same plot. 16:29:15 So, for example, I can add both a line plot and a scatter plot on the the same plot just by calling them so first, I'm going to add the line plot. 16:29:27 So p dot line, and then afterwards I add a scatter plot, using P dot circle, and if you can notice, we can zoom in that the circle. 16:29:39 Second the line is behind the circle. If we were to go back and change the order, I believe the circles will then be behind the line 16:29:50 Okay. See? 16:29:55 Alright. Let's go ahead and take that back to where we were 16:30:00 I also want to demonstrate that, just like with Matt plot lib the axes can be made to play nicely with date time data. 16:30:09 We just have to provide an extra argument when we define our figure. 16:30:14 And so the argument we have to provide is X 16:30:19 Access. 16:30:22 Format, and then is equal to date time. Hmm, let me actually check the documentation for figure 16:30:42 Okay. Not. X-axis format, but X-axis type. 16:30:46 So instead of X-axis format, I want X-axis type, and then still set equal to date time. 16:30:53 So what I did was generate some new data where the horizontal variable is a series of date times, and the vertical variable is just like a random walk. 16:31:02 Okay, so this will plot the date times of X nicely along with the Y values. 16:31:10 Okay? And here we can see the format of the axes is in a date format. 16:31:19 Okay, so we now know how to make both scatter plots and line plots with them. 16:31:24 Boca. Importantly, we've learned what the figure object is within the Boca package, and we are now ready to continue to learn more about plotting functions in Boca and in particular, we're gonna learn how we can use Boca to change the aesthetics. 16:31:41 Of our figures. Okay, I hope you enjoyed the watching this video.