本文介绍了使用Holoviews在地块上添加长名称和单位的一种优雅方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在Python3和Jupyter笔记本上使用Holoviews,并且我正在寻找一种在我的绘图轴上放置长名称和单位的好方法.一个示例如下所示:

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'))

这给了我一个不错的图,但是我宁愿选择"Time(ns)"和"Rate(1/s)之类的东西,而不是x轴上的时间"和y轴上的速率". ',但我不想每次都在代码中键入该代码.我发现 PhilippJFR撰写的这篇博客文章,我需要哪种方法,但是他使用的DFrame()函数已过时,因此,如果可能的话,我想避免使用它.有什么想法吗?

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?

推荐答案

结果很容易做到,但是很难在文档中找到.您只需传递holoviews.Dimension而不是字符串作为kdims参数:

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')))

这篇关于使用Holoviews在地块上添加长名称和单位的一种优雅方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:16