3

I have noticed in the Matplotlib API version 2.2.2, matplotlib.finance is no longer available. In the past that was what I used to create Candlestick Charts.

With matplotlib.finance gone, I am reading that the recommended way is to use matplotlib.axes.bar?

Is there a better way in python to build candlestick charts? I always thought matplotlib was the go to, for plotting, but it doesn't seem like it has anything directly for building candlestick charts.

Otherwise, how does my code look? Aside from a bad color scheme.

import numpy as np
import matplotlib.pyplot as plt

## OHLC Data
open =  np.array([479.09, 499.11, 478.96, 468.63, 449.13, 460.74, 457.99, 449.52, 479.33, 471.89])
high =  np.array([511.67, 515.28, 484.36, 469.94, 468.90, 471.88, 470.60, 484.52, 485.28, 484.87])
low =   np.array([465.14, 469.17, 462.64, 439.52, 441.37, 455.00, 446.00, 448.62, 464.28, 458.74])
close = np.array([499.11, 478.97, 468.64, 449.13, 460.74, 457.99, 449.60, 479.33, 471.89, 462.30])



n_groups = open.size            # number of bars
index = np.arange(n_groups)     # evenly space each bar

## Create Plot Figure
fig, ax = plt.subplots()

## Translate OHLC Data to Bar Graph Data
bar_height = np.absolute(open - close)
bar_bottom  = np.minimum(open, close)

yerr_high = high - np.maximum(open,close)
yerr_low =  np.minimum(open,close) - low + bar_height

bar_width = 0.5

## Color


## Set Chart Alpha Levels
opacity = 1
error_config = {'alpha': opacity}

rects1 = ax.bar(
            index,                                 # X Coordinate of bars

            bar_height,                            # [Largest Price,] (Open/Close)
            bar_width,                             # bar width
            bar_bottom,                            # [Smallest Price,] (Open/Close)
            alpha = opacity,                       # bar alpha level

            error_kw = error_config,               # kwargs to be passed to the errorbar method
            yerr = [yerr_low, yerr_high]           # error bar [Low Price, ], [High Price, ]
            )

## Set Chart Axis and Title
ax.set_xlabel('Date')
ax.set_ylabel('Price')
ax.set_title('Candlestick Chart')
ax.set_xticks(index)

## Plot Chart
fig.tight_layout()
plt.show()
khorn06
  • 53
  • 1
  • 5

1 Answers1

1

This module is deprecated in 2.0 and has been moved to a module called mpl_finance.

source: https://matplotlib.org/api/finance_api.html

You can find more details here: Since matplotlib.finance has been deprecated, how can I use the new mpl_finance module?

and here: https://github.com/matplotlib/mpl_finance

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80