title
Matplotlib Tutorial (Part 8): Plotting Time Series Data

description
In this video, we will be learning how to plot time series data in Matplotlib. This video is sponsored by Brilliant. Go to https://brilliant.org/cms to sign up for free. Be one of the first 200 people to sign up with this link and get 20% off your premium subscription. In this Python Programming video, we will be learning how to plot time series data in Matplotlib. Time series data consists of data that contains dates. For example, in this video, we will be plotting BitCoin prices over the last few weeks. We will learn how to format dates in different ways so that they work better with our graphs. Let's get started... The code from this video (with added logging) can be found at: http://bit.ly/Matplotlib-08 Datetime Tutorial - https://youtu.be/eirjjyP2qcQ Datetime Format Codes - http://bit.ly/python-dt-fmt ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python #Matplotlib

detail
{'title': 'Matplotlib Tutorial (Part 8): Plotting Time Series Data', 'heatmap': [{'end': 300.098, 'start': 270.747, 'weight': 0.71}], 'summary': 'Tutorial covers plotting time series data with matplotlib using python code and csv file examples, including bitcoin prices and dates, and highlights brilliant.org as the sponsor.', 'chapters': [{'end': 51.074, 'segs': [{'end': 51.074, 'src': 'embed', 'start': 15.209, 'weight': 0, 'content': [{'end': 21.651, 'text': "So first we're going to look at some basic examples using some dates that I have directly here within my Python code.", 'start': 15.209, 'duration': 6.442}, {'end': 25.172, 'text': "And then we'll see an example using data from a CSV file.", 'start': 22.031, 'duration': 3.141}, {'end': 30.514, 'text': 'And the data within the CSV file are Bitcoin prices over a couple of weeks.', 'start': 25.532, 'duration': 4.982}, {'end': 35.439, 'text': 'Now I would like to mention that we do have a sponsor for this series of videos, and that is Brilliant.org.', 'start': 30.874, 'duration': 4.565}, {'end': 43.186, 'text': '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.', 'start': 35.739, 'duration': 7.447}, {'end': 45.689, 'text': "And I'll talk more about their services in just a bit.", 'start': 43.526, 'duration': 2.163}, {'end': 47.871, 'text': "So with that said, let's go ahead and get started.", 'start': 46.029, 'duration': 1.842}, {'end': 51.074, 'text': "Okay, so I've got some sample code pulled up here in my script.", 'start': 48.391, 'duration': 2.683}], 'summary': 'Demonstrating date manipulation in python, with bitcoin price data from a csv file and sponsorship by brilliant.org.', 'duration': 35.865, 'max_score': 15.209, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf815209.jpg'}], 'start': 0.209, 'title': 'Plotting time series data in matplotlib', 'summary': 'Covers plotting time series data using matplotlib, demonstrating examples with direct python code and data from a csv file, specifically bitcoin prices over a couple of weeks, while acknowledging the sponsor, brilliant.org, and encouraging viewers to check them out.', 'chapters': [{'end': 51.074, 'start': 0.209, 'title': 'Plotting time series data in matplotlib', 'summary': 'Covers plotting time series data using matplotlib, demonstrating examples with direct python code and data from a csv file, specifically bitcoin prices over a couple of weeks, while acknowledging the sponsor, brilliant.org, and encouraging viewers to check them out.', 'duration': 50.865, 'highlights': ['The video covers plotting time series data using Matplotlib and demonstrates examples with direct Python code and data from a CSV file, specifically Bitcoin prices over a couple of weeks.', 'Acknowledges the sponsor, Brilliant.org, and encourages viewers to check them out using the link in the description section below.']}], 'duration': 50.865, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8209.jpg', 'highlights': ['Covers plotting time series data using Matplotlib and demonstrates examples with direct Python code and data from a CSV file, specifically Bitcoin prices over a couple of weeks.', 'Acknowledges the sponsor, Brilliant.org, and encourages viewers to check them out using the link in the description section below.']}, {'end': 429.916, 'segs': [{'end': 166.195, 'src': 'embed', 'start': 51.414, 'weight': 0, 'content': [{'end': 56.379, 'text': "So first, we'll look at these time series plots using this list of data directly in my script.", 'start': 51.414, 'duration': 4.965}, {'end': 61.423, 'text': "And then we'll look at a real world example with data that I'll load in from a CSV file.", 'start': 56.779, 'duration': 4.644}, {'end': 67.769, 'text': "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.", 'start': 61.824, 'duration': 5.945}, {'end': 70.412, 'text': 'But if not, then let me go over all this real quick.', 'start': 68.13, 'duration': 2.282}, {'end': 72.753, 'text': 'So here at the top we have some imports.', 'start': 70.852, 'duration': 1.901}, {'end': 74.454, 'text': "So we're importing pandas.", 'start': 73.013, 'duration': 1.441}, {'end': 78.996, 'text': "We're also importing date time from the standard library and time deltas.", 'start': 74.734, 'duration': 4.262}, {'end': 82.597, 'text': 'We are importing piplot from matplotlib.', 'start': 79.816, 'duration': 2.781}, {'end': 85.959, 'text': "And we're also importing dates from matplotlib.", 'start': 82.937, 'duration': 3.022}, {'end': 93.762, 'text': "And we're importing that as mpldates because I was afraid I was going to overwrite it with a variable like I did here called this dates.", 'start': 86.019, 'duration': 7.743}, {'end': 97.865, 'text': 'Anyways, we are also using a style here.', 'start': 94.622, 'duration': 3.243}, {'end': 100.446, 'text': "We're using seaborne style with matplotlib.", 'start': 97.925, 'duration': 2.521}, {'end': 108.572, 'text': "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.", 'start': 101.307, 'duration': 7.265}, {'end': 119.239, 'text': 'We have a plt.tightlayout here that adds a padding to our plot, and plt.show will just show us our plot.', 'start': 109.753, 'duration': 9.486}, {'end': 123.802, 'text': "And we'll go over this other data once we are actually ready to plot that.", 'start': 119.619, 'duration': 4.183}, {'end': 127.703, 'text': 'And as usual, all of this is going to be available for download on my GitHub.', 'start': 124.182, 'duration': 3.521}, {'end': 130.364, 'text': "And there's a link to that in the description section below.", 'start': 128.002, 'duration': 2.362}, {'end': 135.625, 'text': 'If anyone would like to copy and paste this into their editor and follow along with this series.', 'start': 130.503, 'duration': 5.122}, {'end': 143.487, 'text': "Okay So for my sample data here, I've got a list of seven dates and I'm using Python's built in date time module to create these.", 'start': 135.965, 'duration': 7.522}, {'end': 146.628, 'text': 'So these are just seven days back to back.', 'start': 143.827, 'duration': 2.801}, {'end': 149.669, 'text': "And then below I've got a Y variable here.", 'start': 147.108, 'duration': 2.561}, {'end': 155.631, 'text': 'for our y-axis, and this is just a list of seven random values.', 'start': 150.089, 'duration': 5.542}, {'end': 166.195, 'text': 'So, to plot these dates, we can simply say down here below our y-axis, plt.plot, and we want to do oops,', 'start': 155.971, 'duration': 10.224}], 'summary': 'Using python, pandas, and matplotlib to plot time series data with sample dates and random values.', 'duration': 114.781, 'max_score': 51.414, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf851414.jpg'}, {'end': 254.142, 'src': 'embed', 'start': 226.303, 'weight': 3, 'content': [{'end': 230.907, 'text': 'And if you wanted to, then you could also go ahead and turn off these markers by setting marker to none.', 'start': 226.303, 'duration': 4.604}, {'end': 233.189, 'text': "But I'm going to go ahead and leave those here for now.", 'start': 231.267, 'duration': 1.922}, {'end': 240.894, 'text': "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.", 'start': 233.789, 'duration': 7.105}, {'end': 247.578, 'text': 'So one way that we can do this is to run the auto format X date method on our figure.', 'start': 241.274, 'duration': 6.304}, {'end': 254.142, 'text': 'And this will rotate our dates so that they fit a bit nicer and change their alignments and things like that.', 'start': 248.038, 'duration': 6.104}], 'summary': 'Using auto format x date method to improve date plot appearance.', 'duration': 27.839, 'max_score': 226.303, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8226303.jpg'}, {'end': 300.098, 'src': 'heatmap', 'start': 270.747, 'weight': 0.71, 'content': [{'end': 280.271, 'text': 'so to get the current figure from pi plot, we can say plt dot gcf, which is get current figure.', 'start': 270.747, 'duration': 9.524}, {'end': 291.615, 'text': 'and now to run this auto format method, we can just run it on that current figure and say auto FMT for format, then underscore X date.', 'start': 280.271, 'duration': 11.344}, {'end': 300.098, 'text': 'Okay, so now if I run this, then we can see that now these dates are rotated and they have different alignments.', 'start': 291.995, 'duration': 8.103}], 'summary': 'The auto format method rotated dates and improved alignment.', 'duration': 29.351, 'max_score': 270.747, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8270747.jpg'}, {'end': 350.587, 'src': 'embed', 'start': 318.227, 'weight': 5, 'content': [{'end': 324.37, 'text': 'what if instead we wanted them to start with the name of the month and then the day and then the year?', 'start': 318.227, 'duration': 6.143}, {'end': 328.012, 'text': 'So to do this, we have to use some date time formatting.', 'start': 324.771, 'duration': 3.241}, {'end': 337.598, 'text': "So to do this, I've already imported this line up here at the top from matplotlib import dates as MPL dates.", 'start': 328.333, 'duration': 9.265}, {'end': 350.587, 'text': "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 strftime method from the datetime class.", 'start': 338.198, 'duration': 12.389}], 'summary': 'Using date time formatting to start with month, day, year.', 'duration': 32.36, 'max_score': 318.227, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8318227.jpg'}], 'start': 51.414, 'title': 'Time series and date plotting', 'summary': "Covers time series plotting with matplotlib using script data and a real-world example from a csv file, along with plotting dates using python's built-in date time module to display seven dates with corresponding random values.", 'chapters': [{'end': 135.625, 'start': 51.414, 'title': 'Time series plotting with matplotlib', 'summary': 'Demonstrates time series plotting using data from a script and a real-world example with data from a csv file, covering imports, style usage, and plot display, with the code available for download on github.', 'duration': 84.211, 'highlights': ['The chapter demonstrates time series plotting using data from a script and a real-world example with data from a CSV file.', 'The chapter covers the imports of pandas, date time, time deltas, piplot from matplotlib, and dates from matplotlib, with mpldates being used to avoid variable overwriting.', 'The code includes the usage of seaborn style with matplotlib, plt.tightlayout for adding padding to the plot, and plt.show for displaying the plot.', "The code and data used in the chapter are available for download on the author's GitHub, allowing readers to follow along with the series."]}, {'end': 429.916, 'start': 135.965, 'title': "Plotting dates with python's built-in date time module", 'summary': "Discusses using python's built-in date time module to plot seven dates with corresponding random values, formatting the plot, and changing the display format of the dates, along with the use of date formatter class from matplotlib.", 'duration': 293.951, 'highlights': ["The chapter discusses using Python's built-in date time module to plot seven dates with corresponding random values.", 'The chapter explains formatting the plot and changing the display format of the dates.', 'The chapter introduces the use of date formatter class from matplotlib for formatting dates.']}], 'duration': 378.502, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf851414.jpg', 'highlights': ['The chapter demonstrates time series plotting using data from a script and a real-world example with data from a CSV file.', 'The chapter covers the imports of pandas, date time, time deltas, piplot from matplotlib, and dates from matplotlib, with mpldates being used to avoid variable overwriting.', "The chapter discusses using Python's built-in date time module to plot seven dates with corresponding random values.", 'The chapter explains formatting the plot and changing the display format of the dates.', "The code and data used in the chapter are available for download on the author's GitHub, allowing readers to follow along with the series.", 'The chapter introduces the use of date formatter class from matplotlib for formatting dates.', 'The code includes the usage of seaborn style with matplotlib, plt.tightlayout for adding padding to the plot, and plt.show for displaying the plot.']}, {'end': 1016.609, 'segs': [{'end': 480.977, 'src': 'embed', 'start': 452.589, 'weight': 1, 'content': [{'end': 463.213, 'text': 'And then we can format the X axis by saying dot X axis dot set underscore major underscore formatter.', 'start': 452.589, 'duration': 10.624}, {'end': 468.247, 'text': 'And we will set that equal to our date format here.', 'start': 464.204, 'duration': 4.043}, {'end': 477.794, 'text': "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.", 'start': 469.208, 'duration': 8.586}, {'end': 480.977, 'text': 'Now this is formatted to say May 24th, May 25th, May 26th, and so on.', 'start': 478.154, 'duration': 2.823}], 'summary': "Formatted x axis to display dates as 'may 24th', 'may 25th', etc.", 'duration': 28.388, 'max_score': 452.589, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8452589.jpg'}, {'end': 555.954, 'src': 'embed', 'start': 529.284, 'weight': 3, 'content': [{'end': 536.027, 'text': "Okay, so now I'm going to uncomment out the other code that I have here and we will explain what this is doing here in just a sec.", 'start': 529.284, 'duration': 6.743}, {'end': 542.809, 'text': 'But first let me paste in where we were plotting that data and also setting the auto format date there.', 'start': 536.367, 'duration': 6.442}, {'end': 548.051, 'text': 'Okay, so up here We are loading in a CSV file here using pandas.', 'start': 543.189, 'duration': 4.862}, {'end': 552.353, 'text': "And if you've been following along with this series, then this probably looks familiar to you,", 'start': 548.531, 'duration': 3.822}, {'end': 555.954, 'text': "since we've loaded in CSV data a few times in the series so far.", 'start': 552.353, 'duration': 3.601}], 'summary': 'Loading csv file using pandas for data plotting and formatting.', 'duration': 26.67, 'max_score': 529.284, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8529284.jpg'}, {'end': 649.705, 'src': 'embed', 'start': 619.715, 'weight': 0, 'content': [{'end': 622.458, 'text': "So it's basically setting it equal to that date column.", 'start': 619.715, 'duration': 2.743}, {'end': 630.666, 'text': "Now when we do price close and set it to data close, I'm grabbing all of the closing prices for those days.", 'start': 622.958, 'duration': 7.708}, {'end': 635.59, 'text': "So we've got the price date and the price close data loaded in from that CSV file.", 'start': 631.126, 'duration': 4.464}, {'end': 640.255, 'text': "So to plot this, it's as easy as passing those into the plot date method.", 'start': 635.991, 'duration': 4.264}, {'end': 649.705, 'text': "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.", 'start': 640.595, 'duration': 9.11}], 'summary': 'Plot closing prices from csv file using date and price data.', 'duration': 29.99, 'max_score': 619.715, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8619715.jpg'}, {'end': 902.363, 'src': 'embed', 'start': 862.539, 'weight': 2, 'content': [{'end': 868.242, 'text': "So that's how you're going to work with date times in pandas using that plot date method.", 'start': 862.539, 'duration': 5.703}, {'end': 873.725, 'text': "Like I said, it's a lot like any other line plot, but you're working with dates here.", 'start': 868.863, 'duration': 4.862}, {'end': 878.387, 'text': "So there's a few different things with how the formatting works and things like that.", 'start': 873.785, 'duration': 4.602}, {'end': 882.59, 'text': 'But basically, this is what you do for time series data in Matplotlib.', 'start': 878.528, 'duration': 4.062}, {'end': 890.294, 'text': "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.", 'start': 882.93, 'duration': 7.364}, {'end': 896.778, 'text': 'Brilliant is a problem solving website that helps you understand underlying concepts by actively working through guided lessons.', 'start': 890.935, 'duration': 5.843}, {'end': 902.363, 'text': 'They have computer science courses ranging from algorithms and data structures to machine learning and neural networks.', 'start': 897.079, 'duration': 5.284}], 'summary': 'Pandas plot date method for time series data in matplotlib, sponsored by brilliant.org with computer science courses.', 'duration': 39.824, 'max_score': 862.539, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8862539.jpg'}], 'start': 430.397, 'title': 'Working with dates in plotting', 'summary': 'Demonstrates formatting x-axis with date in a python plot, focusing on bitcoin data for two weeks, and working with dates in pandas, matplotlib, and brilliant.org sponsorship.', 'chapters': [{'end': 598.938, 'start': 430.397, 'title': 'Working with date formatting in plotting', 'summary': 'Demonstrates how to format the x-axis with date format in a plotting example using python, including loading and plotting csv data and setting date formats for the chart, with a focus on bitcoin data for about two weeks.', 'duration': 168.541, 'highlights': ["Setting the X axis format to display dates in a desired way, such as 'May 24th', 'May 25th', 'May 26th', and so on, by using the 'set_major_formatter' method.", 'Loading and plotting CSV data, specifically bitcoin data for about two weeks, and explaining the data structure with headers including date, open price, high price, low price, close, adjusted close, and volume.', 'Explaining the process of loading CSV data using pandas and demonstrating the familiarity of the process for those following the series, while also mentioning the accidental inclusion of a line at the bottom of the data.']}, {'end': 1016.609, 'start': 599.078, 'title': 'Working with dates in pandas and matplotlib', 'summary': 'Explains how to work with dates in pandas and matplotlib, demonstrating the process of loading and plotting time series data, utilizing pandas methods to convert and sort dates, and introduces the sponsor brilliant.org, offering a problem-solving website for computer science courses.', 'duration': 417.531, 'highlights': ['The process of loading and plotting time series data with pandas and matplotlib is demonstrated, showcasing the steps of loading data as a pandas data frame, plotting the data using the plot date method, and addressing the issue of plotting dates as strings instead of actual dates.', 'The utilization of pandas methods to convert and sort dates is explained, illustrating the process of converting the date column to a datetime using the to_datetime method, and sorting the dates in ascending order using the sort_values method.', 'The introduction of Brilliant.org, a problem-solving website for computer science courses, is presented, highlighting the availability of coding environments, guided lessons, and computer science material covering various fields of mathematics and astronomy.']}], 'duration': 586.212, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_LWjaAiKaf8/pics/_LWjaAiKaf8430397.jpg', 'highlights': ['Loading and plotting CSV data, specifically bitcoin data for about two weeks', "Setting the X axis format to display dates in a desired way using 'set_major_formatter' method", 'The process of loading and plotting time series data with pandas and matplotlib is demonstrated', 'Explaining the process of loading CSV data using pandas and demonstrating the familiarity of the process', 'The introduction of Brilliant.org, a problem-solving website for computer science courses']}], 'highlights': ['Covers plotting time series data using Matplotlib and demonstrates examples with direct Python code and data from a CSV file, specifically Bitcoin prices over a couple of weeks.', 'The chapter demonstrates time series plotting using data from a script and a real-world example with data from a CSV file.', 'Loading and plotting CSV data, specifically bitcoin data for about two weeks', 'The chapter covers the imports of pandas, date time, time deltas, piplot from matplotlib, and dates from matplotlib, with mpldates being used to avoid variable overwriting.', 'The chapter explains formatting the plot and changing the display format of the dates.', "Setting the X axis format to display dates in a desired way using 'set_major_formatter' method", 'The process of loading and plotting time series data with pandas and matplotlib is demonstrated', 'The chapter introduces the use of date formatter class from matplotlib for formatting dates.', 'Acknowledges the sponsor, Brilliant.org, and encourages viewers to check them out using the link in the description section below.', 'The introduction of Brilliant.org, a problem-solving website for computer science courses', "The code and data used in the chapter are available for download on the author's GitHub, allowing readers to follow along with the series.", 'Explaining the process of loading CSV data using pandas and demonstrating the familiarity of the process']}