如何使用调色板全息视图中的十六进制设置线条颜色
How to set line colors using hex from color palette holoviews
我想根据第三列手动指定全息视图中线段的颜色。
我知道 hv.Path 示例,但是,这减少了 1 段的行的长度,这是我不想要的。
我可以使用散景或使用 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)
警告:param.Curve26666:设置非参数属性line_color=#440154 使用仅用于参数的机制
我想我发现了..
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)
请让我知道这是一个很好的做法,或者如果您知道更好的方法..
您可以通过稍微重写函数来使用所谓的暗淡变换:
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))
话虽如此,我认为您不必手动对曲线进行颜色映射,所以我打开了:https://github.com/pyviz/holoviews/issues/3764。
我想根据第三列手动指定全息视图中线段的颜色。
我知道 hv.Path 示例,但是,这减少了 1 段的行的长度,这是我不想要的。 我可以使用散景或使用 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)
警告:param.Curve26666:设置非参数属性line_color=#440154 使用仅用于参数的机制
我想我发现了..
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)
请让我知道这是一个很好的做法,或者如果您知道更好的方法..
您可以通过稍微重写函数来使用所谓的暗淡变换:
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))
话虽如此,我认为您不必手动对曲线进行颜色映射,所以我打开了:https://github.com/pyviz/holoviews/issues/3764。