【问题标题】:How to change alpha of legend glyph in Bokeh如何在散景中更改图例字形的 alpha
【发布时间】:2017-08-12 05:16:30
【问题描述】:

有没有办法改变散景图例中字形的 alpha 值?

举个例子。我将绘图中线条的 alpha 设置为 0.1,但图例中的线条字形也反映了这个 alpha 值。

您可以更改图例中其他所有内容(即边框、文本、背景)的 alpha,但不能更改字形?!

这令人沮丧,因为这意味着我无法分辨哪种颜色与哪个标签相关联(如果 alpha 设置较低)。

import numpy as np
from bokeh.plotting import output_file, figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
alpha = .1
output_file("legend_background.html")
p = figure()
p.line(x, y, legend="sin(x)", line_alpha=alpha)
p.line(x, 2*y, legend="2*sin(x)",
       line_dash=[4, 4], line_color="orange", line_width=2, line_alpha=alpha)
p.line(x, 3*y, legend="3*sin(x)", line_color="green", line_alpha=alpha)
p.legend.location = "top_right"
show(p)

如果能做类似p.legend.glyph_alpha = 0.9 这样的事情就好了。

澄清:在与此相关的实际用例中,我可以有超过 100,000 行(即来自一个标签的 30,000 行和来自另一个标签的 70,000 行),因此我希望能够设置图中线条的 alpha 非常低,以寻找重叠趋势等。

【问题讨论】:

    标签: python bokeh legend-properties


    【解决方案1】:

    我会争辩说,如果 alpha 这么低,你甚至看不到这条线。但无论如何,您不应该更改图例中的 alpha,因为它实际上会在颜色上产生细微的差异。与您的示例类似:

    import numpy as np
    from bokeh.plotting import output_file, figure, show
    x = np.linspace(0, 4*np.pi, 100)
    y = np.sin(x)
    
    output_file("legend_background.html")
    p = figure()
    p.line(x, y, legend="sin(x)", line_color='black',line_alpha=.2)
    p.line(x, 2*y, legend="2*sin(x)",line_dash=[4, 4], line_color="black",line_width=2, line_alpha=.7)
    p.line(x, 3*y, legend="3*sin(x)", line_color="black", line_alpha=1)
    show(p)
    

    编辑 我找不到有关如何执行此操作的任何文档,但是如果您只有 2 个具有 2 种颜色的子组,您可以在您正在查看的域之外使用实际标签绘制一条具有相同标签的附加线。即,这是修复传奇的粗略方法,但它有效。

    p.line(x, y, line_color='black',line_alpha=.1)
    p.line(x, 2*y,line_dash=[4, 4], line_color="orange", line_width=2,line_alpha=.1)
    p.line(x, 3*y, line_color="green", line_alpha=.1)
    p.line(x, y+1000, legend="sin(x)", line_color='black',line_alpha=1)
    p.line(x, 2*y+1000, legend="2*sin(x)",line_dash=[4, 4], line_color="orange", 
    line_width=2, line_alpha=1)
    p.line(x, 3*y+1000, legend="3*sin(x)", line_color="green", line_alpha=1)
    

    【讨论】:

    • 这个问题涉及的实际用例我可以有超过 100,000 行 - 所以它有很大的不同。这个答案没有解决我提出的问题。
    • 感谢您提供更多信息,我编辑了回复。我觉得密度等值线图是分析数据的更好方法。
    • 如果可行,原油就可以了。感谢您对密度等值线的建议 - 我会看看它如何处理我的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多