Skip to content

Commit 67e3388

Browse files
fix xlabel functionality
1 parent 4be4818 commit 67e3388

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/mplfinance/_panels.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,22 @@ def _build_panels( figure, config ):
218218
return panels
219219

220220

221-
def _set_ticks_on_bottom_panel_only(panels,formatter,rotation=45):
221+
def _set_ticks_on_bottom_panel_only(panels,formatter,rotation=45,xlabel=None):
222222

223223
bot = panels.index.values[-1]
224224
ax = panels.at[bot,'axes'][0]
225225
ax.tick_params(axis='x',rotation=rotation)
226226
ax.xaxis.set_major_formatter(formatter)
227227

228+
if xlabel is not None:
229+
ax.set_xlabel(xlabel)
230+
228231
if len(panels) == 1: return
229232

233+
# [::-1] reverses the order of the panel id's
234+
# [1:] all but the first element, which, since the array
235+
# is reversed, means we take all but the LAST panel id.
236+
# Thus, only the last (bottom) panel id gets tick labels:
230237
for panid in panels.index.values[::-1][1:]:
231238
panels.at[panid,'axes'][0].tick_params(axis='x',labelbottom=False)
232239

src/mplfinance/plotting.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ def _valid_plot_kwargs():
185185
'Description' : 'Axes Title (subplot title)',
186186
'Validator' : lambda value: isinstance(value,(str,dict)) },
187187

188-
'xlabel' : { 'Default' : 'Date', # x-axis label
189-
'Description' : 'label for x-axis of main plot',
188+
'xlabel' : { 'Default' : None, # x-axis label
189+
'Description' : 'label for x-axis of plot',
190190
'Validator' : lambda value: isinstance(value,str) },
191191

192192
'ylabel' : { 'Default' : 'Price', # y-axis label
@@ -653,10 +653,12 @@ def plot( data, **kwargs ):
653653

654654
xrotation = config['xrotation']
655655
if not external_axes_mode:
656-
_set_ticks_on_bottom_panel_only(panels,formatter,rotation=xrotation)
656+
_set_ticks_on_bottom_panel_only(panels,formatter,rotation=xrotation,
657+
xlabel=config['xlabel'])
657658
else:
658659
axA1.tick_params(axis='x',rotation=xrotation)
659660
axA1.xaxis.set_major_formatter(formatter)
661+
axA1.set_xlabel(config['xlabel'])
660662

661663
ysd = config['yscale']
662664
if isinstance(ysd,dict):
@@ -705,7 +707,13 @@ def plot( data, **kwargs ):
705707
elif panid == 'lower': panid = 1 # for backwards compatibility
706708
if apdict['y_on_right'] is not None:
707709
panels.at[panid,'y_on_right'] = apdict['y_on_right']
708-
710+
if apdict['xlabel'] is not None:
711+
apdict['xlabel'] = None # set to None so `_addplot_apply_supplements()` won't apply it.
712+
warnings.warn('\n\n ================================================================= '+
713+
'\n\n WARNING: make_addplot `xlabel` IGNORED in Panels mode.'+
714+
'\n Use `mpf.plot(...,xlabel=)` instead.'+
715+
'\n\n ================================================================ ',
716+
category=UserWarning)
709717
aptype = apdict['type']
710718
if aptype == 'ohlc' or aptype == 'candle':
711719
ax = _addplot_collections(panid,panels,apdict,xdates,config)
@@ -783,7 +791,6 @@ def plot( data, **kwargs ):
783791
# working in `addplot`).
784792

785793
axA1.set_ylabel(config['ylabel'])
786-
axA1.set_xlabel(config['xlabel'])
787794

788795
if config['volume']:
789796
if external_axes_mode:
@@ -1250,6 +1257,10 @@ def _valid_addplot_kwargs():
12501257
'Description' : 'label for y-axis (for this addplot)',
12511258
'Validator' : lambda value: isinstance(value,str) },
12521259

1260+
'xlabel' : { 'Default' : None, # x-axis label
1261+
'Description' : 'x-axis label (for addplot ONLY when in external axes mode)',
1262+
'Validator' : lambda value: isinstance(value,str) },
1263+
12531264
'ylim' : {'Default' : None,
12541265
'Description' : 'Limits for addplot y-axis as tuple (min,max), i.e. (bottom,top)',
12551266
'Validator' : lambda value: isinstance(value, (list,tuple)) and len(value) == 2

0 commit comments

Comments
 (0)