简体   繁体   English

你如何在python的散景图例中更改标签的大小?

[英]How do you change size of labels in the Bokeh legend in python?

I have several curves (of different colours) plotted in the same figure and would like to widen the corresponding coloured lines in the legend so that they are easier to differentiate when projected onto a large screen.我在同一个图中绘制了几条曲线(不同颜色),并希望加宽图例中相应的彩色线条,以便在投影到大屏幕上时更容易区分。

I can access the properties of the legend fine, for example the legend's label text font size with:我可以很好地访问图例的属性,例如图例的标签文本字体大小:

p1.legend.label_text_font_size = "15pt"

where p1 is the figure in question.其中p1是有问题的数字。 The problem is I don't know what the term for the "coloured lines" in the legend is and sadly the relevant section in the docs is empty.问题是我不知道图例中“彩色线条”的术语是什么,遗憾的是文档中的相关部分是空的。

You can view the source code on GitHub你可以在GitHub上查看源代码

The only search hit for 'label_text_font_size' is in this file:- 'label_text_font_size'的唯一搜索结果在此文件中:-

 bokeh/bokehjs/src/coffee/renderer/annotation/legend.coffee

and scroll down to the Legend class (line 113 at the time of writing this) then you can see the class attributes.并向下滚动到Legend类(撰写本文时的第 113 行),然后您可以看到类属性。 Currently they are:-目前他们是:-

 display_defaults: -> return _.extend {}, super(), { level: 'overlay' border_line_color: 'black' border_line_width: 1 border_line_alpha: 1.0 border_line_join: 'miter' border_line_cap: 'butt' border_line_dash: [] border_line_dash_offset: 0 label_standoff: 15 label_text_font: "helvetica" label_text_font_size: "10pt" label_text_font_style: "normal" label_text_color: "#444444" label_text_alpha: 1.0 label_text_align: "left" label_text_baseline: "middle" glyph_height: 20 glyph_width: 20 label_height: 20 label_width: 50 legend_padding: 10 legend_spacing: 3 orientation: "top_right" datapoint: null }

.. none of them stand out as being the property that you want, so it might not be possible to change it but you might like to have a play? .. 它们都不是您想要的属性,因此可能无法更改它,但您可能想玩一玩?

NB I don't think all the properties have setters so you may have to set them using something like this: p.legend.__setattr__('label_text_color', "#FF0000")注意我不认为所有的属性都有设置器,所以你可能必须使用这样的东西来设置它们: p.legend.__setattr__('label_text_color', "#FF0000")

NB Bokeh is written in CoffeeScript which I have no experience of so this may all be useless. NB散景是写在CoffeeScript中,我有没有因此这可能是无用的经验。

The following (as proposed by @ciornav) works for me in bokeh 0.13.0:以下(由@ciornav 提出)适用于散景 0.13.0:

p.legend.label_text_font_size = '20pt'

as documented here .如此处所述

Be careful though where you put this code.请小心放置此代码的位置。 When it's eg between your p.figure() and p.line() statements, it will not take effect as it seems to get overridden.例如,当它位于 p.figure() 和 p.line() 语句之间时,它不会生效,因为它似乎被覆盖了。

Put the code after all the artifacts have been created, before p.show() or p.save() .将代码放在所有工件创建之后,在 p.show() 或 p.save() 之前

It's the glyph_height and glyph_width properties.这是glyph_heightglyph_width属性。 Tested this on a scatter plot, they're by default set to 20 and 20 respectively.在散点图上对此进行了测试,默认情况下它们分别设置为 20 和 20。 You can make them bigger when you build the legend item:您可以在构建图例项时使它们变大:

legend = Legend(...,
                glyph_height=30,
                glyph_width=30,
                ...)

Unfortunately, I tried setting it after the legend was already built and it doesn't seem to work...I could be wrong though.不幸的是,我尝试在图例构建后设置它,但它似乎不起作用......不过我可能是错的。

Edit: Also, I'm using Bokeh 0.12.7编辑:另外,我正在使用 Bokeh 0.12.7

In the current version of Bokeh(1.2) you can change the object size in the legend by using:在当前版本的 Bokeh(1.2) 中,您可以使用以下方法更改图例中的对象大小:

p.legend.glyph_height = #some int
p.legend.glyph_width = #some int
p.show()

Make sure that you change your legend properties after youve drawn all your geometries.确保在绘制所有几何图形后更改图例属性。

Maybe this could help:也许这可以帮助:

http://docs.bokeh.org/en/0.10.0/docs/user_guide/styling.html#id4 http://docs.bokeh.org/en/0.10.0/docs/user_guide/styling.html#id4

command that should work.... just adapt your size:应该工作的命令......只需调整你的尺寸:

p.legend.label_text_font_size = '30pt'

As of Bokeh 0.12.6, this is not possible.从 Bokeh 0.12.6 开始,这是不可能的。 Legends automatically use exactly the same visual properties (eg color, size width) of the thing that they are representing.图例自动使用与它们所代表的事物完全相同的视觉属性(例如颜色、大小宽度)。 So to make the line in the legend thicker, you would need to make the line itself thicker.因此,要使图例中的线条更粗,您需要使线条本身更粗。

Being able to override things might be a reasonable request, I'd urge you to make an issue on GitHub to discuss the feature proposal:能够覆盖内容可能是一个合理的要求,我敦促您在 GitHub 上提出问题以讨论功能提案:

https://github.com/bokeh/bokeh/issues https://github.com/bokeh/bokeh/issues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM