Matplotlib Tutorial 14 - candlestick OHLC graphs

sentdex · Beginner ·🛠️ AI Tools & Apps ·10y ago
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 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 create and customize candlestick OHLC graphs using Matplotlib, which is useful for visualizing time-series stock price information. By following the steps, viewers can create their own interactive and informative graphs. The tutorial covers importing necessary modules, creating OHLC data, plotting the data, and customizing the plot.

Key Takeaways
  1. Import necessary modules including Matplotlib and Matplotlib.finance
  2. Create a new list to store OHLC data
  3. Use the Candlestick function to plot OHLC data on a subplot
  4. Customize the plot by changing the colors of the candlesticks
  5. Change the width of each candle using the 'width' parameter
  6. Specify color up and color down using the 'colorup' and 'colordown' parameters
  7. Use the 'DateFormatter' class to convert Matplotlib's date numbers to a human-readable format
  8. Use the 'MaxNLocator' class to specify the exact number of tick marks on the x-axis
💡 Matplotlib's Candlestick function provides a convenient way to create candlestick OHLC graphs, which can be customized to improve readability and aesthetics.

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →