Skip to content

Commit 8d46383

Browse files
Merge pull request #564 from vedant-gawande/master
Added xlabel= kwarg to mpf.plot()
2 parents 4db565c + a05dd0d commit 8d46383

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

examples/plot_customizations.ipynb

+12-11
Large diffs are not rendered by default.

src/mplfinance/_panels.py

+8-1
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/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version_info = (0, 12, 9, 'beta', 3)
1+
version_info = (0, 12, 9, 'beta', 4)
22

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

src/mplfinance/plotting.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ def _valid_plot_kwargs():
194194
'axtitle' : { 'Default' : None, # Axes Title (subplot title)
195195
'Description' : 'Axes Title (subplot title)',
196196
'Validator' : lambda value: isinstance(value,(str,dict)) },
197+
198+
'xlabel' : { 'Default' : None, # x-axis label
199+
'Description' : 'label for x-axis of plot',
200+
'Validator' : lambda value: isinstance(value,str) },
197201

198202
'ylabel' : { 'Default' : 'Price', # y-axis label
199203
'Description' : 'label for y-axis of main plot',
@@ -675,10 +679,12 @@ def plot( data, **kwargs ):
675679

676680
xrotation = config['xrotation']
677681
if not external_axes_mode:
678-
_set_ticks_on_bottom_panel_only(panels,formatter,rotation=xrotation)
682+
_set_ticks_on_bottom_panel_only(panels,formatter,rotation=xrotation,
683+
xlabel=config['xlabel'])
679684
else:
680685
axA1.tick_params(axis='x',rotation=xrotation)
681686
axA1.xaxis.set_major_formatter(formatter)
687+
axA1.set_xlabel(config['xlabel'])
682688

683689
ysd = config['yscale']
684690
if isinstance(ysd,dict):
@@ -727,8 +733,8 @@ def plot( data, **kwargs ):
727733
elif panid == 'lower': panid = 1 # for backwards compatibility
728734
if apdict['y_on_right'] is not None:
729735
panels.at[panid,'y_on_right'] = apdict['y_on_right']
730-
731736
aptype = apdict['type']
737+
732738
if aptype == 'ohlc' or aptype == 'candle':
733739
ax = _addplot_collections(panid,panels,apdict,xdates,config)
734740
_addplot_apply_supplements(ax,apdict,xdates)
@@ -1096,6 +1102,11 @@ def _addplot_columns(panid,panels,ydata,apdict,xdates,config):
10961102
def _addplot_apply_supplements(ax,apdict,xdates):
10971103
if (apdict['ylabel'] is not None):
10981104
ax.set_ylabel(apdict['ylabel'])
1105+
# Note that xlabel is NOT supported for addplot. This is because
1106+
# in Panels Mode, there is only one xlabel (on the bottom axes)
1107+
# which is handled by the `xlabel` kwarg of `mpf.plot()`,
1108+
# whereas in External Axes Mode, users can call `Axes.set_xlabel()` on
1109+
# the axes object of their choice.
10991110
if apdict['ylim'] is not None:
11001111
ax.set_ylim(apdict['ylim'][0],apdict['ylim'][1])
11011112
if apdict['title'] is not None:

0 commit comments

Comments
 (0)