benn.9833
benn.9833

Reputation: 311

matplotlib finance candlestick2_ohlc: vertical line color and width

In regards to candlestick2_ohlc's vertical line, how do I change its default color of black to something else? I've been looking at the source code, but I couldn't figure out how to change it correctly.

Also, when you are dealing with roughly 400 data points or more, the 'width' parameter needs to be rather larger. But when its large and you zoom in, the candlesticks overlap.

Anyway to work around this?

from matplotlib.finance import candlestick2_ohlc
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from pandas import read_csv
import numpy as np

path1 = "./ES 06-15.Last.txt"  # typical stock data
t_0 = 30
t_end = 431
N_data = read_csv(path1, sep=';|,', names=['datetime1', 'open1', 'high1',
                  'low1', 'close1', 'volume1'],
                  skiprows=t_0, nrows=t_end - t_0 + 3,
                  converters={'open1': np.float32, 'high1': np.float32,
                              'low1': np.float32, 'close1': np.float32})

fig = plt.figure(facecolor='k')
ax1 = plt.subplot(1,1,1, axisbg='#191919')

ax1.yaxis.label.set_color('w')
ax1.xaxis.label.set_color("w")
ax1.spines['bottom'].set_color("#5998ff")
ax1.spines['top'].set_color("#5998ff")
ax1.spines['left'].set_color("#5998ff")
ax1.spines['right'].set_color("#5998ff")
ax1.tick_params(axis='y', colors='w')
ax1.tick_params(axis='x', colors='yellow')
plt.ylabel('Price')

x = np.arange(len(N_data))
my_xticks = N_data['datetime1']
plt.xticks(x,my_xticks)
ax1.xaxis.set_major_locator(mticker.MaxNLocator(6))
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(25)

candlestick2_ohlc(ax1, N_data['open1'], N_data['high1'],
                  N_data['low1'], N_data['close1'], width=2,
                  colorup='#008000', colordown='#FF0000', alpha=1)
plt.show()

Upvotes: 0

Views: 4370

Answers (1)

footonwonton
footonwonton

Reputation: 794

I know this is an oldish question, however here is your answer.

Open the source file located in /site-packages/matplotlib/finance.py and edit the candlestick2_ohlc function around line 1127.

There should be the following code:

rangeCollection = LineCollection(rangeSegments,
                                 colors=((0, 0, 0, 1), ),
                                 linewidths=lw,
                                 antialiaseds=useAA,
                                 )

If you change to.

rangeCollection = LineCollection(rangeSegments,
                                 colors=colors,  # << this bit
                                 linewidths=lw,
                                 antialiaseds=useAA,
                                 )

This will make the wick for each candle the same colour as the main body.

Alternatively, if you wish to have the same colour for each candle wick regardless of direction, edit the def section like so.

def candlestick2_ohlc(ax, opens, highs, lows, closes, width=4,
                 colorup='k', colordown='r', linecolor=None,
                 alpha=0.75,
                 ):

Adding linecolor=None will provide a default setting.

Then edit again.

if linecolor is None:
    linecolor = colors
rangeCollection = LineCollection(rangeSegments,
                                 colors=linecolor,
                                 linewidths=lw,
                                 antialiaseds=useAA,
                                 )

Now when the function is called from the main script without the linecolor parameter, the default will be... wick same colour as the candle body.

candlestick2_ohlc(ax1, popen, phigh, plow, pclose, width=2,
                  colorup='g', colordown='r', alpha=1)

or if white is desired.

candlestick2_ohlc(ax1, popen, phigh, plow, pclose, width=2,
                  colorup='g', colordown='r', linecolor='w', alpha=1)

Upvotes: 1

Related Questions