Matplotlib Tutorial 9 - converting data from the internet
Skills:
ML Pipelines60%
Key Takeaways
This tutorial covers converting datestamps from the Yahoo finance API to a format that Matplotlib understands, using a custom function bytespdate2num to achieve this conversion.
Full Transcript
what is going on everybody Welcome to the ninth data visualization with python and matap plot lib tutorial video where we left off we were basically pulling data from the internet we were we got the data we've separated it and we can even unpack it into these variables here but the problem is the date stamp here is a little problematic for us because it's not like a legible date okay so so to us we understand like okay we can see okay this is 2010 this is June 29th so we can kind of understand that but we have to be be very explicit to our programs uh what that date is now a lot of programs use what's called Unix time and Unix time is just the number of seconds after January 1st 1970 mot lip however doesn't use Unix time okay it has its own little conversion thing I don't know I don't even know what the what the conversion is but it usually Corr responds to numbers like 70,000 or so so anyways uh that's that moving along um so we need to convert this and to start converting data we have to understand that it the data comes through as like a a bite string it's not even a string so we have to convert it now usually just decode will work but to convert it to a true date stamp in string form for whatever reason do decode is not enough we have to do our own kind of decoding as well so in this uh tutorial we're going to write that this decoding function here because it doesn't exist yet we'll write that and then we'll actually pull the data and we'll graph it if the star is a line so uh let's clean this function up a little bit though there's no reason to have all these white spaces all right so we're going to make this function so let's copy that come up to the top and now we're going to Define and we're going to say bite state to num and then we basically the first thing that gets passed here we're going to say is fmt that's short for format so format is this data right here it's the format of that date stamp now the next thing that we're going to do here is um basically we've got format and then we're going to actually have one more thing we're going to have the encoding and the encoding is um utf8 that's okay so that's what we need for the internet data so now we build this function we're going to say St Str converter and that's going to be equal to MD dates. stpd to num so this is strip dat to number based on what based on the format okay now just for the record if you don't understand this kind of decoding stuff that's okay that's not what this tutorial is supposed to be over back in Python 2 this was not necessary this function already kind of existed for you you didn't need to know how it worked but since it doesn't work in Python 3 for some reason we have to write our own okay and uh so this is hopefully you can understand feel free to ask about it if you want but don't be like frustrated if you don't understand it so anyway string converter that's going to be equal to M dates. stripd to number based on the format and again this is basically that that's going to strip it for the matap plot live date number that 70,000 number whatever that means then we're going to define the bytes uh conver and it's it converts b as in bites and it Returns the S which is just string and that's b. decode based on the encoding which is utf8 then it Returns the string converter converter the string converter of that new element s return not return and then uh at the end of this function we return the uh bytes converter okay so uh yeah so now return by yeah Okay so we've got that that'll return basically this answer and that should be good we should have the conversion if something goes wrong we'll we'll revisit that so now everything should be unpacked um I'm saying this as if like the program is like running live I'm just kind of running through the program in my head so we've unpacked everything with that function we have now uh converted everything and now we're actually ready to plot this stuff so um we we should be able to get rid of just like calling PLT do plot and close and stuff so let's try that and see what we do so we'll say PLT pt. plot and then the X will be a date and instead of just plot let's actually call plot plore date this notifies python that hey we're plotting dates uh and then so we plot dates and then date and then we'll plot the close P for close price and that should be fine the X label now is not X it's date y label is not yit price and that should be good let's uh let's run that and see see what happens um labels so for some reason we're passing we got this labels line which we shouldn't have gotten interesting one two three four five six so this labels is in there as well for some reason so if so let's go back up to this if values not in line and let's just add um if values not in line and labels not in line line some reason that was there I don't really recall ever seeing that one uh so if that's all true let's run this one more time okay we got a price awesome so this is uh when we use plot date for some reason the default is like more of a scatter I'm not sure why but we'll fix that right now uh so we go plot date and then just do a comma and then add the argument of just like this Dash so it's like you know your minus sign run that one more time and then now we've got just the price now we're getting this error about No Label that's because we didn't label the price so we can do um we'll just do keyword here label actually let's do here keyword argument should come last label equals um price okay good enough all right so now we've got uh we're plotting and you'll notice here we've got the dates down here at the bottom and what's kind of nifty is if we um pull this like this and like if we zoom in a specific spot you'll see that that they've expanded like originally they were just the years but if we zoom in we get the month in the year now we can keep zooming in now we've got the the the month Day in the year okay so depending on your Zoom uh that that date stamp is going to vary a little bit now what happens though if we zoom in all the way and we make this like the original size that it was we've got a little bit of overlap there on the dates um or like if we do this right so one little Nifty thing to do is to like rotate the dates and stuff and so we're going to be talking about rotating the dates and kind of cleaning up this chart a little bit in the uh next tutorial but at least for now you should be able to have this you know Price Line at least plotted up that we grabbed from Yahoo so if you have any questions comments concerns for whatever reason if your graph has not popped up please feel free to post below otherwise as always thanks for watching thanks for all the support subscriptions and until next time
Original Description
This tutorial is focused around converting the datestamps from the Yahoo finance API to times that Matplotlib understands. To do this, we're going to write a new function, bytespdate2num.
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: ML Pipelines
View skill →
🎓
Tutor Explanation
DeepCamp AI