python - 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'
它就可以了,但是使用数组就不行了。我只是对数组中的每个元素使用相同的大小来表明它不适用于数组。
关于如何解决这个问题的任何线索?
解决方案
使用数组 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)
推荐阅读
- ldap - Keycloak 绑定到 LDAP 的最低权限
- rust - 我怎样才能实现`从
?` - node.js - AWS ElasticBeanstalk Amazon Linux 2 .platform 文件夹未复制 NGINX conf
- attributes - Asciidoctor:激活 CLI 属性中的替换
- python - 列表的 DataFrame 列和 NaN 到 String
- java - 无法在邮递员上使用 MTLS 服务,但它可以在 SOAP UI 中使用
- python - pytorch:如何在没有任何条目的情况下正确初始化张量?
- node.js - 特定于域的路由 - Nodejs
- django - 将具有特定属性的自定义用户添加到“朋友”列表
- python - 在 C++ 中创建的 python 对象超出范围?