Luiz Suzuki
Luiz Suzuki

Reputation: 3

Bokeh charts with multiple font sizes in LabelSets

community,

I am building a TreeMap using squarify + bokeh. I would like to have different text_font_sizes for each label as the bigger squares have a lot of space for a small font and the small squares sometimes are not big enough for a medium font.

I already tried doing the following (I have 10 items in the chart):

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

if I use text_font_size='10pt' it works just fine, but with the array it doesn’t. I just used the same size for every element in the array to show that it does not work with an array.

Any clues on how to solve this?

Upvotes: 0

Views: 221

Answers (1)

Jasper
Jasper

Reputation: 1795

Using an array for text_font_size is indeed not possible. I've added an ugly workaround to my answer. You could create a feature request on the Github page if you want it to be added.

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)

enter image description here

Upvotes: 1

Related Questions