An elegant way to add long names and units to plots with Holoviews

时间秒杀一切 提交于 2020-02-23 04:17:25

问题


I've started to use Holoviews with Python3 and Jupyter notebooks, and I'm looking for a good way to put long names and units on my plot axis. An example looks like this:

import holoviews as hv
import pandas as pd
from IPython.display import display
hv.notebook_extension()

dataframe = pd.DataFrame({"time": [0, 1, 2, 3],
                          "photons": [10, 30, 20, 15],
                          "norm_photons": [0.33, 1, 0.67, 0.5],
                          "rate": [1, 3, 2, 1.5]}, index=[0, 1, 2, 3])
hvdata = hv.Table(dataframe, kdims=["time"])
display(hvdata.to.curve(vdims='rate'))

This gives me a nice plot, but instead of 'time' on the x-axis and 'rate' on the y-axis, I would prefer something like 'Time (ns)' and 'Rate (1/s)', but I don't want to type that in the code every time. I've found this blog post by PhilippJFR which kind of does what I need, but the DFrame() function which he uses is depreciated, so I would like to avoid using that, if possible. Any ideas?


回答1:


You can find good alternatives in this SO question:
Setting x and y labels with holoviews

I like doing it like this:
Creating a tuple with the name of the variable and the long name you would like to see printed on the plot:

hvdata = hv.Table(
    dataframe,
    kdims=[('time', 'Time (ns)')],
    vdims=[('rate', 'Rate (1/s)')],
)



回答2:


Turns out it's easy to do but hard to find in the documentation. You just pass a holoviews.Dimension instead of a string as the kdims parameter:

hvdata = hv.Table(dataframe, kdims=[hv.Dimension('time', label='Time', unit='ns')])
display(hvdata.to.curve(vdims=hv.Dimension('rate', label='Rate', unit='1/s')))


来源:https://stackoverflow.com/questions/41727084/an-elegant-way-to-add-long-names-and-units-to-plots-with-holoviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!