plt.text Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 16:28:18 Hi! Everybody! Welcome back in this video, we continue to learn about Matt Pot Lib. 16:28:23 And we learn about how you can add text directly to your plots. 16:28:27 Let me go ahead and share that Jupiter notebook 16:28:34 Okay, so let me go. And we're gonna open number 7. 16:28:38 So we've learned how to make line plots. We've learned how to make scanner plots. 16:28:43 We've learned about the ordering of plot elements let's go ahead and learn how we can add text directly to our plots. 16:28:51 So in a way that do this is with the text, command, so this can be plot, text, or ax. 16:28:55 Text both work. We're gonna use pltot text. 16:29:00 I think, for most of this one. So the way Plt. 16:29:03 At taught text works is you're gonna put in your horizontal position, your vertical position, followed by the text. 16:29:09 So I'm gonna put make a figure. And then append the text to an axes. 16:29:16 Object on that figure. So my horizontal position will start at 0 point 4. 16:29:21 My vertical position will start at 0 point 4 5, and then it will just say, Hello! 16:29:27 There! Now, this is the horizontal and vertical position of the bottom left hand corner of the text, so the little bottom left hand corner of my capital. H. 16:29:40 Okay. 16:29:42 Alright. So, yeah, that's it. Now we know how to add texts. 16:29:47 So how can we customize the text to make it look the way we would like? 16:29:50 So there's a variety of ways we can customize the text. 16:29:53 You can learn about all of them here at the text documentation link, we're gonna go ahead and go over. 16:30:00 Probably the most, the ones that you're gonna wanna know the most. 16:30:04 So the first one is, how big can we make our text? 16:30:07 That's the font size argument. So same same plot as before. 16:30:11 But now we're gonna go ahead and change and make the font size bigger. 16:30:16 So let's go ahead and say 18 font size equals 18. 16:30:20 And now my font is bigger. We can also change the color of the text, and the background color, so the color of the text can be controlled with the color and the color of the background color can be controlled with the background color argument so first, I'm going to have an example where I change. 16:30:40 The text to be read, so color equals red. Okay. Now, I have a red text then I'm gonna change the background color to be yellow, almost like I'm highlighting it. 16:30:49 So background color equals yellow. 16:30:53 And that's almost like I ran my highlighter over it, and then this last one I change both at the same time to have a black background with a white text. 16:31:02 Okay, you can change the opacity with the Alpha argument. 16:31:08 We've seen this before with scatter and plot, so I'm not gonna take time going over it here. 16:31:13 You know how it works. You can change the font style. 16:31:17 Using these arguments. So you can have the font. Family argument, and then you can choose any of these options so I'm going to have an example where I set my font family to cursive. 16:31:27 And you can see what the cursive font looks like. 16:31:30 It looks pretty nice 16:31:33 So in a previous notebook we had an example where we had some legend text, where I put dollar symbols in there. 16:31:41 So dollar symbols are allowing you to type things the way you would in law, tech or latex. 16:31:48 So this is sort of software that people use to type things in mathematical mode. 16:31:55 So that way math text looks nicer, and it's easier to type than in, say, like a Microsoft word document. 16:32:03 So you would put whatever you want to put mathematically. 16:32:05 You put them between $2 symbols, and then, if you have something like a math function that you would like to look nice, you do like it's typically pre-programmed. 16:32:14 So you could do like a slash. Synd to get the sign function written out nicely. 16:32:19 A slash log to get log written out nicely, and so forth. 16:32:22 So, if you are not familiar with law tech at all, and you do want to write something in like a math expression. 16:32:29 You. I encourage you to do a web search to be like la tech square root, or something like that. 16:32:36 And you'll be able to find examples of how to do it. 16:32:39 So here's some examples where I append, like 2 plus 2 equals. 16:32:43 4. Sign of pi equals 0 square the square root of a squared squared plus b squared equals plus or minus c. 16:32:55 So this is a nice feature to have in in plots. 16:32:59 Okay, this works not just in the text argument, but also in labels for legends and titles and access labels. 16:33:09 Basically anywhere you can do text. So let's look at this example. 16:33:14 It's still gonna be. Hello there! But now my horror, my horizontal position starts at point 9 16:33:23 So you can see the Hello there does start at point 9. 16:33:27 But Hello! There is so long at this font. So why, that it doesn't fit within the regular axes limits of 0 to one regular when you don't have any data elements on it so unlike with plot and scatter the limits of the horizontal or vertical 16:33:48 Axes of the axes. Object, don't adjust. 16:33:51 In response to the text. So this is something where, if you want your text to fit entirely within the axes, and you don't have any other data elements on it, or you don't have a data element that moves your text that far or moves your axis limits that far you're gonna have 16:34:07 To set those yourself so this can be done with either plt excellent, and plot Ylm or ax set underscore xlim or ax set underscore Ylm. 16:34:19 So we're gonna use the Plt dot Xlim version. 16:34:24 So I do, Xlim, and then to increase the bounds you first put in you're going to put in a list of twouple an array, and then the first entry is the minimum. 16:34:33 So for us, we're gonna stick with 0. And then the second entry is, how far, how far to the right you want to go. 16:34:40 So for us, let's say 1.2 5 16:34:44 So now you can see that my X-axis starts at 0 and goes all the way to 1.2 5. 16:34:51 So you might be wondering why we would want to put text directly onto a graphic. 16:34:57 That's a reasonable question. There's a number of reasons. 16:34:59 So sometimes you want to provide your audio some instructions, directly on the plot. 16:35:05 So sometimes like, for instance, on 5 38, they have interactive plots, and like, there's little instructions on move your cursor here to see this stuff like that other times. 16:35:16 It's useful and not just useful, but best practices to label your data directly instead of using a legend. 16:35:24 So if it's possible, it's better to label your data directly using text annotations instead of using legend. 16:35:31 And one of the main reasons for this is that if we go back to our legend, example, and this previous notebook 16:35:39 Due to 2. Basically, the idea is that the audience eyes have to when they want to understand what the blue line is. 16:35:48 They have to take their eyes off the blue line. Go to the legend, see what it is, and then go back to the blue line. 16:35:53 And so this is sort of makes it harder in and other examples. 16:35:58 Not this one where it's a little bit simpler, but in other graphs, where it's a little bit more complicated, it makes it harder for your audience to see and understand what the pattern of the data is because they have to keep going back and forth to get this possibly critical information from the 16:36:14 Legend with their eyes. So every time they have to take their eyes off of the data to look at the legend, that's time they're spending, not looking at your graph in some sense. 16:36:23 So the best practices is, instead of doing a legend, just label the graph directly. 16:36:28 So I could put the word sine of X here, or the label sign of X here, and the label Cosine of X here, and then they look at the label, and it's right next to the thing that it's labeling. 16:36:39 So that's what best practices are considered. It's not always possible. 16:36:44 But if it is possible, it's encouraged that you label it directly. 16:36:48 So let's return to that example. But now I'm going to use text to label it. 16:36:52 So in this example. This, I guess I didn't need to look up that this is the legend of now I'm gonna use text to label it directly. 16:36:59 So now the label cosine of X and sine of X are immediately next to the lines that are cosine of X and sine of X. 16:37:06 Alternatively, maybe you prefer to lead. Read from left to right. 16:37:11 You can put those labels on the left hand side like, so, okay, so when you do stuff like this, I will admit this takes a little bit of trial and error. 16:37:21 So I had to try a few times to come up with these numbers for placing them there the horizontal and vertical positions. 16:37:29 But I personally like this better than the legend, and this seems to be the consensus and the data visualization community as well. 16:37:36 So now we understand how to add text annotations to our plots. 16:37:43 That's a pretty powerful tool when it comes to connecting additional information to our audience. 16:37:47 We also? What else did we learn? We learned how to style our text, and we also learned a little bit about data visualization. 16:37:52 Best practices. So that's a lot for one notebook, in the next notebook. 16:37:57 We're going to continue to add more tools to our mat plot, live tool, chart, toolbox, and learn how to make various plots that use the rectangle to convey information. 16:38:07 So I hope you enjoyed learning about how to add text to your plots.