如何解决在Bokeh Python中,如果x和y轴包含字母,LabelSet批注将无法正常工作
也许标题不清楚,所以让我解释一下我的问题。我尝试了在x和y轴上使用所有数字的LabelSet,它可以完美工作。但是,当我更改为使用所有字母(例如A,A +,B等)时,LabelSet无法理解并将所有注释放在左上角(我在下面的图片中附加了有关错误的信息。)
我尝试调试,发现问题出在标签= LabelSet(x ='average_grades',y ='exam_grades')。如果我将x =整数更改为y =整数,则可以使用。请帮助我修复它,因为我想在很多情况下都应用LabelSet,而不仅仅是数字。
#importing libraries
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models.annotations import LabelSet,Label
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Select
from bokeh.layouts import layout
#create columndatasource
source=ColumnDataSource(dict(average_grades=["B+","A","D-"],exam_grades=["A+","C","D"],student_names=["Stephan","Helder","Riazudidn"]))
#create the figure
grade1 = ["F","D-","D","D+","C-","C+","B-","B","B+","A-","A+"]
grade2 = ["F","A+"]
f = figure(x_range= grade1,y_range= grade2)
f.plot_width = 1100
f.plot_height = 650
#add labels for glyphs
labels=LabelSet(x='average_grades',y='exam_grades',text="student_names",x_offset=20,y_offset=20,text_color = 'red',source=source,level = 'glyph',render_mode = "css",text_font_size = "20pt")
f.add_layout(labels)
description = Label(x = 4,y = 2,text="Hello World",render_mode = "css")
f.add_layout(description)
#create glyphs
f.circle(x="average_grades",y="exam_grades",size=8)
#create function
def update_labels(attr,old,new):
labels.text=select.value
# #labels.text = getattr(select,attr)
#create select widget
options=[("average_grades","Average Grades"),("exam_grades","Exam Grades"),("student_names","Student Names")]
select=Select(title="Attribute",options=options)
select.on_change("value",update_labels)
# #create layout and add to curdoc
lay_out=layout([[select]])
curdoc().add_root(f)
curdoc().add_root(lay_out)
解决方法
请始终提供有关版本信息以及每个问题。
这似乎是一个错误。我可以在最新的Bokeh版本2.2.1上看到该问题,但是,如果尝试使用2.3开发版本,则该问题已消失。因此,很明显,这一点在最近某个时候已经解决,尽管可能是无意间。请提交GitHub issue,以便我们确保有意修复并保留测试中的任何补丁。解决方案是等待下一个版本2.3(如果已完成,则等待2.2.2)。
同时,我唯一能提供的建议是使用整数坐标代替类别(字符串)坐标,然后使用带有刻度标签替代的fixed ticker values:
p.xaxis.major_label_overrides = {1: "A",2,"B",...}
使其“看起来相同”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。