4

I create a program that generate the candlestick chart and I have a problem with it. my data type is stock so it only have a data in weekdays.

So after I plotted it, it has spaces between the candlesticks.

result image

How can I remove these?

Here is my plotter function code:

ohlc = []
date = []
while (loop < candleLength) :
    date.append(dateToFloat(stockData['date'][loop]))
    append = date[loop], stockData['open'][loop], stockData['high'][loop], stockData['low'][loop], stockData['close'][loop]
    ohlc.append(append)
    loop += 1

fig = plt.figure()
ax = plt.subplot2grid((1,1), (0,0))

candlestick_ohlc(ax, ohlc, width=0.66, colorup='#4dff4d', colordown='#ff471a')


for label in ax.xaxis.get_ticklabels():
    label.set_rotation(45)

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.xaxis.set_major_locator(mondays)
ax.grid(True)
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
YONG BAGJNS
  • 501
  • 2
  • 8
  • 21

3 Answers3

2

You can set xticks sequential numbers (0, 1, 2, 3 ... up to the number of candles you want to plot) and then set xticklabels actual dates for better visualization. See my answer to this question for detailed implementation.

Elgin Cahangirov
  • 1,932
  • 4
  • 24
  • 45
0

Check out this link: How do I plot only weekdays using Python's matplotlib candlestick?

This should answer your question. I use it as well in my code.

Community
  • 1
  • 1
Aloex
  • 103
  • 8
0

Try adding this into your code.

def mydate(x,pos):
    try:
        return mdates[int(x)].strftime('%Y-%m-%d')
    except IndexError:
        return ''

ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate))

Like this:

import matplotlib.ticker as ticker

ohlc = []
date = []
while (loop < candleLength) :
    date.append(dateToFloat(stockData['date'][loop]))
    append = date[loop], stockData['open'][loop], stockData['high'][loop], stockData['low'][loop], stockData['close'][loop]
    ohlc.append(append)
    loop += 1

fig = plt.figure()
ax = plt.subplot2grid((1,1), (0,0))

candlestick_ohlc(ax, ohlc, width=0.66, colorup='#4dff4d', colordown='#ff471a')


for label in ax.xaxis.get_ticklabels():
    label.set_rotation(45)


def mydate(x,pos):
    try:
        return mdates[int(x)].strftime('%Y-%m-%d')
    except IndexError:
        return ''

ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate))

ax.xaxis.set_major_locator(mondays)
ax.grid(True)
江明哲
  • 27
  • 5