Transcript:
Hey, there today we’re talking about one of my favorite ways to improve the communication of my matplotlib visuals, and that’s by adding text. You might enhance your visual by annotating specific examples or you might decide to completely remove your legend and label your lines directly, so after I walk you through the basics, we’ll be going through some specific examples, so you can see how I add text to my visuals. Let’s get started, so we’re going to start off coding by importing some of our usual suspects. Pipelot numpy as well as Seabourn for some additional styling, and I’ve also created two arrays here. These are just for plotting purposes. Were calling them X and Y so we have a scatter plot of these data right now and one thing we might want to do is annotate a particular point on this plot, so let’s do that to add text to this plot. Its Pi plot dot text, and now we need three different arguments for this function. The first will be our X position y position, and then finally, the string that we’d like to add to the plot, so let’s put this at 4 5.5 and we’ll call this the peak point there. We go so one thing to note here. These X and Y positions are in terms of the coordinates on your graph, right, so 4 and then 5.5 so it goes along with the axes by default and let’s put just a bit more text on here to make sure that we’ve got it. Let’s also annotate the low point on our graph so plttext we can continue, adding as much text as we’d like. Let’s put that at 2 1.3 and we’ll call that our low point there we go and so it’s at 2 and 1.3 so you might have already noticed that when we pass in those X and Y coordinates. The part of the text that’s lining up with that position is going to be the bottom left corner by default, but you can actually change this by setting your horizontal or vertical alignment, so let’s try horizontal alignment. We can reference that as H A and if we set that to be center, take a look at that peak point text, it will now align, so that’s center aligned with the position that we’ve given it, and, of course, down here on the bottom, we can also vertically align that text. If we’d like that’s going to be VA, let’s also set that to center. This one’s going to be more subtle, but now the low point text has vertically center aligned with the X and Y coordinates that we’ve given it. Now, let’s learn about styling those texts by looking at a few specific examples, so when it comes to styling that font, there’s a lot that you can do. Let’s take a look at some examples and here I’m showing you a specific example from my previous baseline video, so this is some hypothetical student grades and what I recommended in that video is to potentially add a baseline that tells us what the maximum possible grade is. So if you’re doing this. If you’re adding a baseline, you might want to label that baseline, which is what we’re going to do now, so let’s add some text plttext still the same. Let’s put it at 2.5 and 104 and we’re going to call this maximum grade. This is the maximum possible grade. Okay, so we’ve written maximum grade right above the baseline. This helps our audience know what that line represents, but let’s also style that text so that it matches the styling of the baseline. So the first thing we might want to do is increase the font size that’s with this property called font size and we can set that to be whatever point we’d like, so that’s made the font quite a bit bigger. We can also set the color of this font with the word color, so I’m actually going to match the color to exactly what it is with the line. Let’s just copy paste. This is Xkcd’s brick, red color. All right, great one. Other thing I might want to do is make that font bold since that line is quite dark, so let’s also do that. That’s font weight. Let me set that to be bold, and you might even decide that you want italicized font just to kind of draw everything together. This one’s more aesthetics than anything else. But if you do want Italic font, that’s going to be font style and we’ll set that to be italic there we go so lots you can do to style the font that you are putting on your figures Besides just adjusting the styling of your font. You can also put a little box around your text as well. So right, now I’ve identified in this figure. There is one particular critical point that I’ve added a circle to this figure, but let’s also label that so that everyone knows what that circle means, so we can do that by adding some text to this figure. We write plttext and now I’m just supplying where I’d like the text to be. Let’s put it at six five and I’ll write in critical points. Okay, um, so we could, of course style the font again. But one other thing we could possibly do is just add a bounding box and we’re going to do that with this property called be box. So what be box will take in is a dictionary of all the different styles that you would like to apply to your bounding box, so for us, let’s change the face color of this bounding box to be red and let’s also set its transparency, so if we set transparency, that’s going to be this property called Alpha, so there we go, We’ve put a little box around our critical point text, and we’ve also made that red as well. There are two more things. I want to show you about text because they come in really handy, and I use them quite often, so that’s by adding text at a general position or adding text by a variable. Let’s take a look. So if you want to add text. Based on a general position rather than specific X and Y values, you can include this property called transform and set that equal to X dot transaxes. Let me walk you through a function and show you how it works. First of all, I’ve defined this new function called add mean line and what it does. It accepts data. It will calculate the mean of that data and then we’re going to grab the current axes of whatever figure I’m working on, add a vertical line at the mean position and we’ll also add a little bit of text that says what the mean is equal to because I want this function to work on all kinds of different plots. I don’t necessarily know what the X and Y values will look like, so what I can do is add this transform equals ax dot transaxis and that transforms all the positions on my grid to range between 0 0 and 1. 1. So when I now have X and y positions here 0.7.9 these are going to be normalized positions so that this is always going to be the upper right corner. No matter what my X and y axes look like, so let’s try this one out. Here’s my function. Add mean line and I’m going to add it to this figure that happens to be a histogram, so all that function needs. Or what are my data here? Those are values. And here we go, so we add a mean line and we write in what the mean is and just to demonstrate right now. My data points are coming from a normal distribution of mean 10 I could increase that. Let’s say 20 and this function is still going to work. I’m still going to be able to write mean right here in the upper right corner. No matter what that mean actually shifts to the final thing, I want to show you is how to write in text based on a variable, and I actually think this is probably how I use text the most often in my figures, so lets. Try it out right now. I have some student grades represented in a bar plot. These are the same student grades from before, but one thing I might want to do is directly label those bars with the values of the student grades. So here’s what I’m going to do. I’m actually going to loop over all the different grades, so we’re going to do a for loop, and I’m actually going to enumerate these as well. Okay, so I have a loop where I have I. Which is the index value and G, which is the actual grade of the student and for every single student grade. I’m going to add a bit of text. So now we have our piplottxt within the loop. So this is something that’s pretty powerful that you could loop over something iterate over something. Now when it comes to my X and Y positions, I’m actually going to use I and G which are dependent on the grades list. Okay, so my X position is actually I the index value and my Y position. I’m going to put the text just a little lower than the actual grade value. I want to put that text inside the bar. So let’s do G minus eight now. Finally, when it comes to the text itself, I can use what’s called an F string. If you’re not used to these, definitely go look them up. They’re pretty simple. The idea is that what we can do with an F string is actually print out a variable so print a variable as text. As long as I’m using these curly braces. I will go ahead and grab the grade itself and print it directly on the bar. Okay, let’s see what happens, all right, so for every single student grade, we’ve grabbed the grade. We’ve put it on the appropriate bar. So this is a really nice way to be able to label your bar plots directly and we’re doing this in a loop so that we don’t have to type this out over and over and over again, a couple styling tips for you here. You might want to center this text, so it’s in the middle of the bar. This is where our horizontal alignment will help and we’ll definitely want to increase the font size because these are a little too small here. We go great and the other big benefit of directly. Labeling your bars like this. Maybe you don’t even need your y-axi’s anymore, right, because someone can directly read off the positions from the bar helps with the communication, so you might even decide to completely remove your y ticks, and you can do that by passing in a empty list for those ticks, and you don’t need the spine anymore either. So in my seaborne D spine function, I can set left to be true and there we go. This makes for a really nice, clean visual and helps people understand immediately. What each student scored because it’s written right on the bar? One final thing to note is that there is this other Pi Plot function called annotate with annotate. You’re able to draw an arrow pointing at something on your figure. So if you’re interested in that, be sure to check out, annotate as well. If you like this video, be sure to give it a thumbs up and I will see you in the next one. [music] bye!