python bokeh在同一行上显示标题和图例

2024-05-12 18:41:11 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能在博克的同一行上对齐标题和侧边图例

这是我从https://github.com/bokeh/bokeh/blob/2.3.0/examples/models/file/legends.py派生的下面代码得到的

标题位于图例上方

Bokeh title and legend not aligned

谢谢你的帮助

from numpy import cos, linspace, pi, sin

from bokeh.core.enums import LegendLocation
from bokeh.io import output_notebook, show
from bokeh.models import (Circle, ColumnDataSource, DataRange1d, Legend, Line,
                          LinearAxis, PanTool, Plot, SaveTool, WheelZoomTool,Title)
# from bokeh.models.annotations import Title
x = linspace(-2*pi, 2*pi, 400)
y = sin(x)
y2 = cos(x)

source = ColumnDataSource(data=dict(x=x, y=y, y2=y2))

xdr = DataRange1d()
ydr = DataRange1d()

plot = Plot(
    x_range=xdr, y_range=ydr,
    plot_width=1000, plot_height=600,
    min_border=0,
    toolbar_location=None,
    background_fill_color='#F0F0F0',
    border_fill_color='lightgray'
)

t = Title()
t.text = 'Main title'
t.level
plot.title = t

line_glyph = Line(x="x", y="y", line_color="navy", line_width=2, line_dash="dashed")
line = plot.add_glyph(source, line_glyph)
circle = Circle(x="x", y="y2", size=6, line_color="red", fill_color="orange", fill_alpha=0.6)
circle = plot.add_glyph(source, circle)

plot.add_layout(LinearAxis(), 'below')
plot.add_layout(LinearAxis(), 'left')

legend = Legend(
        items=[("line", [line]), ("circle", [circle])],
        location="top_right", orientation="horizontal",
        border_line_color="black",
        title='Example Title'
        )

plot.add_layout(legend, "above")

output_notebook()
show(plot)

Tags: fromimportaddplottitlemodelslinebokeh