How to Create a Candlestick Chart in Matplotlib?

Candlestick charts are a popular way to visualize stock market data. They show the opening, closing, high, and low prices of a stock or security for a given time period. Each candlestick represents one time period, where the body shows opening and closing prices, and the wicks (thin lines) show the highest and lowest prices traded during that period.

In this tutorial, we will use Matplotlib to create a candlestick chart for stock price data. We'll use the plt.bar() function to draw the individual components of each candlestick.

Understanding Candlestick Components

A candlestick chart consists of several visual elements ?

  • Body Rectangle showing opening and closing prices
  • Upper Wick Line from body to highest price
  • Lower Wick Line from body to lowest price
  • Color Green for price increase, red for price decrease

Syntax for Creating Candlesticks

We use plt.bar() to create each component of the candlestick ?

# For bullish (up) candles
plt.bar(up.index, up.close-up.open, width, bottom=up.open, color='green')
plt.bar(up.index, up.high-up.close, thin_width, bottom=up.close, color='green')
plt.bar(up.index, up.low-up.open, thin_width, bottom=up.open, color='green')

# For bearish (down) candles  
plt.bar(down.index, down.close-down.open, width, bottom=down.open, color='red')
plt.bar(down.index, down.high-down.open, thin_width, bottom=down.open, color='red')
plt.bar(down.index, down.low-down.close, thin_width, bottom=down.close, color='red')

Example: Weekly Stock Price Candlestick Chart

Let's create a candlestick chart for a week of stock data. First, we'll prepare the data and separate bullish from bearish days ?

import pandas as pd
import matplotlib.pyplot as plt

# Create sample stock data for one week
stock_data = pd.DataFrame({
    'open': [60, 70, 80, 90, 100, 110, 120],
    'close': [55, 85, 75, 100, 95, 120, 105],
    'high': [70, 95, 85, 110, 105, 125, 125],
    'low': [50, 60, 70, 80, 85, 105, 100]
}, index=pd.date_range("2023-04-01", periods=7, freq="d"))

print("Stock Data:")
print(stock_data)
Stock Data:
            open  close  high  low
2023-04-01    60     55    70   50
2023-04-02    70     85    95   60
2023-04-03    80     75    85   70
2023-04-04    90    100   110   80
2023-04-05   100     95   105   85
2023-04-06   110    120   125  105
2023-04-07   120    105   125  100

Now let's create the candlestick chart by separating bullish and bearish days ?

import pandas as pd
import matplotlib.pyplot as plt

# Create sample stock data
stock_data = pd.DataFrame({
    'open': [60, 70, 80, 90, 100, 110, 120],
    'close': [55, 85, 75, 100, 95, 120, 105],
    'high': [70, 95, 85, 110, 105, 125, 125],
    'low': [50, 60, 70, 80, 85, 105, 100]
}, index=pd.date_range("2023-04-01", periods=7, freq="d"))

# Separate bullish (up) and bearish (down) days
up_days = stock_data[stock_data.close >= stock_data.open]
down_days = stock_data[stock_data.close < stock_data.open]

# Set colors and widths
bull_color = 'green'
bear_color = 'red'
body_width = 0.6
wick_width = 0.05

plt.figure(figsize=(10, 6))

# Plot bullish candlesticks
plt.bar(up_days.index, up_days.close - up_days.open, body_width, 
         bottom=up_days.open, color=bull_color, alpha=0.8, label='Bullish')
plt.bar(up_days.index, up_days.high - up_days.close, wick_width, 
         bottom=up_days.close, color=bull_color)
plt.bar(up_days.index, up_days.low - up_days.open, wick_width, 
         bottom=up_days.open, color=bull_color)

# Plot bearish candlesticks
plt.bar(down_days.index, down_days.close - down_days.open, body_width, 
         bottom=down_days.open, color=bear_color, alpha=0.8, label='Bearish')
plt.bar(down_days.index, down_days.high - down_days.open, wick_width, 
         bottom=down_days.open, color=bear_color)
plt.bar(down_days.index, down_days.low - down_days.close, wick_width, 
         bottom=down_days.close, color=bear_color)

# Customize the chart
plt.title('Weekly Stock Price Candlestick Chart', fontsize=14, fontweight='bold')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.xticks(rotation=45)
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[A candlestick chart showing 7 days of stock data with green candles for bullish days and red candles for bearish days, including proper wicks showing high/low prices]

Key Components Explanation

Component Purpose Width
Body Shows open to close price range Wide (0.6)
Upper Wick Shows high to close/open price Thin (0.05)
Lower Wick Shows low to open/close price Thin (0.05)

Color Coding

Candlestick charts use color to quickly convey price movement ?

  • Green/White Bullish day (close >= open)
  • Red/Black Bearish day (close < open)

Conclusion

Creating candlestick charts in Matplotlib involves using plt.bar() to draw bodies and wicks separately. This approach gives you full control over the appearance and allows for customization of colors, widths, and styling to effectively visualize stock price movements.

---
Updated on: 2026-03-27T01:22:11+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements