9

I would like to change the labels on the x and y axis on a figure from holoviews to be something other than the internal variable name. It seems the typical way to affect the axis labels is to change the variable names themselves to the label. This is rather inconvenient if you want complex labels, especially if you are frequently converting from other complex data objects like pandas dataframes.

Is there a general way to either: (A) change x and y labels of a figure as or after you plot it or (B) set up an human readable alias for variable names?

Caleb
  • 3,839
  • 7
  • 26
  • 35

3 Answers3

8

You can change the axis labels as or after you plot a figure like this for example

hv.Image(np.random.rand(10,10), kdims=['x','y']).redim.label(x='neXt', y='Ys')

EDIT: In earlier versions of HoloViews you are able to change the axis labels easily like this, check the second answer on Holoviews FAQ

curve = hv.Curve(df, 'x_col', 'y_col')
curve = curve.options(xlabel='X Label', ylabel='Label for Y')
4

There are indeed dimension aliases in HoloViews, although we should document them better. There are two ways of defining them. You can either supply a tuple of the form (name, label) as a dimension or explicitly declare an Aliases object and supply the attribute. Here is a simple example:

aliases = hv.util.Aliases(x='Some long label')
hv.Image(np.random.rand(10,10), kdims=[aliases.x, ('y', 'Inline label')])

The plotting code will use the long label, and you'll be able to refer to either the name or the label when using the object's methods. You can also supply a tuple to a dimension directly: hv.Dimension(('name', 'label'), range=(0,10)) if you also want to define a range or other Dimension parameter.

philippjfr
  • 3,997
  • 14
  • 15
  • Okay, thanks! Is there any way to broadcast aliases like to multiple objects, for example, to all items in a Holomap or Layout? It would save a lot of typing when you are building complex figures. – Caleb Dec 06 '16 at 17:51
  • I took away the answered flag, at least for now, since this doesn't seem to work well when I convert to holoview types from pandas DataFrames. – Caleb Dec 06 '16 at 21:00
  • You're right this doesn't seem to be working well at all for dataframes. I've opened a corresponding issue, which I hope to address very soon: https://github.com/ioam/holoviews/issues/1001 – philippjfr Dec 07 '16 at 13:19
  • Thanks. Yes, you described the problem I was having well in the issue you opened. Some rough edges like this, but holoviews seems very nice. I look forward to seeing how it develops. – Caleb Dec 07 '16 at 16:09
  • I'll go ahead and re-select this as the correct answer, since it is the canonical way to do it, and it should eventually work in correctly for DataFrames as well. – Caleb Dec 07 '16 at 16:45
0

You can change x and y labels by providing a tuple of the column name and the longer label you would like to be displayed:

import numpy as np
import pandas as pd
import holoviews as hv
hv.extension()

data = np.random.normal(size=[50, 2])
df = pd.DataFrame(data, columns=['col1', 'col2'])

hv.Points(
    df, 
    kdims=[('col1', 'long label of col1'), ('col2', 'long label of col2')]
)

Other alternatives are in this question:
An elegant way to add long names and units to plots with Holoviews

This results in the following plot:
Holoviews plot with custom x and y label

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96