2

Is there a way to change the alpha of the glyph in a Bokeh legend?

Take this example. I have the alpha of the lines in my plot set to 0.1, but the line glyph in the legend mirrors this alpha value also.

You can change the alpha of everything else in the legend (i.e border, text, background) but not the glyph?!

This is frustrating as it means I can't tell which colour is associated with which label (if the alpha is set low).

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)

It would be great to just be able to do something like p.legend.glyph_alpha = 0.9.

To clarify: In the actual use case this relates to, I can have upwards of 100,000 lines (i.e 30,000 from one label and 70,000 from another) and therefore I want to be able to set the alpha of the lines in the plot very low to look for overlapping trends etc.

Michael Hall
  • 2,834
  • 1
  • 22
  • 40

1 Answers1

0

I would argue that with an alpha that low you aren't even able to see the line. But anyways you shouldn't change the alpha in the legend because it actually makes subtle differences in the color. Similar to your example:

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)

enter image description here

Edit I couldn't find any documentation on how to do this, but if you have only 2 subgroups with 2 colors you could just plot an additional line with the same label outside of the domain you are looking at with the actual label. i.e. This is a crude way to fix your legend but it works.

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)

enter image description here

BenT
  • 3,172
  • 3
  • 18
  • 38
  • The actual real-world use case this question relates to I can have upwards of 100,000 lines - so it makes a big difference. This answer doesn't address the question I posed. – Michael Hall Aug 12 '17 at 06:06
  • Thanks for the additional info, I edited the response. I feel like a density contour plot would be a better way to analyze the data though. – BenT Aug 12 '17 at 14:35
  • Crude is fine if it works. Thanks for the recommendation on the density contour - I'll see how it works with my data. – Michael Hall Aug 13 '17 at 06:43