Matplotlib Tutorial 11 - handling unix time

sentdex · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

This video tutorial covers handling Unix time stamps and converting them to date stamps for plotting in Matplotlib, utilizing the Yahoo Finance API and Python's datetime module.

Full Transcript

what is going on everybody Welcome to the 11th tutorial in our map plot lib tutorial video series here what we're going to be talking about in this video is handling for Unix time so there are all kinds of ways that you might get date stamps and all that we spend a lot of time on the conversion there um but because there's so many I can't just show like every version of a date stamp but I do think it's important to at least show also how to convert from a Unix Tim stamp to the matap plot lib kind of format so uh coming over to our Yahoo finance API like I was saying before if you use a term like in these large numbers you know you're going to get these like kind of date stamp but if like let's say we do 10d for 10 days uh you'll see that things have changed significantly and now no longer do we have this you know year month day situation we've got these numbers like this is like 1.4 billion what's that well these are Unix timestamps so again Unix time is just the number of seconds after January 1st 1970 so now we have to convert that to a date time that is acceptable for matplot lib so the way that we're going to do that is we're going to just use another little simple function it'll actually be a little easier than the previous one we wrote because there's some handling for it already Unix timestamps like I was saying before are really popular so it's really popular with computers so people have handled Unix timestamps for a long time so come coming over here uh to our gra or our uh file what we want to do now is uh at the top let's go ahead and import uh date time as DT generally you import date time as DT and then sometimes people will import date time. dat time as date time okay so again this is just a standard that people do if they import the main module it's. DT so now what we're going to do is let's um thinking I still have this up so let's pull up um a command prompt here and open python you can do this wherever you want or you can just watch you don't have to follow along if you don't want to but feel free to if you want so uh first let's import uh date time asdt we're also going to need to import uh time now let's say first of all if you're not familiar you can print you know time.time and that gives you a Unix time okay so this is the current Unix time so let's say um our example time equals time.time so we can print example now and there you go you've got your time now what we want to do is let's go let's print dt. dat time. from amp example okay so we print that out and what we get is a is a legible time stamp okay so 2015 the uh month is July day 8 or 8th so July 8th 2015 and then this is in you know military time so 2 22 23 and then you know milliseconds basically okay so then we want to convert that date and the way that we're going to do that is we're going to convert it to numpy time so date converion we would say is equal to np. Vector oh we don't have nump import so let's import numpy and we would say dates com equals mp. vectorize dt. dat time. from timestamp I thought we imported oh we just imported numpy we didn't say as NP so I'll just call I'll just do numpy datom so then we could use date com and we can convert a date stamp like this you know to to a date time so we're going to take this code um and we're going to come over here and we need to bring in Let's see we don't well we don't need time because that was just for an example uh so date time DT this should be all we need actually so we'll come down uh here where we've pulled the date here and instead of converting we're actually not going to convert anything so um well we could well we could add a converter if we wanted but yeah we're not going to do that so we'll just do this uh um and we'll just convert this as a list after the fact so we'll come down here and then what we would say is and in fact let me just uh let's take this line just cuz I want to save it for later copy it paste and then highlight the whole line and then alt three to comment it out because we're going to go back to using this data but I do just like I said I want to show you guys how to convert Unix time just in case you need it so then delete all the way up so we'll use this one for now but soon we'll return back to our old uh format so here what we need to do now is we need to convert the date uh the date time so we use date com for date converter and that's going to be equal to do we have numpy yes so date con equals NP dot uh vectorize and we vectorize based on the dt. DAT time.time stamp or actually from time stamp so from time stamp from time stamp Okay so so now we're going to say uh date equals date com uh on the date cool so that should do it so now instead of using this 10 year range let's do a 10 instead of y 10 D which we know gives us Unix time instead and we will run that I think we're all set let's run it see what happens that worked out cool okay so what we have here are the days and and and it probably looks kind of funny to you because they're connected but these are basically you know this is where the stock market was open I mean this is intraday data here so we can zoom in this is like one day of data okay so stock market open stock market close and then they're just basically connect this is a close of the market open trade close trade close trade close trade close Okay so that's what's going on here uh but anyway long story short it worked so that's how you can convert Unix timestamps to matplot lib dates that's all I'm going to say on them I'm going to return back to the original 10 years information uh in the next tutorial and we're going to get rid of this information here but for those of you who want to do intraday data go for it uh pretty soon we're going to get off of um so a lot of people will be like all I want to get rid of these nasty lines well pretty soon we're going to be doing Candlestick graphs so there won't be this nasty line connecting them so keep that in mind uh but anyway I did just want to show how you could convert from Unix time to dates time because it's pretty important to know how to do it anyways so that's it for this tutorial if you have any questions or comments uh regarding this tutorial please feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until the next video

Original Description

In this Matplotlib tutorial video, we're going to cover how to handle the conversion of unix time stamps to then plot date stamps in your graph. With the Yahoo Finance API, you will notice that if you use large time frames, like 1y for one year, you will get those date stamps we've been working with, but, if you use something like 10d for 10 days, you will instead get timestamps that are unix time. Unix time is the number of seconds after Jan 1st 1970, and it represents a normalized method for time across programs. That said, Matplotlib doesn't want unix time stamps. sample code: http://pythonprogramming.net http://hkinsley.com https://twitter.com/sentdex http://sentdex.com http://seaofbtc.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This tutorial teaches how to convert Unix time stamps to date stamps for plotting in Matplotlib, using the Yahoo Finance API and Python's datetime module. It covers the importance of handling time series data and converting data formats. The tutorial provides a step-by-step guide on how to achieve this conversion and plot the data in Matplotlib.

Key Takeaways
  1. Import necessary modules (datetime, numpy)
  2. Use the Yahoo Finance API to retrieve data
  3. Convert Unix time stamps to date stamps using datetime module
  4. Vectorize the conversion using numpy
  5. Plot the date stamps in Matplotlib
💡 Converting Unix time stamps to date stamps is crucial for plotting time series data in Matplotlib, and utilizing the datetime module and numpy can simplify this process.

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →