如何在holoviews + bokeh中获得带有图例标签的全高垂直线? [英] How do I get a full-height vertical line with a legend label in holoviews + bokeh?

查看:63
本文介绍了如何在holoviews + bokeh中获得带有图例标签的全高垂直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 bokeh 后端在 holoviews 中绘制一条垂直线,该后端的标签显示在我的图例中.无论这条线是单独放置还是被其他元素覆盖,我都需要将此线作为绘图的整个高度.我该如何实现?

I want to plot a vertical line in holoviews with the bokeh backend which has a label that shows up in my legend. I need this line to be the full height of the plot, regardless of whether it is alone or overlaid with other elements. How can I achieve this?

我在示例中添加了一个曲线图,因为否则,即使是可以出现在图例中的元素,也只会使用其标签作为标题.

I'm adding in a curve plot in the example because otherwise even elements that can appear in a legend just use their label as a title.

import numpy as np
import holoviews as hv
hv.extension("bokeh")

x = np.linspace(0, 1)
curve = hv.Curve((x, np.sin(x)), label="sin(x)")
vline = hv.VLine(0.5, label="vline")
curve * vline

这给出了以下情节:

,其中没有垂直线标签.如何显示标签?

which has no label for the vertical line. How do I get the label to show up?

推荐答案

此问题,但尚未在文档中显示, VLine HLine 不会出现在图例中,也没有计划添加对它们的支持(基本上在 bokeh ,它们的创建方式有所不同,因此没有简单的方法将它们放入图例中.可以改用 Spikes .但是,如另一个问题所述,峰值不能很好地叠加.特别是,如果没有给出明确的高度,他们不会将其高度调整为情节的整个高度.我提出了两种解决方法.

As mentioned in this issue but not yet in the docs, VLine and HLine don't appear in legends, and there is no plan to add support for them (basically, in bokeh they're created differently, so there's not an easy way to put them in the legend). One can use Spikes instead. However, as documented in another issue, spikes don't overlay well. In particular, they don't adjust their height to be the full height of the plot if given no explicit height. Here are two workarounds that I've come up with.

您可以显式找出垂直线应与之重叠的另一个元素的高度,并使用它来创建适当高度的尖峰.这行得通,但它相当脆弱,因为您需要在充分了解可能被峰值覆盖的所有方面进行调整.

You can explicitly find out the height of the other element that the vertical line should be overlaid with and use this to create a spike of the proper height. This works, but it's rather brittle because you need to adapt it with full knowledge of everything that could be overlaid with the spike.

import numpy as np
import holoviews as hv
hv.extension("bokeh")

x = np.linspace(0, 1)
curve = hv.Curve((x, np.sin(x)), label="sin(x)")
height = curve.data["y"].max() - curve.data["y"].min()
spikes = hv.Spikes(([0.5], [height]), vdims="height", label="mid")
spikes * curve

这同时使用了 VLine Spikes .尖峰将不可见,除非它将有助于添加图例.vline将位于尖峰的顶部,并且vline已经进行了自我调整以填充图形的整个高度.这需要创建一个额外的元素,但是它更加健壮,因为您可以将此峰值和vline的乘积与任何其他元素重叠,并且仍然获得一条填充图高度并显示在图例中的线.但是,由于图例条目基于尖峰,因此只有确保它们具有相似的外观(例如,vline和尖峰具有相同的颜色),它才会看起来像vline.

This uses both a VLine and a Spikes. The spike won't be visible except that it will contribute an entry to the legend. The vline will be on top of the spike, and vlines already adjust themselves to fill the whole height of the figure. This requires creating an extra element, but it is more robust because you can overlay the product of this spike and vline with any other elements and still get a line that fills the height of the plot and appears in the legend. As the legend entry is based on the spike, though, it will only look like the vline if you make sure that they have a similar appearance (e.g. the vline and the spike have the same color).

# need to make sure the colors are the same for spikes/vlines
# would look a bit better if I adjusted the spike thickness too
spikes = hv.Spikes([0.5], label="mid").opts(color="black")
vline = hv.VLine(0.5).opts(color="black")

spikes * curve * vline

将来, Spikes 会在未明确指定高度的情况下将自身缩放为全高,这样就不需要这些替代方法了.

In the future, Spikes will hopefully scale themselves to be full-height when not explicitly given a height, and then these workarounds won't be needed.

这篇关于如何在holoviews + bokeh中获得带有图例标签的全高垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆