How to Create a Candlestick Chart in Matplotlib?
A candlestick chart, often known as a Japanese candlestick chart, is a financial chart that shows the price movement of stocks, derivatives, and other financial instruments in real-time, there are simply four essential components that must be examined. The open, high, low, and close are the four key elements, the candlestick chart has been used. It’s one of the world’s oldest charts.
The syntax of making a candlestick chart is as follows. Here we have used plt.bar method to build the candlestick chart.
- “up” dataframe will store the stock_prices when the closing stock price is greater than or equal to the opening stock prices
- “down” dataframe will store the stock_prices when the closing stock price is lesser than the opening stock prices
Example 1: Python program to plot 4 columns with up and down sticks
We can also make a candlestick chart by using the mpl_finance module. To use mpl_finance we need to install it first, which can be done by using the code. We have to install mpl_finance.
pip install mpl_finance
candlestick_ohlc(ax, ohlc.values, width, colorup, colordown)
- ac is the axis
- values are the input values
- width is the width of each candle stick
- colorup is the color for up sticks
- colordown is the color for down sticks
Example 2: Here, we define a dataset of stock prices that contains 5 parameters i.e open, close, high, low, and index (i.e date) and after that, we used pandas.to DateTime to convert the date, and then pandas.astype to convert all of the data to float ().
We can also use a dataframe rather than defining it on our own. The dataset can be downloaded by clicking here.