[Python] How to prevent legend being truncated in bokeh?

Discussão em 'Python' iniciado por Stack, Julho 19, 2025.

  1. Stack

    Stack Membro Participativo

    Here's a short Python3 script that draws a bunch of sine waves with bokeh (2.2.1).

    import numpy
    import bokeh.models
    import bokeh.plotting

    x = numpy.linspace(0, 4*numpy.pi, 100)
    sinx = numpy.sin(x)

    bokeh.plotting.output_file("truncatedlegend.html")

    plot = bokeh.plotting.figure(toolbar_location="above")

    glyphs = [ plot.line(x, (1 + i/20)*sinx, line_width=2) for i in range(41) ]

    legend = bokeh.models.Legend(
    items=[
    ("%.2f*sin(x)" % ((1 + i/20)), [ glyphs ]) for i in range(41)
    ]
    )

    plot.add_layout(legend, 'right')

    bokeh.plotting.show(plot)


    The legend should have 40 entries, but there's only space for 24 of them, so the rest are thrown away. I tried changing the margin of the plot,

    margin = plot.margin
    plot.margin = (margin[0], margin[1], margin[2] + 600, margin[3])


    and it did increase the available space (in that a second plot (not shown), moves down)---but the legend doesn't expand into the available space.

    Continue reading...

Compartilhe esta Página