简体   繁体   English

如何使用调色板holoviews中的十六进制设置线条颜色

[英]How to set line colors using hex from color palette holoviews

I want to specify manually the color of a line segment in holoviews, based on a third column. 我想基于第三列在holoviews中手动指定线段的颜色。

I'm aware of the hv.Path examples, however, this reduces the length of the line with 1 segment, which I don't want. 我知道hv.Path示例,但是,这减少了1段的行的长度,这是我不想要的。 I can do it using bokeh, or using matplotlib, but I can't get it right using holoviews 我可以使用bokeh或使用matplotlib来做到这一点,但使用holoviews却无法正确完成

def get_color(min_val, max_val, val, palette):
    return palette[(int((val-min_val)*((len(palette)-1)/(max_val-min_val))+.5))]
from bokeh.io import output_file, show
from bokeh.plotting import figure
y = [0,1,2,3,4,5]
x = [0]*len(y)
z = [1,2,3,4,5]
p = figure(plot_width=500, plot_height=200, tools='')
[p.line([x[i],x[i+1]],[y[i],y[i+1]],line_color = get_color(1,5,z,Viridis256), line_width=4) for i,z in enumerate(z) ]
show(p)
import numpy
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# The line format you curently have:
lines = [[(0, 1, 2, 3, 4), (4, 5, 6, 7, 8)],
         [(0, 1, 2, 3, 4), (0, 1, 2, 3, 4)],
         [(0, 1, 2, 3, 4), (8, 7, 6, 5, 4)],
         [(4, 5, 6, 7, 8), (0, 1, 2, 3, 4)]]

# Reformat it to what `LineCollection` expects:
lines = [zip(x, y) for x, y in lines]

z = np.array([0.1, 9.4, 3.8, 2.0])

fig, ax = plt.subplots()
lines = LineCollection(lines, array=z, cmap=plt.cm.rainbow, linewidths=5)
ax.add_collection(lines)
fig.colorbar(lines)

# Manually adding artists doesn't rescale the plot, so we need to autoscale
ax.autoscale()

plt.show()
from bokeh.io import output_file, show
from bokeh.plotting import figure
y = [0,1,2,3,4,5]
x = [0]*len(y)
z = [1,2,3,4,5]
p = figure(plot_width=500, plot_height=200, tools='')
[p.line([x[i],x[i+1]],[y[i],y[i+1]],line_color = get_color(1,5,z,Viridis256), line_width=4) for i,z in enumerate(z) ]
show(p)
from bokeh.palettes import Viridis256



curvlst = [hv.Curve([[x[i],y[i]],[x[i+1],y[i+1]]],line_color = get_color(1,5,z,Viridis256)) for i,z in enumerate(z) ]
hv.Overlay(curvlst)

WARNING:param.Curve26666: Setting non-parameter attribute line_color=#440154 using a mechanism intended only for parameters 警告:param.Curve26666:使用仅用于参数的机制来设置非参数属性line_color =#440154

You could use a so called dim transform by rewriting the function a little bit: 您可以通过稍微重写函数来使用所谓的暗淡变换:

def get_color(val, min_val, max_val, palette):
    return [palette[(int((val-min_val)*((len(palette)-1)/(max_val-min_val))+.5))]]

y = [0,1,2,3,4,5]
x = [0]*len(y)
z = [1,2,3,4,5]

hv.NdOverlay({z: hv.Curve(([x[i],x[i+1]], [y[i],y[i+1]])) for i, z in enumerate(z)}, kdims=['z']).opts(
    'Curve', color=hv.dim('z', get_color, 1, 5, Viridis256))

That being said, I don't think you should have to manually colormap Curves so I've opened: https://github.com/pyviz/holoviews/issues/3764 . 话虽如此,我认为您不必手动对“曲线”进行颜色映射,所以我已经打开了: https : //github.com/pyviz/holoviews/issues/3764

I think I found out.. 我想我发现了..

from bokeh.palettes import Viridis256
def get_color(min_val, max_val, val, palette):
    return palette[(int((val-min_val)*((len(palette)-1)/(max_val-min_val))+.5))]


curvlst = [hv.Curve([[x[i],y[i]],[x[i+1],y[i+1]]]).opts(color=get_color(1,5,z,Viridis256)) for i,z in enumerate(z) ]
hv.Overlay(curvlst)

Please let me know it this is good practise, or if you know a better way.. 请让我知道这是很好的做法,或者如果您知道更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM