Skip to content

Commit 50df2ef

Browse files
fix tight_layout (issue #156)
1 parent a901ef4 commit 50df2ef

File tree

3 files changed

+479
-7
lines changed

3 files changed

+479
-7
lines changed

examples/scratch_pad/issues/Issue156_title_ruins_tight_layout.ipynb

Lines changed: 466 additions & 0 deletions
Large diffs are not rendered by default.

src/mplfinance/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
version_info = (0, 12, 5, 'alpha', 1)
2+
version_info = (0, 12, 5, 'alpha', 2)
33

44
_specifier_ = {'alpha': 'a','beta': 'b','candidate': 'rc','final': ''}
55

src/mplfinance/plotting.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ def plot( data, **kwargs ):
312312
formatter = IntegerIndexDateTimeFormatter(new_dates, fmtstring)
313313
xdates = np.arange(len(new_dates))
314314

315-
316315
if collections is not None:
317316
for collection in collections:
318317
axA1.add_collection(collection)
@@ -342,17 +341,17 @@ def plot( data, **kwargs ):
342341
else:
343342
axA1.plot(xdates, mavprices)
344343

344+
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
345345
if not config['tight_layout']:
346-
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
347346
#print('plot: xdates[-1]=',xdates[-1])
348347
#print('plot: xdates[ 0]=',xdates[ 0])
349348
#print('plot: len(xdates)=',len(xdates))
350349
#print('plot: avg_dist_between_points =',avg_dist_between_points)
351350
minx = xdates[0] - avg_dist_between_points
352351
maxx = xdates[-1] + avg_dist_between_points
353352
else:
354-
minx = xdates[0]
355-
maxx = xdates[-1]
353+
minx = xdates[0] - (0.45 * avg_dist_between_points)
354+
maxx = xdates[-1] + (0.45 * avg_dist_between_points)
356355

357356
if len(xdates) == 1: # kludge special case
358357
minx = minx - 0.75
@@ -376,7 +375,8 @@ def plot( data, **kwargs ):
376375
axA1.set_ylim(config['set_ylim'][0], config['set_ylim'][1])
377376
elif config['tight_layout']:
378377
axA1.set_xlim(minx,maxx)
379-
axA1.set_ylim(miny,maxy)
378+
ydelta = 0.01 * (maxy-miny)
379+
axA1.set_ylim(miny-ydelta,maxy+ydelta)
380380
else:
381381
corners = (minx, miny), (maxx, maxy)
382382
axA1.update_datalim(corners)
@@ -614,7 +614,13 @@ def plot( data, **kwargs ):
614614
volumeAxes.set_ylabel(vol_label)
615615

616616
if config['title'] is not None:
617-
fig.suptitle(config['title'],size='x-large',weight='semibold')
617+
if config['tight_layout']:
618+
# IMPORTANT: 0.89 is based on the top of the top panel
619+
# being at 0.18+0.7 = 0.88. See _panels.py
620+
# If the value changes there, then it needs to change here.
621+
fig.suptitle(config['title'],size='x-large',weight='semibold', va='bottom', y=0.89)
622+
else:
623+
fig.suptitle(config['title'],size='x-large',weight='semibold', va='center')
618624

619625
for panid,row in panels.iterrows():
620626
if not row['used2nd']:

0 commit comments

Comments
 (0)