Skip to content

Added xlabel= kwarg to mpf.plot() #564

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

Merged
merged 6 commits into from
Oct 31, 2022
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
23 changes: 12 additions & 11 deletions examples/plot_customizations.ipynb

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/mplfinance/_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,22 @@ def _build_panels( figure, config ):
return panels


def _set_ticks_on_bottom_panel_only(panels,formatter,rotation=45):
def _set_ticks_on_bottom_panel_only(panels,formatter,rotation=45,xlabel=None):

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

if xlabel is not None:
ax.set_xlabel(xlabel)

if len(panels) == 1: return

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

2 changes: 1 addition & 1 deletion src/mplfinance/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version_info = (0, 12, 9, 'beta', 3)
version_info = (0, 12, 9, 'beta', 4)

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

Expand Down
15 changes: 13 additions & 2 deletions src/mplfinance/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ def _valid_plot_kwargs():
'axtitle' : { 'Default' : None, # Axes Title (subplot title)
'Description' : 'Axes Title (subplot title)',
'Validator' : lambda value: isinstance(value,(str,dict)) },

'xlabel' : { 'Default' : None, # x-axis label
'Description' : 'label for x-axis of plot',
'Validator' : lambda value: isinstance(value,str) },

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

xrotation = config['xrotation']
if not external_axes_mode:
_set_ticks_on_bottom_panel_only(panels,formatter,rotation=xrotation)
_set_ticks_on_bottom_panel_only(panels,formatter,rotation=xrotation,
xlabel=config['xlabel'])
else:
axA1.tick_params(axis='x',rotation=xrotation)
axA1.xaxis.set_major_formatter(formatter)
axA1.set_xlabel(config['xlabel'])

ysd = config['yscale']
if isinstance(ysd,dict):
Expand Down Expand Up @@ -727,8 +733,8 @@ def plot( data, **kwargs ):
elif panid == 'lower': panid = 1 # for backwards compatibility
if apdict['y_on_right'] is not None:
panels.at[panid,'y_on_right'] = apdict['y_on_right']

aptype = apdict['type']

if aptype == 'ohlc' or aptype == 'candle':
ax = _addplot_collections(panid,panels,apdict,xdates,config)
_addplot_apply_supplements(ax,apdict,xdates)
Expand Down Expand Up @@ -1096,6 +1102,11 @@ def _addplot_columns(panid,panels,ydata,apdict,xdates,config):
def _addplot_apply_supplements(ax,apdict,xdates):
if (apdict['ylabel'] is not None):
ax.set_ylabel(apdict['ylabel'])
# Note that xlabel is NOT supported for addplot. This is because
# in Panels Mode, there is only one xlabel (on the bottom axes)
# which is handled by the `xlabel` kwarg of `mpf.plot()`,
# whereas in External Axes Mode, users can call `Axes.set_xlabel()` on
# the axes object of their choice.
if apdict['ylim'] is not None:
ax.set_ylim(apdict['ylim'][0],apdict['ylim'][1])
if apdict['title'] is not None:
Expand Down