How to control Holoviews y-tick format?

2019-07-29 20:15发布

问题:

I am trying to gain control of the Holoviews Curve (though this should work for any other plotting element) and set the y-tick labels to be in scientific notations.

In MatPlotLib I would do this with ticklabel_format(style='sci', axis='y', scilimits=(0,0)).

Currently I have this:

But would like the Y axis numbers to be in scientific notations instead of the long decimal numbers currently shown on the Y axis.

Is there anyway to do this and what is it?

回答1:

HoloViews let's you supply a tick formatter directly on a Dimension, so you can do something like this:

from matplotlib.ticker import ScalarFormatter
yfmt = ScalarFormatter()
yfmt.set_powerlimits((0,0))
yfmt.set_scientific(True)
hv.Curve(np.random.rand(10), vdims=[hv.Dimension('y', value_format=yfmt)])

If you ever find options that you can't control directly you can always attach hooks on your Element, which will give you direct control over the plotting object, e.g.:

def sciticks(plot, element):
    plot.handles['axis'].ticklabel_format(style='sci', axis='y', scilimits=(0,0))

hv.Curve(np.random.rand(10,2))(plot=dict(final_hooks=[sciticks])) 

The plot argument to the callback function is the HoloViews plotting class, which will let you access various plotting objects on its handles attribute. The handles contain various important plot objects, for example the 'axis' keyword corresponds to the matplotlib Axes object, but among other things you can also access and modify the Figure using 'fig' and the Artist using 'artist'.