如何防止在bokeh中截断图例?

2024-04-27 08:52:04 发布

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

下面是一个简短的Python3脚本,它使用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[i] ]) for i in range(41)
    ]
)

plot.add_layout(legend, 'right')

bokeh.plotting.show(plot)

图例应该有40个条目,但是只有24个条目的空间,所以剩下的就扔掉了。我试着改变情节的界限

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

它确实增加了可用空间(第二个图(未显示)向下移动)--但图例没有扩展到可用空间


Tags: inmarginimportnumpyforplotmodelsline
1条回答
网友
1楼 · 发布于 2024-04-27 08:52:04

这是一个关于其他一些问题的问题,所以我以前见过。人们的想法是增加不止一个传说

输出

two legends will do

你的例子

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) ]

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

plot.add_layout(legend1, 'right')
plot.add_layout(legend2, 'right')
bokeh.plotting.show(plot)

现在我在网上找不到以前的帖子。但是在GitHub上有一个开放的特性请求,其中提到了这个解决方案

相关问题 更多 >