Skip to content

Commit 49c5f7c

Browse files
Merge pull request #158 from DanielGoldfarb/master
Fix tight_layout (issue #156); also set axisbelow so grid not show through plot.
2 parents 25554d8 + ed7be21 commit 49c5f7c

File tree

4 files changed

+455
-8
lines changed

4 files changed

+455
-8
lines changed

examples/scratch_pad/issues/Issue156_title_ruins_tight_layout.ipynb

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

src/mplfinance/_panels.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,15 @@ def _build_panels( figure, config ):
156156
lift = panels['height'].loc[panid+1:].sum()
157157
panels.at[panid,'lift'] = lift
158158
if panid == 0:
159+
# rect = [left, bottom, width, height]
159160
ax0 = figure.add_axes( [0.15, 0.18+lift, 0.70, height] )
160161
else:
161162
ax0 = figure.add_axes( [0.15, 0.18+lift, 0.70, height], sharex=panels.at[0,'axes'][0] )
162163
ax1 = ax0.twinx()
163164
ax1.grid(False)
164-
if panid == volume_panel:
165+
if config['saxbelow']: # issue#115 issuecomment-639446764
166+
ax0.set_axisbelow(True) # so grid does not show through plot data on any panel.
167+
elif panid == volume_panel:
165168
ax0.set_axisbelow(True) # so grid does not show through volume bars.
166169
panels.at[panid,'axes'] = (ax0,ax1)
167170

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: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ def _valid_plot_kwargs():
223223

224224
'return_width_config' : { 'Default' : None,
225225
'Validator' : lambda value: isinstance(value,dict) and len(value)==0 },
226+
227+
'saxbelow' : { 'Default' : True, # Issue#115 Comment#639446764
228+
'Validator' : lambda value: isinstance(value,bool) },
226229
}
227230

228231
_validate_vkwargs_dict(vkwargs)
@@ -312,7 +315,6 @@ def plot( data, **kwargs ):
312315
formatter = IntegerIndexDateTimeFormatter(new_dates, fmtstring)
313316
xdates = np.arange(len(new_dates))
314317

315-
316318
if collections is not None:
317319
for collection in collections:
318320
axA1.add_collection(collection)
@@ -342,17 +344,17 @@ def plot( data, **kwargs ):
342344
else:
343345
axA1.plot(xdates, mavprices)
344346

347+
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
345348
if not config['tight_layout']:
346-
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
347349
#print('plot: xdates[-1]=',xdates[-1])
348350
#print('plot: xdates[ 0]=',xdates[ 0])
349351
#print('plot: len(xdates)=',len(xdates))
350352
#print('plot: avg_dist_between_points =',avg_dist_between_points)
351353
minx = xdates[0] - avg_dist_between_points
352354
maxx = xdates[-1] + avg_dist_between_points
353355
else:
354-
minx = xdates[0]
355-
maxx = xdates[-1]
356+
minx = xdates[0] - (0.45 * avg_dist_between_points)
357+
maxx = xdates[-1] + (0.45 * avg_dist_between_points)
356358

357359
if len(xdates) == 1: # kludge special case
358360
minx = minx - 0.75
@@ -376,7 +378,8 @@ def plot( data, **kwargs ):
376378
axA1.set_ylim(config['set_ylim'][0], config['set_ylim'][1])
377379
elif config['tight_layout']:
378380
axA1.set_xlim(minx,maxx)
379-
axA1.set_ylim(miny,maxy)
381+
ydelta = 0.01 * (maxy-miny)
382+
axA1.set_ylim(miny-ydelta,maxy+ydelta)
380383
else:
381384
corners = (minx, miny), (maxx, maxy)
382385
axA1.update_datalim(corners)
@@ -616,7 +619,13 @@ def plot( data, **kwargs ):
616619
volumeAxes.set_ylabel(vol_label)
617620

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

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

0 commit comments

Comments
 (0)