Udit Vashisht
Author: Udit Vashisht

Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

  • Sept. 16, 2019, 11:38 a.m.
  • 4 minutes read
Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

Python Candlestick Chart in Matplotlib

Python Candlestick Chart | Chapter 11

A candlestick chart or Japanese candlestick chart is a financial chart used to depict the price movement of securities, derivatives etc. in financial market. In this tutorial we will use Python to plot Candlestick Chart.

A Candlestick Chart essentialy have an Open, High, Low and Close (also called OHLC). Matplotlib has a moudule called matplotlib.finance which has a method to create candlestick chart in Python, but the same has been deprecated. However, plotting a Candlestick chart in Python and Matplotlib is still possible with the help of module mpl_finance, which consists of code extracted from the deprecated matplotlib.finance module.

Python Candlestick Plot in Matplotlib

Now, we will create a Candlestick chart in Python. For that we will have to install mpl_finance module and we will be using candlestick_ohlc method to plot the data.

First of all, we will install mpl_finance:-

pip install mpl_finance

We will be creating Candlestick Chart/Plot for NIFTY 50 data for the period from 01.08.2019 to 13.09.2019, which I have downloaded from here and named it as ‘candlestick_python_data.csv’. We will be using pandas’ read_csv() method to read the csv files. Quickly create a Python file ‘python_candlestick_chart.py’ and start coding:

# python_candlestick_chart.py

import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import pandas as pd
import matplotlib.dates as mpl_dates


# Extracting Data for plotting
data = pd.read_csv('candlestick_python_data.csv')
ohlc = data.loc[:, ['Date', 'Open', 'High', 'Low', 'Close']]
ohlc['Date'] = pd.to_datetime(ohlc['Date'])
ohlc['Date'] = ohlc['Date'].apply(mpl_dates.date2num)
ohlc = ohlc.astype(float)

# Creating Subplots
fig, ax = plt.subplots()

candlestick_ohlc(ax, ohlc.values, width=0.6, colorup='green', colordown='red', alpha=0.8)

# Setting labels & titles
fig.suptitle('Daily Candlestick Chart of NIFTY50')

# Formatting Date
date_format = mpl_dates.DateFormatter('%d-%m-%Y')



Let me quickly walk you through the code.

  1. Firstly, we have made the necessary imports, we will be using matplotlib.pyplot to plot the chart, candlestick_ohlc from mpl_finance to plot the candlestick chart, pandas to extract data using read_csv method, matplotlib.dates for formatting the dates.
  2. We are using the style ‘ggplot’. You can read more about styles in our Chapter 1.
  3. Then we are extracting the data from the downloaded csv using pandas.read_csv(). Thereafter, we have converted the date to pandas.to_datetime and finally we have converted all the data to float using pandas.astype().You can read more about extracting data from csv from here.
  4. There after we have create a simple Python Subplot. Read more about subplots in Python from here.
  5. Then we have used candlestick_ohlc method to plot the candlestick chart in Python. We can pass arguments, like width, colorup, colordown, alpha etc. to this method.
  6. Thereafter, we have set the labels and title to our Python Subplot. Learn more about it from here.
  7. Finally, we have formatted the date to our liking (read more from here ) and used plt.show() to plot the Python Candlestick Chart.


Python Candlestick Plot with SMA Overlay in Matplotlib

We can also overlay the Simple Moving Average(SMA) on the Python Candlestick Plot. Let us calculate the SMA for 5 days (Since, we started with data of only 30-40 days in beginning) and overlay it on the existing Python Candlestick Plot in Matplotlib.

# python_candlestick_chart.py

ohlc['SMA5'] = ohlc['Close'].rolling(5).mean()
ax.plot(ohlc['Date'], ohlc['SMA5'], color='green', label='SMA5')

fig.suptitle('Daily Candlestick Chart of NIFTY50 with SMA5')




Table of Contents of Matplotlib Tutorial in Python

Matplotlib Tutorial in Python | Chapter 1 | Introduction

Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4

Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

Python Histograms | Matplotlib Tutorial in Python | Chapter 6

Scatter Plotting in Python | Matplotlib Tutorial | Chapter 7

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8

Python Realtime Plotting | Matplotlib Tutorial | Chapter 9

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10

Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

If you have liked our tutorial, there are various ways to support us, the easiest is to share this post. You can also follow us on facebook, twitter and youtube.

In case of any query, you can leave the comment below.

If you want to support our work. You can do it using Patreon.

Related Posts

How to host and schedule your python script on PythonAnywhere?
By Udit Vashisht | 9 months ago

In our last post here, we learned how to create a python script which will automatically delete your messages from gmail account based on a query.

The complete ready-to-use code can be found here.

Follow the steps in the above tutorial to download the ‘credentials.json’ file and ...

Read More
Chapter 7- Numbers, Conversions and Operators
By Udit Vashisht | 7 months, 1 week ago

In the previous chapter, we learned about various data types. From this chapter onward, we will learn in detail about each data type.


This data type stores the numeric values and is immutable. For the beginners, it is sufficient to understand that immutable objects are those which can not ...

Read More
Python object-oriented programming (OOP) - A complete tutorial
By Udit Vashisht | 2 weeks, 5 days ago

Python object-oriented programming (OOP)

Object-oriented programming

Object-oriented programming also known as OOP is a programming paradigm that is based on objects having attributes (properties) and procedures (methods). The advantage of using Object-oriented programming(OOP) is that it helps in bundling the attributes and procedures into objects or modules. We can ...

Read More