问题
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?
回答1:
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')
回答2:
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.
回答3:
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:
来源:https://stackoverflow.com/questions/40979365/setting-x-and-y-labels-with-holoviews