Matplotlib Tutorial 18 - annotating last price to edge of matplotlib graph example
Key Takeaways
This video tutorial demonstrates how to annotate the last price of a stock to the edge of a Matplotlib graph, using tools like Matplotlib and techniques such as dynamic annotation and customization of box properties.
Full Transcript
what is going on everybody welcome to part 18 of our matplotlib tutorial series in this part what we're going to talk about is an example of using annotations to plot stock price outside of the graph kind of so when we plot a stock chart a lot of times it's it's hard to see like you can look at price on the left hand side there and you can kind of see what you know the current price is but a lot of times we care very highly about what's like the current last price so like with eBay for example it's like I don't know what I know what the current price is I mean I guess we would go with the close and it's probably here is the close or here is the close but we really don't know and that's anywhere between 61 and 61.5 so a lot of times what people want to do is they want to have an annotation of price over to the right hand side so this would be like an example of a graphing application that's doing that I just typed in like stock prices to Google Images and found this so a lot of graphing applications will do that they'll annotate the price to the right hand side mirror where that last price is and then it moves so it's really easy to find out hey what was the actual last price so a lot of people were asking me about this in my tkinter series and I just never got around to adding it but this is how you do it so we'll close out of this and close out of this and let's go ahead and add that now and so with annotations it's it's really not too much on top of what we've already you won't really won't learn anything new it's just an example of you know putting this to work so first I'm going to get rid of these annotations here so I'm just gonna highlight all of these and alt 3 that comments them out and in fact actually let's do let me just leave a note here annotation example with arrow and then down here this was our font dict example and then finally I don't know hard coded text okay and then I'll go this and now all three this okay so now what we're going to do is we're going to place what we would do like an annotations here so we would say ax 1 dot a note 8 and what we would annotate here is something like we would do the string of close P minus 1 so that's the like what is the text that we want to annotate here well we want to annotate whatever the last price was but that's gonna mean int so we're actually to float spoke we want to convert that to a string so we're doing that first so that's the last price then we have to like what are we annotating basically and so that would be like where is this going to be located so we want to annotate that and the annotate that it's like you know basically pointing to is date minus 1 and then close P minus 1 this one doesn't matter so much but it you know it could it depends on how you want to handle this but in our case it really won't make too much of a difference and then now we're gonna say XY text and before remember we did this as an axis for action well we're not going to do that because we actually we want this to kind of move dynamically with the price so we would say XY text equals date minus 1 so what was the last date and then what we're gonna do is we would add some to it because again this is actually the if you pay attention you can see here this would be your spine and this would be your spine and the annotation of price is usually like off-the-charts so to speak it's it's off of the graph so we want the same thing so we're gonna say the last date that's our x-axis so we're gonna say the last date plus let's do for for now and so that will hopefully move us over quite a bit and then we're you know where is the y that we want this well generally you place that annotation dynamically up or down in accordance to the price so for us we wanted we want it to be exactly where that P is so we're gonna say close P minus one and that really can be it but we can add a little bit more so first let's like bring this up and let's see how that looks non keyword argument what are we doing here XY text so we forgot to put this in parenthesis there let's try it one more time I'm still getting invalid syntax here what have we done so we I guess we just totally forgot yeah the parenthesis there so now we can see that at least we have the price that we're running us we don't really have the space here so we can well fix that because we already have the adjustments there so we have the price there so we can see the last price was sixty point nine nine so okay we'll close this let's go to the bottom here and on right currently it says point nine four we'll change that to point and I know and then what we're gonna do is come back up to our annotation here where are we there we go and now we can kind of customize that so if you if you can see here we've got so we actually real cover something new congratulations for those who didn't leave right away so you've got like this little box here right like we can we can customize to handle for that kind of situation so for example after we give the XY text we can give one more parameter here and we're gonna say B B well B box equals B Box underscore props and then we'll just add those props right up here so now we're gonna say is B Box underscore props equals so these are just the box properties right so props whenever you see that it's like short for properties we like to give nicknames through our things in Python because we're cool and so P box props will be the dict of and then in here we're gonna say box style and you can give a bunch of box styles so for those of you don't know you can always like like I've tried to kind of illustrate before if there's something you're just not you don't know on matplotlib or really any module you go to google and you type it in so for it in in this example if you want to know more about annotating matplotlib what am I more of my options you would type in matplotlib annotate hit enter on Google and you find yourself probably on this page and so this gives us all the kind of examples of annotations that we might might have and then down here these are kind of like the styles that are possible here and then these are kind of like visual examples of what those styles would look like so either round or maybe round 4 or the left arrow would be you know valid choices here so let me move this over again and so we're gonna say box style that's what we were just looking at was box styles we're gonna say box style equals and we'll just do the round let's choose that round option and then you can add padding if you want we're just gonna leave it alone for now and then you've got face color and edge color okay so face color we're gonna say the face color is white and then the edge color will make black which is K so W is short for white K is black because B is blue so anyway and then we can change the line width if we wanted I think we'll leave that for now we'll say line width equals 1 just so you can change it if you want but that's about it so there are a bunch of parameters there for our box here let's run it now and see what we're looking at all right so now you've got kind of like this box here that has our price of course our default load is still kind of man we should kind of give ourselves some more space on the bottom in the left so let's go ahead and change that to for those of you who are following linearly so bottom will give it point two four and left will give point 11 see how we do now beautiful we need a little bit more right though but anyway okay cool so that's looking pretty good so you could also do like that gives us the price but you could also go with like the left arrow for example so instead of round we can do I think it was L arrow L arrow like that find out yeah okay so there's that I'm just not I'm not the biggest fan of that little arrow I kind of like you know the round is probably my favorite but the arrow is a little more explicit and like what it's actually doing so you can feel free to choose whatever you want I'm gonna go with the round though I like that one the best I think or maybe around four whoops not round four round four that one's kind of good too I don't know you can go with whatever you want I think I'm gonna go back to round anyway that's it for this tutorial if you have any questions or comments up to this point feel free to leave them below otherwise as always thanks for watching thanks for all the support on subscriptions until next time
Original Description
In this Matplotlib tutorial, we're going to show an example of how we can track the last price of a stock, by annotating it to the right side of the axis like a lot of charting applications will do.
While people like to see historical prices in their live graphs, they also want to see the most recent price. What most applications do, is the annotate the last price at the y-axis height of the price, and then kind of highlight it and move it around a bit in a box of sorts as price changes. Using our recently-learned annotation tutorial, we can do this along with adding a bbox.
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: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
The no-code AI business stack that earns $5k/month on autopilot
Dev.to AI
The Future of QA: AI-Powered Test Automation Explained
Dev.to · Chaithrika Gummadi
Virtual Staging the Smart Way: AI-Powered Furnished Rooms from Raw Files
Dev.to AI
Why 60% of Real Estate Agents Using AI Are Wasting Their Time
Medium · AI
🎓
Tutor Explanation
DeepCamp AI