Matplotlib Tutorial 9 - converting data from the internet

sentdex · Beginner ·🛠️ AI Tools & Apps ·10y ago
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 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 datestamps from the Yahoo finance API to a format that Matplotlib understands, and how to create a custom function to achieve this conversion. The tutorial covers the basics of data visualization with Python and Matplotlib, and provides a practical example of how to work with API data.

Key Takeaways
  1. Define a custom function bytespdate2num to convert datestamps
  2. Use the MDates.strpdate2num function to convert datestamps to a Matplotlib-compatible format
  3. Plot the converted data using Matplotlib's plot_date function
  4. Customize the plot with labels, titles, and rotation
💡 The bytespdate2num function is a custom solution to convert datestamps from the Yahoo finance API to a format that Matplotlib understands, and is necessary because the default conversion function does not work in Python 3.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →