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

Add explicit xlabel/ylabel(/zlabel) plot options #3154

Merged
merged 4 commits into from
Nov 8, 2018
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
2 changes: 1 addition & 1 deletion holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ class OverlayPlot(GenericOverlayPlot, LegendPlot):
'border', 'invert_xaxis', 'invert_yaxis', 'sizing_mode',
'title_format', 'legend_position', 'legend_offset',
'legend_cols', 'gridstyle', 'legend_muted', 'padding',
'xlim', 'ylim', 'zlim']
'xlabel', 'ylabel', 'xlim', 'ylim', 'zlim']

def __init__(self, overlay, **params):
super(OverlayPlot, self).__init__(overlay, **params)
Expand Down
13 changes: 12 additions & 1 deletion holoviews/plotting/mpl/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class ElementPlot(GenericElementPlot, MPLPlot):
zaxis = param.Boolean(default=True, doc="""
Whether to display the z-axis.""")

zlabel = param.String(default=None, doc="""
An explicit override of the z-axis label, if set takes precedence
over the dimension label.""")

zrotation = param.Integer(default=0, bounds=(0, 360), doc="""
Rotation angle of the zticks.""")

Expand Down Expand Up @@ -128,6 +132,13 @@ def _finalize_axis(self, key, element=None, title=None, dimensions=None, ranges=
# Set axis labels
if dimensions:
self._set_labels(axis, dimensions, xlabel, ylabel, zlabel)
else:
if self.xlabel is not None:
axis.set_xlabel(self.xlabel)
if self.ylabel is not None:
axis.set_ylabel(self.ylabel)
if self.zlabel is not None and hasattr(axis, 'set_zlabel'):
axis.set_zlabel(self.zlabel)

if not subplots:
legend = axis.get_legend()
Expand Down Expand Up @@ -780,7 +791,7 @@ class OverlayPlot(LegendPlot, GenericOverlayPlot):
'xticks', 'yticks', 'zticks', 'xrotation', 'yrotation',
'zrotation', 'invert_xaxis', 'invert_yaxis',
'invert_zaxis', 'title_format', 'padding',
'xlim', 'ylim', 'zlim']
'xlabel', 'ylabel', 'zlabel', 'xlim', 'ylim', 'zlim']

def __init__(self, overlay, ranges=None, **params):
if 'projection' not in params:
Expand Down
3 changes: 3 additions & 0 deletions holoviews/plotting/mpl/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class RasterGridPlot(GridPlot, OverlayPlot):
zaxis = param.Parameter(precedence=-1)
zrotation = param.Parameter(precedence=-1)
zformatter = param.Parameter(precedence=-1)
xlabel = param.Parameter(precedence=-1)
ylabel = param.Parameter(precedence=-1)
zlabel = param.Parameter(precedence=-1)


def __init__(self, layout, keys=None, dimensions=None, create_axes=False, ranges=None,
Expand Down
20 changes: 17 additions & 3 deletions holoviews/plotting/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,14 @@ class GenericElementPlot(DimensionedPlot):
The "bare" options allow suppressing all axis labels, including ticks and ylabel.
Valid options are 'left', 'right', 'bare', 'left-bare' and 'right-bare'.""")

xlabel = param.String(default=None, doc="""
An explicit override of the x-axis label, if set takes precedence
over the dimension label.""")

ylabel = param.String(default=None, doc="""
An explicit override of the y-axis label, if set takes precedence
over the dimension label.""")

xlim = param.NumericTuple(default=(np.nan, np.nan), length=2, doc="""
User-specified x-axis range limits for the plot, as a tuple (low,high).
If specified, takes precedence over data and dimension ranges.""")
Expand Down Expand Up @@ -967,11 +975,17 @@ def get_extents(self, element, ranges, range_type='combined', xdim=None, ydim=No


def _get_axis_labels(self, dimensions, xlabel=None, ylabel=None, zlabel=None):
if dimensions and xlabel is None:
if self.xlabel is not None:
xlabel = self.xlabel
elif dimensions and xlabel is None:
xlabel = dim_axis_label(dimensions[0]) if dimensions[0] else ''
if len(dimensions) >= 2 and ylabel is None:
if self.ylabel is not None:
ylabel = self.ylabel
elif len(dimensions) >= 2 and ylabel is None:
ylabel = dim_axis_label(dimensions[1]) if dimensions[1] else ''
if self.projection == '3d' and len(dimensions) >= 3 and zlabel is None:
if getattr(self, 'zlabel', None) is not None:
zlabel = self.zlabel
elif self.projection == '3d' and len(dimensions) >= 3 and zlabel is None:
zlabel = dim_axis_label(dimensions[2]) if dimensions[2] else ''
return xlabel, ylabel, zlabel

Expand Down
4 changes: 4 additions & 0 deletions holoviews/plotting/plotly/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class ElementPlot(PlotlyPlot, GenericElementPlot):
labels or a matplotlib tick locator object. If set to None
default matplotlib ticking behavior is applied.""")

zlabel = param.String(default=None, doc="""
An explicit override of the z-axis label, if set takes precedence
over the dimension label.""")

graph_obj = None

def initialize_plot(self, ranges=None):
Expand Down
10 changes: 10 additions & 0 deletions holoviews/tests/plotting/bokeh/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def test_element_yrotation(self):
plot = bokeh_renderer.get_plot(curve).state
self.assertEqual(plot.yaxis[0].major_label_orientation, np.pi/2)

def test_element_xlabel_override(self):
curve = Curve(range(10)).options(xlabel='custom x-label')
plot = bokeh_renderer.get_plot(curve).state
self.assertEqual(plot.xaxis[0].axis_label, 'custom x-label')

def test_element_ylabel_override(self):
curve = Curve(range(10)).options(ylabel='custom y-label')
plot = bokeh_renderer.get_plot(curve).state
self.assertEqual(plot.yaxis[0].axis_label, 'custom y-label')

def test_element_labelled_x_disabled(self):
curve = Curve(range(10)).options(labelled=['y'])
plot = bokeh_renderer.get_plot(curve).state
Expand Down
20 changes: 20 additions & 0 deletions holoviews/tests/plotting/bokeh/testoverlayplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ def test_overlay_no_yaxis(self):
plot = bokeh_renderer.get_plot(overlay).state
self.assertFalse(plot.yaxis[0].visible)

def test_overlay_xlabel_override(self):
overlay = (Curve(range(10)) * Curve(range(10))).options(xlabel='custom x-label')
plot = bokeh_renderer.get_plot(overlay).state
self.assertEqual(plot.xaxis[0].axis_label, 'custom x-label')

def test_overlay_ylabel_override(self):
overlay = (Curve(range(10)) * Curve(range(10))).options(ylabel='custom y-label')
plot = bokeh_renderer.get_plot(overlay).state
self.assertEqual(plot.yaxis[0].axis_label, 'custom y-label')

def test_overlay_xlabel_override_propagated(self):
overlay = (Curve(range(10)).options(xlabel='custom x-label') * Curve(range(10)))
plot = bokeh_renderer.get_plot(overlay).state
self.assertEqual(plot.xaxis[0].axis_label, 'custom x-label')

def test_overlay_ylabel_override_propagated(self):
overlay = (Curve(range(10)).options(ylabel='custom y-label') * Curve(range(10)))
plot = bokeh_renderer.get_plot(overlay).state
self.assertEqual(plot.yaxis[0].axis_label, 'custom y-label')

def test_overlay_xrotation(self):
overlay = (Curve(range(10)) * Curve(range(10))).opts(plot=dict(xrotation=90))
plot = bokeh_renderer.get_plot(overlay).state
Expand Down
10 changes: 10 additions & 0 deletions holoviews/tests/plotting/matplotlib/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def test_stream_cleanup(self):
plot.cleanup()
self.assertFalse(bool(stream._subscribers))

def test_element_xlabel(self):
element = Curve(range(10)).options(xlabel='custom x-label')
axes = mpl_renderer.get_plot(element).handles['axis']
self.assertEqual(axes.get_xlabel(), 'custom x-label')

def test_element_ylabel(self):
element = Curve(range(10)).options(ylabel='custom y-label')
axes = mpl_renderer.get_plot(element).handles['axis']
self.assertEqual(axes.get_ylabel(), 'custom y-label')

def test_element_xformatter_string(self):
curve = Curve(range(10)).options(xformatter='%d')
plot = mpl_renderer.get_plot(curve)
Expand Down
20 changes: 20 additions & 0 deletions holoviews/tests/plotting/matplotlib/testoverlayplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ def cb(X):
self.assertEqual(len(plot.subplots), 3)
for i, subplot in enumerate(plot.subplots.values()):
self.assertEqual(subplot.cyclic_index, i)

def test_overlay_xlabel(self):
overlay = (Curve(range(10)) * Curve(range(10))).options(xlabel='custom x-label')
axes = mpl_renderer.get_plot(overlay).handles['axis']
self.assertEqual(axes.get_xlabel(), 'custom x-label')

def test_overlay_ylabel(self):
overlay = (Curve(range(10)) * Curve(range(10))).options(ylabel='custom y-label')
axes = mpl_renderer.get_plot(overlay).handles['axis']
self.assertEqual(axes.get_ylabel(), 'custom y-label')

def test_overlay_xlabel_override_propagated(self):
overlay = (Curve(range(10)).options(xlabel='custom x-label') * Curve(range(10)))
axes = mpl_renderer.get_plot(overlay).handles['axis']
self.assertEqual(axes.get_xlabel(), 'custom x-label')

def test_overlay_ylabel_override(self):
overlay = (Curve(range(10)).options(ylabel='custom y-label') * Curve(range(10)))
axes = mpl_renderer.get_plot(overlay).handles['axis']
self.assertEqual(axes.get_ylabel(), 'custom y-label')