首页 > 解决方案 > LabelSets 中具有多种字体大小的散景图

问题描述

社区,

我正在使用 squarify + bokeh 构建 TreeMap。我想为每个标签设置不同的 text_font_sizes,因为较大的正方形有很多空间可以容纳小字体,而小正方形有时不足以容纳中等字体。

我已经尝试过执行以下操作(图表中有 10 个项目):

plotsource = ColumnDataSource(
    data=dict(
        Xlab = Xlab,
        Ylab = Ylab,

        Share = Share,
        Colors = source.data['Colors'],
        LabelColors = source.data['LabelColors'],
        Labels = source.data['Labels'],
        FontSizes = ['10pt']*10,

    )
)

…

labels1 = LabelSet(x='Xlab', y='Ylab', text='Labels', level='glyph',
    text_font_style='bold', text_color='LabelColors', text_align = 'center', source=plotsource, text_font_size='FontSizes')

如果我使用text_font_size='10pt'它就可以了,但是使用数组就不行了。我只是对数组中的每个元素使用相同的大小来表明它不适用于数组。

关于如何解决这个问题的任何线索?

标签: pythonbokeh

解决方案


使用数组 fortext_font_size确实是不可能的。我在我的答案中添加了一个丑陋的解决方法。如果你想添加它,你可以在Github 页面上创建一个功能请求。

from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource, Range1d, LabelSet, Label

output_file("label.html", title="label.py example")

plotsource = ColumnDataSource(data=dict(Xlab=[66, 71, 72, 68, 58, 62],
                                    Ylab=[165, 189, 220, 141, 260, 174],
                                    Labels=['Mark', 'Amir', 'Matt', 'Greg', 'Owen', 'Juan'],
                                    LabelColors=['red', 'green', 'blue', 'purple', 'gold', 'pink'],
                                    FontSizes=['12pt', '14pt', '16pt', '18pt', '20pt', '22pt']))

p = figure(title='Dist. of 10th Grade Students at Lee High')
p.scatter(x='Xlab', y='Ylab', size=8, source=plotsource)
p.xaxis[0].axis_label = 'Weight (lbs)'
p.yaxis[0].axis_label = 'Height (in)'

labels1 = []

for x, y, label, color, fontsize in zip(plotsource.data['Xlab'], plotsource.data['Ylab'], plotsource.data['Labels'], plotsource.data['LabelColors'], plotsource.data['FontSizes']):
    labels1.append(Label(x=x, y=y, text=label, level='glyph', text_font_style='bold', text_color=color, text_align ='center', text_font_size=fontsize))
    p.add_layout(labels1[-1])

show(p)

在此处输入图像描述


推荐阅读