WHT

Time Series Matplotlib | Matplotlib Tutorial (part 8): Plotting Time Series Data

Corey Schafer

Subscribe Here

Likes

1,803

Views

82,291

Matplotlib Tutorial (part 8): Plotting Time Series Data

Transcript:

Hey, there, how’s it going? Everybody in this video we’re going to be looking over time series data and plotting dates in Matplotlib. Now there’s a ton of data out there that contains date information so knowing how to plot this properly is definitely going to be a huge help when creating these graphs so first we’re going to look at some basic examples using some dates that I have directly here within my Python code and then we’ll see an example using data from a CSV file and the data within the CSV file are Bitcoin prices over a couple of weeks now? I would like to mention that we do have a sponsor for the series of videos and that is brilliant ORG, so I really want to thank brilliant for sponsoring this series and it would be great if you all could go check them out using the link in the description section below and support the sponsors and I’ll talk more about their services in just a bit so with that said, let’s go ahead and get started. OK, so I’ve got some sample code pulled up here in my script. So first, we’ll look at these time series plots using this list of data directly in my script, and then we’ll look at a real world example with data that I’ll load in from a CSV file now. If you’ve been following along with the series, then you’ll likely recognize a lot of the other matplotlib code that I have here at the moment, but if not, then let me go over all this real quick so here at the top, we have some imports so we’re importing. Pandas we’re also importing date/time from the standard library and time Deltas. We are importing pipe lot from Matplotlib and we’re also importing dates from matplotlib and we’re importing that as MPL dates. Because I was afraid I was going to override it with a variable like I did here called this dates anyways. We are also using a style here we’re using Seabourn style with matplotlib. These are the data that we’re going to be using. But I’m going to gloss over this for now and just point out the rest of this code here. We have a PLT tight layout here that adds, a padding to our plot and Plt Dot show will just show us our plot and we’ll go over this other data once we are actually ready to plot that, and as usual, all this is going to be available for download on my Github, and there’s a link to that in the description section below If anyone would like to copy and paste this into their editor and follow along with this series. Okay, so for my sample data here. I’ve got a list of seven dates and I’m using Pythons Built-in date/time module to create these. So these are just seven days back-to-back and then below. I’ve got a y variable here for our y-axis and this is just a list of saying seven random values so to plot these dates, we can simply say down here below our y-axis. Plt dot plot and we want to do. Oops, that is plot date, and we want dates to be the x-axis and we want Y to be the y-axi’s. So if I run this, then we can see here that it plots those out. Now if you get some warnings in your output down here, then don’t worry about that. Mine is just warning me about some future change in pandas that will be taking place, but we can see that we get those dates and values plotted out now. I’m not sure why, but by default, this plot has markers instead of being connected by a line, but we can fix that easily just by saying that we want the line style line style of this plot to be solid. So now if I run that, I want to make this small again. Now that we’ve seen that warning so now we can see that these are now connected by a line, and if you wanted to, then you could also go ahead and turn off these markers by setting marker to none, But I’m going to go ahead and leave those here for now. Okay, so now that we have some dates to work with, let’s look at some different ways that we can format our plot to make this look a bit better so one way that we can do. This is to run the auto format. X date method on our figure and this will rotate our dates so that they fit a bit nicer and change their alignments and things like that. Now we haven’t talked much about figures and axes in this series yet that is going to be in the sub plots video in a couple more videos, but basically, this is going to be a method on our figure and on this pipe lot object that we have been using so to get the current figure from pipe lot we can say. P. LT Dot G. CF, which is get current figure and now to run this autoformat method. We can just run it on that current figure and say Auto F MT4 format then underscore X date. Okay, so now if I run this, then we can see that now. These dates are rotated and they have different alignments that just makes it so that these are, you know, not so bunched together and it makes it easier to read. Okay, so now that we’ve got that. Auto formatting in place. Let’s also see how we can change the format of our dates. So what if instead of how they’re displayed now with the year month day? What if instead we wanted them to start with the name of the month and then the day and then the year so to do this, we have to use some date/time formatting so to do this. I’ve already imported this line up here at the top from Matplotlib import dates as MPL date and from that imported module we’re going to use the date Formatter class and we’re going to be passing in any format string that you could also pass into the STRF time method from the date/time class. Now, if you don’t know how to format dates then. I do have a separate video on Python date/time on the date/time module that goes into more detail about that, so I’ll leave a link to that video in the description section below if anyone is interested in that and also leave a link to the Python documentation where you can find the formatting codes for the format that you’re looking for, so for this example. Let me write out the format that we want so down here below where we ran that get current figure now. I’m going to say date Underscore format is equal to MP L dates. That’s what we imported at the top and we’re going to use the date Formatter class from that imported module and now we’re going to pass in our format string and again. I’m going to leave a link in the description section below too Python documentation where you can find these formatting codes. But I have mine written down here. I always need to look these up. But for the abbreviated name of the month that is % lowercase B and then for the days, that’s % D and then the year is %. Capital Y to do all four digits of the year and again I will leave a link to that documentation so that you can look up other formatting codes. If you want to change it up so now, we need to set this as the format for our X axis, so just like. I grabbed the figure to run the auto format method. I’m going to need to grab the axis to run this format method so to grab the current axis, it’s a lot similar to getting the current figure. We’ll say Plt Dot G C A which is get current axis and then we can format the x-axi’s by saying Dot X-axi’s dot set underscore major underscore formatter and we will set that equal to our date format here. So now if I formatted that correctly, If I run this, then we can see that now. We don’t have that year month date that we had before now. This is formatted to say. May 24th May 25th May 26 And so on. So you can format your dates. However, you like to show up in your chart, OK? So now that we’ve seen how to work with date times using this simple example here now let’s look at some data that I have in here in a CSV file and see if we can load that in and plot it. So let me remove what we have now and I’ll uncomment this code that I have here at the bottom, so I am going to copy these two lines here where we are doing our plot date and also our auto format so. I’m going to cut those out so that I can paste them in later and now I am going to remove from dates all the way down to where we set that formatter. I just wanted to show how to form about those dates, but I’m not gonna format this next example. Okay, so now. I’m going to uncomment out the other code that I here and we will explain what this is doing here in, just a sec, but first let me paste in where we were plotting that data and also setting the autoformat. Date there. Okay, so up here, we are loading in a CSV file here using pandas, and if you’ve been falling along with this series, then this probably looks familiar, too, since we’ve loaded in CSV data a few times in the series so far, but just in case. Let me go ahead and show you this CSV data and also go over how we’re loading this in so we are loading in the data from data Dot Csv and I have this pulled open here in the other tab. So this is the data that we’re loading loading in. So these are the headers here. The first value is the date. The next value is the open price. The next value is the high price for that day. Low price, close, adjusted, close and volume and like. I was saying this is just Bitcoin data for about. I think two weeks or so, so this I just pulled off line and actually this line here at the bottom. That’s not supposed to be there. I’m gonna add that in later. I just had that there for a while. I was testing. Okay, so let me go back to the code and explain how we’re loading this in. So when we read this in it’s loading this in as a panda’s data frame and whenever we say price date is equal to data and then pass in that key of date, what it’s doing is its setting price date equal to all of these dates here, so it’s basically setting it equal to that date column now when we do price close and set it to data close. I’m grabbing all of the closing prices for those days, so we’ve got the price date and the price closed data loaded in from that CSV file so to plot this, it’s as easy as passing those into the plot date method. So I’m going to pass in price date as the X, which is the first value here, and the price close will be the Y value, so I will paste that in there, and if we run that, then we can see that we get that data plotted out now, right now this might look okay, but it’s not actually plotting out our X axis as dates. It’s actually plotting these out strings so to show this let me add a line to the end of the data of our CSV file, and I’m going to add it out of order, so that’s. What that line was there before whenever I was doing some testing, So what I’m gonna do is I’m just going to copy my top line. Here and my top line is May 18th so at the very bottom I’m gonna paste in another line and I’m gonna make this May 17th and I’m just going to leave the prices and everything the same as the first day. So now, if I run this, then we can see that we don’t have a May 17th here at the beginning. It’s putting it here at the end, so that doesn’t really make any sense now like I said the reason it’s doing, this is because those are being read in as a string and not dates so to fix this we’re actually going to use some panda’s methods to set that to a date and then we’ll also sort that as well. Now this isn’t a panda’s tutorial, so I’m not gonna go into much detail here, but I just wanted to show this in case anyone is working with dates that are out of order. This is a pretty common thing to do is to need to sort by dates by data that you’re loading in so to do this underneath our data here, make a couple of blank lines, so I’m gonna take this data date column here, and I’m going to set that equal to, and I’m gonna say that I want to do pandas. And then a method called to underscore date/time and I want to convert that date column to a date/time. So what we’re doing here is? We are converting that date column to a date time using the two date/time method from pandas, and then we are just replacing all those values, which were strings with those converted date times. And now, if we want to sort that, then we can simply say data dot sort, and now that those are date times, we can just sort by date and also. I want this to just sort in place, so I’m gonna say in place equals true in place just basically means that it goes head and modifies that data instead of us needing to say, like data equals data. Dot, sort something like that, so we don’t have to do that since we’re changing that in place so now with those two changes there, If I save that and run it now, let me see if I’m getting an error. For some reason. Date is not defined up. You guys probably caught that as I was typing it, but I said date date. What I meant was data date. Did I make that mistake anywhere else? No, okay, so let me try that. And well, let’s see well and I made another mistake here. Sorry about that in a panda’s data frame. That is not sort that is sort values. Sorry to confuse you all there. Hopefully that is all the mistakes that I made. So now if I rerun that, then we can see. Let me take that output down there a little bit now. We can see that our date here. At the beginning, we gave it the same value as the next day so we can see that now. It’s showing up here at the beginning instead of being put at the end. So that’s how you’re gonna work with date times in pandas using that plot date method like I said, it’s a lot like any other line plot, but you’re working with dates here, so there’s a few different things with how the formatting works and things like that, but basically, this is what you do for time series data in Matplotlib. Okay, so we’re just about finished up here, but before we end. I’d like to mention the sponsor of this video and that is brilliant. Org brilliant is a problem-solving website that helps you understand underlying concepts by actively working through guided lessons, they have computer science courses ranging from algorithms and data structures to machine learning and neural networks. They even have a coding environment built into their website so that you can run code directly in the browser and that’s a great way to complement watching my tutorials because you can apply what you’ve learned in their active problem-solving environment and that helps to solidify that knowledge there are guided. Lessons will challenge you, but you also have the ability to get hints or even solutions. If you need them, it’s really tailored towards understanding that material, so they’re computer science material is fantastic, and I really like what they’re doing. They also have plenty of courses, depending on what you’re most interested in, so they have courses in different fields of mathematics or astronomy, solar energy, computational biology and all kinds of other great content so to support my channel and learn more about brilliant, you can go to brilliant Org /c m/s to sign up for free and also the first 200 people that go to that link will get 20% off the annual premium subscription. And you can find that link in the description section below and again that’s brilliant Org /c m/’s. Okay so. I think that’s gonna do it for this video. I hope you feel like you got a good introduction to working with dates and matplotlib and how we’d plot that type of data in the next video. We’re going to be learning how to plot live data in real time now. These real-time plots can be used in a lot of different applications for monitoring things that are constantly being changed or updated, so that can be data that you’re pulling down from an online. API or may be something that you’re reading from a sensor or something like that. There’s a lot of different types of applications for that, so definitely be sure to check that out, but if anyone has any questions about what we covered in this video, then feel free to ask in the comment section below. And I’ll do my best to answer those, and if you enjoy these tutorials and would like to support them, then there are several ways you can do that. The easiest way is to simply like the video and give it a thumbs up, and also it’s a huge help to share these videos with anyone who you think would find them useful. And if you have the means, you can contribute through Patreon and there’s a link to that page in a description section below. Be sure to subscribe for future videos. And thank you all for watching you.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more