Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tight_layout support adjusting Figure borders (padding) around plot #196

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
817 changes: 817 additions & 0 deletions examples/scratch_pad/dev_scale_padding.ipynb

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions examples/scratch_pad/issues/issue193.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pandas as pd
import mplfinance as mpf

print('\nmpf.__version__=',mpf.__version__,'\n')

filename = '../../data/SP500_NOV2019_IDay.csv'

intraday = pd.read_csv(filename,index_col=0,parse_dates=True)
intraday = intraday.drop('Volume',axis=1) # Volume is zero anyway for this intraday data set
intraday.index.name = 'Date'
intraday.shape
intraday.head(3)
intraday.tail(3)

iday = intraday.loc['2019-11-06 15:00':'2019-11-06 16:00',:]

print('len(iday)=',len(iday))

#----------------------------------------------------------------
mpf.plot(iday,type='candle')

print('\n\nscale_padding=0.65')
mpf.plot(iday,type='candle',scale_padding=0.65)
#----------------------------------------------------------------

print('\n\ntight_layout=True')
mpf.plot(iday,type='candle',tight_layout=True,block=False,title='Tight Layout')

print('\n\ntight_layout, and scale_padding=0.85')
mpf.plot(iday,type='candle',tight_layout=True,scale_padding=0.85,title='Tight/Scale=0.85')

#----------------------------------------------------------------
print('\n\nscale_padding=2.5')
mpf.plot(iday,type='candle',scale_padding=2.5)

print('\n\ntight_layout, and scale_padding=0.25')
mpf.plot(iday,type='candle',tight_layout=True,scale_padding=0.25)
#----------------------------------------------------------------

# filename='../../data/SP500_NOV2019_Hist.csv'
# df = pd.read_csv(filename,index_col=0,parse_dates=True)
# df.index.name = 'Date'
# df.shape
# df.head(3)
# df.tail(3)

# print('len(df)=',len(df))

# mpf.plot(df,type='candle')
15 changes: 15 additions & 0 deletions src/mplfinance/_arg_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,18 @@ def _process_kwargs(kwargs, vkwargs):

def _valid_panel_id(panid):
return panid in ['main','lower'] or (isinstance(panid,int) and panid >= 0 and panid < 10)

def _scale_padding_validator(value):
if isinstance(value,(int,float)):
return True
elif isinstance(value,dict):
valid_keys=('left','right','top','bottom')
for key in value:
if key not in valid_keys:
raise ValueError('Invalid key "'+str(key)+'" found in `scale_padding` dict.')
if not isinstance(value[key],(int,float)):
raise ValueError('`scale_padding` dict contains non-number at key "'+str(key)+'"')
return True
else:
raise ValueError('`scale_padding` kwarg must be a number, or dict of (left,right,top,bottom) numbers.')
return False
46 changes: 43 additions & 3 deletions src/mplfinance/_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,50 @@ def _build_panels( figure, config ):

#print('panels=')
#print(panels)

left_pad = 0.18
right_pad = 0.10
top_pad = 0.12
bot_pad = 0.18

scale_left = scale_right = scale_top = scale_bot = 1.0

scale_padding = config['scale_padding']
if isinstance(scale_padding,dict):
if 'left' in scale_padding: scale_left = scale_padding['left']
if 'right' in scale_padding: scale_right = scale_padding['right']
if 'top' in scale_padding: scale_top = scale_padding['top']
if 'bottom' in scale_padding: scale_bot = scale_padding['bottom']
else: # isinstance(scale_padding,(int,float):
scale_left = scale_right = scale_top = scale_bot = scale_padding

if config['tight_layout']:
right_pad *= 0.4
top_pad *= 0.4
scale_left *= 0.6
scale_right *= 0.6
scale_top *= 0.6
scale_bot *= 0.6

left_pad *= scale_left
right_pad *= scale_right
top_pad *= scale_top
bot_pad *= scale_bot

plot_height = 1.0 - (bot_pad + top_pad )
plot_width = 1.0 - (left_pad + right_pad)

# print('scale_padding=',scale_padding)
# print('left_pad =',left_pad)
# print('right_pad=',right_pad)
# print('top_pad =',top_pad)
# print('bot_pad =',bot_pad)
# print('plot_height =',plot_height)
# print('plot_width =',plot_width)

psum = sum(pratios)
for panid,size in enumerate(pratios):
panels.at[panid,'height'] = 0.7 * size / psum
panels.at[panid,'height'] = plot_height * size / psum

# Now create the Axes:

Expand All @@ -157,9 +197,9 @@ def _build_panels( figure, config ):
panels.at[panid,'lift'] = lift
if panid == 0:
# rect = [left, bottom, width, height]
ax0 = figure.add_axes( [0.15, 0.18+lift, 0.70, height] )
ax0 = figure.add_axes( [left_pad, bot_pad+lift, plot_width, height] )
else:
ax0 = figure.add_axes( [0.15, 0.18+lift, 0.70, height], sharex=panels.at[0,'axes'][0] )
ax0 = figure.add_axes( [left_pad, bot_pad+lift, plot_width, height], sharex=panels.at[0,'axes'][0] )
ax1 = ax0.twinx()
ax1.grid(False)
if config['saxbelow']: # issue#115 issuecomment-639446764
Expand Down
2 changes: 1 addition & 1 deletion src/mplfinance/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

version_info = (0, 12, 6, 'alpha', 1)
version_info = (0, 12, 6, 'alpha', 2)

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

Expand Down
4 changes: 4 additions & 0 deletions src/mplfinance/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from mplfinance._arg_validators import _kwarg_not_implemented, _bypass_kwarg_validation
from mplfinance._arg_validators import _hlines_validator, _vlines_validator
from mplfinance._arg_validators import _alines_validator, _tlines_validator
from mplfinance._arg_validators import _scale_padding_validator
from mplfinance._arg_validators import _valid_panel_id

from mplfinance._panels import _build_panels
Expand Down Expand Up @@ -237,6 +238,9 @@ def _valid_plot_kwargs():

'saxbelow' : { 'Default' : True, # Issue#115 Comment#639446764
'Validator' : lambda value: isinstance(value,bool) },

'scale_padding' : { 'Default' : 1.0, # Issue#193
'Validator' : lambda value: _scale_padding_validator(value) },
}

_validate_vkwargs_dict(vkwargs)
Expand Down
Binary file modified tests/reference_images/addplot01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/addplot10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/alines01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/hlines01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/hlines02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/pnf01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/pnf02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/pnf03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/pnf04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/renko01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/renko02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/renko03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/renko04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/vlines01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/vlines02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/reference_images/vlines03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.