Skip to content

Commit 046fe5c

Browse files
committed
fix x-axis labels
1 parent 09fc781 commit 046fe5c

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

examples/customization_and_styles.ipynb

Lines changed: 8 additions & 3 deletions
Large diffs are not rendered by default.

src/mplfinance/_utils.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
263263

264264
return rangeCollection, barCollection
265265

266-
def _construct_renko_collections(renko_params, closes, marketcolors=None):
266+
def _construct_renko_collections(dates, renko_params, closes, marketcolors=None):
267267
"""Represent the price change with bricks
268268
269269
Parameters
@@ -296,24 +296,30 @@ def _construct_renko_collections(renko_params, closes, marketcolors=None):
296296

297297
cdiff = [(closes[i+1] - closes[i])/brick_size for i in range(len(closes)-1)] # fill cdiff with close price change
298298

299-
bricks = []
299+
bricks = [] # holds bricks, 1 for down bricks, -1 for up bricks
300+
new_dates = [] # holds the dates corresponding with the index
300301

301302
prev_num = 0
302303
start_price = closes[0]
303304

304305

305-
for diff in cdiff:
306-
if diff > 0:
307-
bricks.extend([1]*int(round(diff, 0)))
306+
for i in range(len(cdiff)):
307+
num_bricks = abs(int(round(cdiff[i], 0)))
308+
309+
if num_bricks != 0:
310+
new_dates.extend([dates[i]]*num_bricks)
311+
312+
if cdiff[i] > 0:
313+
bricks.extend([1]*num_bricks)
308314
else:
309-
bricks.extend([-1]*abs(int(round(diff, 0))))
315+
bricks.extend([-1]*num_bricks)
310316

311317
verts = []
312318
colors = []
313319
for index, number in enumerate(bricks):
314-
if number == 1: # positive
320+
if number == 1: # up brick
315321
colors.append(uc)
316-
else: # negative
322+
else: # down brick
317323
colors.append(dc)
318324

319325
prev_num += number
@@ -334,7 +340,7 @@ def _construct_renko_collections(renko_params, closes, marketcolors=None):
334340
linewidths=lw
335341
)
336342

337-
return (rectCollection, )
343+
return (rectCollection, ), new_dates
338344

339345
from matplotlib.ticker import Formatter
340346
class IntegerIndexDateTimeFormatter(Formatter):

src/mplfinance/plotting.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,20 @@ def plot( data, **kwargs ):
258258
else:
259259
fmtstring = '%b %d'
260260

261-
if config['show_nontrading']:
262-
formatter = mdates.DateFormatter(fmtstring)
263-
xdates = dates
264-
else:
265-
formatter = IntegerIndexDateTimeFormatter(dates, fmtstring)
266-
xdates = np.arange(len(dates))
267-
268-
ax1.xaxis.set_major_formatter(formatter)
269-
270261
ptype = config['type']
271262

263+
if ptype is not 'renko':
264+
if config['show_nontrading']:
265+
formatter = mdates.DateFormatter(fmtstring)
266+
xdates = dates
267+
else:
268+
formatter = IntegerIndexDateTimeFormatter(dates, fmtstring)
269+
xdates = np.arange(len(dates))
270+
271+
ax1.xaxis.set_major_formatter(formatter)
272+
273+
274+
272275
renko_params = config['renko_params']
273276

274277
collections = None
@@ -280,8 +283,13 @@ def plot( data, **kwargs ):
280283
marketcolors=style['marketcolors'] )
281284
elif ptype == 'renko':
282285
renko_params = _process_kwargs(kwargs['renko_params'], _valid_renko_kwargs())
283-
collections = _construct_renko_collections(renko_params, closes,
284-
marketcolors=style['marketcolors'] )
286+
collections, new_dates = _construct_renko_collections(dates, renko_params, closes,
287+
marketcolors=style['marketcolors'] )
288+
289+
formatter = IntegerIndexDateTimeFormatter(new_dates, fmtstring)
290+
xdates = np.arange(len(new_dates))
291+
292+
ax1.xaxis.set_major_formatter(formatter)
285293
elif ptype == 'line':
286294
ax1.plot(xdates, closes, color=config['linecolor'])
287295
else:

0 commit comments

Comments
 (0)