Matplotlib Tutorial 14 - candlestick OHLC graphs
Skills:
CV Basics85%
Key Takeaways
This video tutorial covers creating open, high, low, close (OHLC) candlestick charts using Matplotlib, a popular data visualization library in Python, with specific tools including Matplotlib, Matplotlib.ticker, and Matplotlib.finance.
Full Transcript
hello everybody and welcome to another matte plot Lube tutorial video in this video we're going to be building on the uh series that we've been working on here and that is how to do Candlestick open high low close graphs so uh with that let's just Dive Right In so first of all we're going to need to import a couple more things from map plot lib so we're going to take this line here and let's just cut that put it together here it's always a good idea to have the Imports with the specific module kind of together we're also going to import uh matplot li. ticker as M ticker and then we're going to go from Matt plot Matt plot oh my goodness Matt plot live. Finance uh we want to import candle stick ohlc so that's going to allow us to plot these really fancy Candlestick graphs if you're not familiar with a Candlestick graph it's just a really nice concise way to plot open high low close and Direction all in like one little marker thing so it's really useful um if you don't know anything about Candlestick charts don't worry about it uh it's really quick to make them so it's won't cost you too much of your time but feel free to skip if you'd like so uh the first thing that we want to do or really the only thing we need to do when it comes to candlesticks is to come on down to let's um basically where we grab our data and in fact really all this stuff like we can keep date and price but like all this stuff here uh we don't need any of it so I'm just going to like delete okay so now we've got all of our information date close high low open all that what we want to do is we need to create a um we have to create a new kind of list so what we're going to do is we're going to say we're going to create a pretty crappy uh counter but that's okay so we're going to say x equals 0 y will be equal to the Len of date and then we're going to say this new or we're going to say ohlc equals uh this empty list and then what we're going to say is while X is less than y we're going to build this list up we're going to say append me equals and then it's going to be date x uh open p x High p x low p x close p x and then we'll also throw in volume volume X okay so that's the line that we're about to append and then we'll go ahead and say ohlc do append and what do we want to append well we want to append me then uh we're going to say X+ equals 1 so that should be uh decent enough uh so then we've populated this with date open high low close volume data and then now we can plot that data super quick and easy so to plot it what we do is we're going to use Candlestick ohlc it's this kind of special little function here so we'll take that function we'll come on down here and what we're going to do is Candlestick ohlc which we imported then you specify onto what subplot are we going to slap this bad boy ax1 then we're going to say what is the bad boy it's ohlc and then uh we specify well you really get away with probably just this that should be good enough and then we'll talk about some of the customization that you can do so let's save and run that real quick and we don't have label so that's what's hitting us there and uh we also see shucky darns we lost our dates so we have to handle for that map plot lib so now these are candlesticks it's really hard to see it you know they're like it little basically but you can kind of see it when it goes up the candlesticks are generally black and when it goes down they're red we can zoom in though we really get a good good feel for these candlesticks but right now there's just way too much data here so let's change let's go up to the top here instead of 10 years let's do I don't know 1 M for one month let's see how that looks that's a little more reasonable so we'll go with that for now Okay so we've got our candlesticks now what can we do from here well ready in Black well it okay but it doesn't make the most sense so let's uh change those colors so first of all to do that uh what we can say is we can say uh width we can change the width this is the width of each little candle we'll just do 0.6 for now and probably even less 0.4 is probably best and then we can specify color up equals something and then color down equals something now these can be anything you could just say color up is G and color down is red uh that should should be fine uh but we we'll use this hex color thing again and like let's just pick color down we'll say uh kind of like a a lightish red so we'll do this DB whatever copy that um move the color down there and then let me just pick a random little green here this one will do pound this so 77d 879 and then db3 f3f that'll do so now what we'll do is uh let's run that see how we're doing with the new width and stuff okay cool so that makes a little more sense the drops are red the Rises are green the Earth is on its axes again we still have this date problem so now what we do is let's close out of this oh map plot live you're killing me Smalls now what we're going to do is we're going to you know restart our little customization of ax1 and the way way that we do this is first of all uh we got rid of that four darn it let's do for label in ax1 do whoops ax1 dox axis. getor tick labels labels what do we want to do well we want to label that setor rotation to 45 should have left that but oh well next what we're going to do is we're going to say uh ax1 um well there's a couple things here first let's just do the the date conversion I guess so ax1 dox axes. setor major uncore for matter we want to set that formatter to MD dates. dat formatter so remember earlier I was saying how the M plot lib dates are like some sort of 70,000 number little wrong 700,000 but as you can see that those were the xaxis labels so we have to convert those that 700,000 number that's a mat plot lib numpy conversion date number we want to convert that using a date formatter and we can now we can tell Mt plot lib what's the format of the date we want so you could literally say you could say I want my date to be percent year uh followed by a parenthesis closing percent month followed by Dash percent D okay you are totally permitted to do that let's save and run it and sure enough we can see 25 2015 close parentheses 0616 for example there obviously that's really silly and absurd I just want you guys to understand that you can do anything you want while you're converting into and out of dates you can do all kinds of fancy stuff but uh let's go ahead and fix that now make that uh a dash let be reasonable everybody and uh now what we're going to do like let's go ahead and run that one real quick and that's cool but let's say like we want a little more granularity like we have tons of space here let's use it well you can actually specify the exact number of like little markers that you want and the way that you can do that is is uh by saying ax1 xaxis do setor major locator and you can set that to M tier. Max NL pay attention capital m capital n capital L there locator and then here you specify how many you want we want 10 give us 10 so we're asking for 10 here now we've got our 10 little locator uh things so uh let's zoom in here so you should always have basically those 10 now sometimes it's going to trim those sides it's just going to do that as a favor to you but generally you'll have as many as you want you could add more or less like we could say um x axis let's say we want I don't know five okay so let's do five so you can see it's trimmed one but we've got you know one two three four and stuff like that so anyways um and then some cases it gives us three apparently um but it gives it kind of like a starting point I suppose so we'll go with 10 that seems like a pretty good uh number to use now uh I think that's it for this uh tutorial in the next tutorial we're going to be talking all about making graphs pretty because this graph it doesn't look horrible but it does not look great and the other thing I wouldn't mind doing to let's go ahead and add a grid back let's say uh ax1 grid true okay so anyways yeah in the next tutorial what we'll talk about is kind of making a graph more appealing okay so there's a lot of options here and in the previous matplot lib series that I did we spent a lot of time customizing our chart and nowadays it's a lot easier to customize your chart so that's one of the main reasons I've decided to redo the series is because that was taking a lot of time but it's a lot easier nowadays so that's what we're going to be talking about in the next tutorial so stay tuned for that if you have any questions or comments please do feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time
Original Description
In this Matplotlib tutorial, we're going to cover how to create open, high, low, close (OHLC) candlestick charts within Matplotlib. These graphs are used to display time-series stock price information in a condensed form.
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: CV Basics
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