gpt4 book ai didi

python - 在 Bokeh 图中创建两线图例

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:14 26 4
gpt4 key购买 nike

我正在尝试创建一个 Bokeh 图,上面有很多单独的线条,准确地说是 14 条(不是我的主意,有人告诉我这样做)。问题是请求的格式在图表底部有水平的图例,它太长了,所以我需要它是两行,但我似乎找不到任何关于如何做到这一点的指示。希望以前有人必须解决这个问题。

带有单行图例的当前代码。

#graph data to chart
r0 = p1.line(data['Time'], data['Signal 1'], line_color="MediumBlue")
r1 = p1.line(data['Time'], data['Signal 2'], line_color="MidnightBlue")
r2 = p1.line(data['Time'], data['Signal 3'], line_color="red", y_range_name="Temp")
r3 = p1.line(data['Time'], data['Signal 4']*10, line_color="gray", y_range_name="Temp")
r4 = p1.line(data['Time'], data['Signal 5']*3.281, line_color="gold")
r5 = p1.line(data['Time'], data['Signal 6'], line_color="brown", y_range_name="Temp")
r6 = p1.line(data['Time'], data['Signal 7'], line_color="tomato", y_range_name="Temp")
r7 = p1.line(data['Time'], data['Signal 8'], line_color="blue", line_dash="4 4", y_range_name="Temp")
r8 = p1.line(data['Time'], data['Signal 9'], line_color="cyan", y_range_name="Temp")
r9 = p1.line(data['Time'], data['Signal 10']*57.3, line_color="plum", y_range_name="Temp")
r10 = p1.line(data['Time'], data['Signal 11']*1.94, line_color="green", y_range_name="Temp")

#create html file
output_file("Engine_Disp_Test.html", title="Engine Disp Test")

#Configure Legend
legend=Legend(items=[
("Signal 1" , [r0]),
("Signal 2" , [r1]),
("Signal 3" , [r2]),
("Signal 4" , [r3]),
("Signal 5", [r4]),
("Signal 6", [r5]),
("Signal 7", [r6]),
("Signal 8", [r7]),
("Signal 9", [r8]),
("Signal 10", [r9]),
("Signal 11", [r10])
], location=(70,-10), orientation="horizontal")

最佳答案

从 Bokeh 0.12.4 开始,没有任何东西可以让您自动将图例拆分为多行(或多列)。但是您可以通过添加两个图例来解决它。请注意,我调整了位置并添加了一个 min_border_bottom 值,因为底部图例似乎被切断了。

from bokeh.io import output_file, show
from bokeh.models import Legend
from bokeh.plotting import figure

p = figure(min_border_bottom=130)

r1 = p.line(x=[0, 1], y=1, line_width=2, color="red")
r2 = p.line(x=[0, 1], y=2, line_width=2, color="blue")
r3 = p.line(x=[0, 1], y=3, line_width=2, color="green")
r4 = p.line(x=[0, 1], y=4, line_width=2, color="orange")

legend1 = Legend([items=[("r1" , [r1]), ("r2", [r2])],
location=(70,20), orientation="horizontal")

legend2 = Legend(items=[("r3" , [r3]), ("r4", [r4])],
location=(70,10), orientation="horizontal")

p.add_layout(legend1, 'below')
p.add_layout(legend2, 'below')

output_file("foo.html")

show(p)

enter image description here

关于python - 在 Bokeh 图中创建两线图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41573881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com