[MUSIC] Okay on to lineplots, now we've actually seen line plots a couple of times but they looked like scatter plots. In fact, the very first plot I showed you was a lineplot, because we create a lineplot with the plot function and it renders a number of different series of data points and connects each one in a series with a line. In the very first one I showed you only at one point, so you didn't see any lines, let's jump right into this. There's a couple of things which are new about this versus the scatter plots we just saw. First we only gave y axis values to our plot column, no X axis values. Instead, the plot function was smart enough to figure out what we wanted and to use the index of the series as the X value. This is pretty handy when you want to make quick plots. Second, we see that the plot identifies this as a two series of data and that the colors of data from the series are different including the data points and the lines between the data points. This is different from the scatter plot, which required us to label those lines directly. Okay, so I got an import map plot, lib pie plot. Now, we can use numpy to create a linear series of data points one through eight. Will also create a number of quadratically increasing values as the second series just for a demonstration. Remember you can use broadcasting in numpy to do this. So you'll often see this and want to do this for quick demos. This is just our nice little array here of eight points, and then here we're just going to square it to create that exponential data. This time I'm going to use the mini formatting language to describe how I want those markers and lines to be rendered. So previously you saw me use this O and it just made this circle marker, but we can pretend that with a dash to use a solid line between the circle markers. Note that I'm passing the data followed by the formatting for each series, so we're going to see the result as a two data series. The linear one at the bottom of the quadratic one at the top. So this looks a little odd and a little different, and it's worth studying this for a moment. Here I'm calling plot, I'm passing in our data, I'm telling our plot how it should render our data. Then I'm passing in another piece of data and I'm telling it how to render that and we can just keep doing this for as many series of data that we have. All right, so we can see those two pieces of data, the quadratic and the linear being plotted there. Now, it's pretty easy to extend this and add an arbitrary new line. So for instance, I'm going to use this dash dash r which tells plot to add a dash line in red. So here I'm going to have the exact same plot call linear data exponential data, dash o, dash o. But then I'm going to actually make a subsequent call to plot, and I'm going to pass it in these three points and I'm going to say dash dash r. Do I have to put this on a subsequent line? No, I could have just added these extra parameters here. I just wanted to show you that you can do that. Right, and so now you can see that we've got our two first series of data points but now we have this third series that has essentially no markers and it's just got dash dash lines throughout. Did you notice what I did there though? I didn't call plot dot figure by default pi plot scripting interface will call plot.gcf when you try and do something. If there's no figure, then it's going to create one for you, the way jupytelab is configured by default, it will automatically close a figure after each cell is executed. So this means that pi plot is going to create a new figure for us each and every time. This is a great time saver, goodbye plot dot figure. We can use the regular axis functions creating labels for the axis and the figure as a whole as well, and we can create a legend too. But since we didn't label the data points as we did with the scatter plot, we need to create legend entries when we add the legend itself. So I like to label my data points when I actually add the series directly, but you don't have to do that. So I want to show you because you'll see lots of different examples with this. And of course the ordering for most of these, it doesn't really matter because it's setting these axis object before it's actually rendering. So here I'm going to create an X label, a Y label and a title before I actually have any data. In fact, the very first one here is going to create a new figure and a new axis object and set the label before we've done anything. Then I'm going to add a legend, and I'm going to say well the legends got three things in, a Baseline, Competition and Us. And then I'm going to do the exact same plot calls that you just saw. And you can see it renders here nicely, and we've got some data, we've got some other data and we've got the title. This is a good time to introduce the fill between function of matplotlib. Now, this function isn't specific to lineplots but you can see it commonly used with lineplots to highlight an area. So I want to introduce it here in this context. Let's imagine that we want to highlight the difference between the green and the blue curve somewhere. We can tell the axis to paint a color between these series using the fill between function. So I'm going to have that same code X label, Y label, title, legend plot. And then now we actually need to grab that current axes object and then call fill-between, now we didn't specify any X values that are called a plot. Remember by default it's just going to use the indexes of your list or your array that's passed. So I'm actually going to just going to use the same range of data points that it's already using which is symmetrical. And then we're going to put this as our lower bounds and our upper bounds with the color that we want painted. And for fun, I'm going to add a transparency. So here I'm saying pi plot get me the current axis, then call on it fill-between I want you to create a range from the linear data. So a bunch of numbers that's our X values. Here's our then linear and exponential data and I want you to set the color of this fill-between the blue, and I want you to set the alpha 0.25. And the linear and exponential data are going to show the upper, sorry, the lower and upper bounds of this respectively, okay? So that renders nicely, we've got this blue line, we've got the orange line, we've got the points and we've got this nice fill-between shaded area, and we have all of our title and so forth. Our charts are really becoming a little bit more sophisticated in how they work. So let's try working with dates, most likely you're going to have a series of X values and Y values which you're looking at plot. Often with lineplots this comes in the form of a date time on the X axis. Now in earlier versions of matplotlib it was painful and you could go back to maybe a previous lecture video that I taught on this, but actually I've been able to shorten this quite a bit because matplotlib is growing quite a bit. So let's change our X axis here to a series of eight daytime instances and one day intervals. So I'm going to use the numpy arrange function here which we saw previously and you'll commonly see this in stack overflow questions or textbooks or just example videos like this. And I'm going to ask for an arrangement of dates at daily intervals and so I give it the start date and the end date as a time. And then I set the dtype to be date time 64, and then arrange is going to create that arrangement there. Now I'm just going to re plot our linear and exponential dates. So we have our observations are linear date, our observations and our exponential date. Again, this can be very unfamiliar for people who are not used to positional parameters. Previously we had data, formatting and then data formatting, now because we want to have these dates along the bottom, we have those dates, data formatting, dates, data formatting. So the position of the argument to plot is changing with the number of arguments that we specify in the format of those arguments is very unorthodoxed in a lot of programming languages and including in a lot of python. But it's supported in python and it speaks really to matplotlibs roots in matlab. Okay, this produces a pretty decent looking figure with the date that we want. The dates overlap pretty heavily. Now, there's a couple of things that we could do here for instance, everything's from 2017. So we could take the easy way out and just iterate through and change the labels by stripping off the year and then add an X axis label which clarifies this is all for 2017. This is pretty reasonable but I want to show you a couple of features of matplotlib and how we can deal with text instead. There's lots of interesting properties of the axes object and you should use some of them in the assignment and I encourage you to go to the docks and explore them. Now that you're starting to pick up some of the skills and understand how to build those basic plots. For instance, you can get the grid lines, the tick locations for both major and minor ticks and so forth. Just like all artists and axes has a bunch of children which are themselves artists. In fact, if you're following along in the jupyter lab notebook in coursera with this lecture, why don't you just pause the video and run that wreck GC function we wrote earlier to explore what kind of artists are on this X axes object, and what it actually contains. What I want to show you though is that you can access the text of the ticks using the get tick labels function. Each of the tick labels are a text object which itself as an artist. And this means you can use a number of different artist functions and one specific to text is to be able to set the rotation function, which allows the rotation to be set in degrees. So let's iterate through the axis labels and change each one to rotate it. Okay, so here's just our plot data as normal, now I want to grab that X axis, so I just grabbed the axis object that's plural of access. And then grab the X axis and then for each item inside of there I want to get the tick labels, and I want to set that rotation to 45 degrees. All right, so that looks pretty easy, it was quick pretty fluid to do and you can imagine how we could customize this further if you want it. So let's add back in our headings now, so the exact same thing but now I've added to the bottom are labels x,y and title. I tend to add my labels at the bottom but that's just habit as you saw earlier, you can add those whenever you would like to. All right, there we go, wonderfully readable dates in matplotlib. And while we're talking of text and readability, let me share two other insights. The first dealing with equations, matplotlib has a fairly strong connection to la tech, a typesetting language used by mathematicians and scientists. This means that you can use a subset of la tech directly on your labels and matplotlibs going to render them as equations. Here for instance, we could set the title of the axis so that there's an X squared directly in it. To do this, we escaped to la tech math mode with dollar signs. Note that this works regardless of whether you've got la tech installed or not for a long time you wouldn't have la tech installed on Mac Os or windows. But with a full la tech installation, you actually have significant control over the text formatting. For the second insight I want to talk about the size of the figure itself. Why are we looking at the small postage stamp figures? Well, that depends on your screen size and resolution. But the point is we can tell matplotlib that we want the figure to be a certain size by passing in parameters when we first make that figure. So in this next example I'm going to specify it in inches, but there's various other dimensions that you can use, you can set them as centimeters or DPI and so forth. So I'm going to create a new figure this time explicitly. But I'm going to pass in this fig size keyword argument as a tuple and I'm going to make it eight comma six which means eight inches by six inches. Then I'm just going to put in my usual data here from above, so this is basically copy and place from above. But I wanted to show the title here, when you want to escape into la tech math mode, you need just put the dollar sign and then put inside of any text formatting string and then put what you want. So and this in la tech means X to the power of two. You don't have to do this, right? But and la tech itself is a huge learning curve in and of itself. But for basic formula it's quite nice and you can of course do much more extensive things if you're in a scientific industry. That's been a pretty successful look at matplotlib basics I think, from the architecture overview, to creating clean charts, we could include in a report or just on our own through data explorations. I want to show you one more thing this week in the next lecture, the bar chart.