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