我有一个执行某些计算的类,使用generate_plot()创建Bokeh figure,并将绘图对象存储到self.p。我希望然后能够使用字典以编程方式更改存储的图形属性,但我找不到这样做的方法。其思路如下:
Class BokehPlot(object):
<<init, other functions, generate_plot()>>
self.p = generate_plot()
def set_properties(self, this_dict):
for key, value in this_dict.items():
if key is valid:
self.p.key = value我希望能够这样做,以便即使在已经生成绘图之后,也能够使用set_properties()函数更改父脚本中的属性。
有什么想法吗?
发布于 2021-05-09 06:06:30
您可以尝试执行以下操作:这将打开两个绘图,第二个绘图中的attribute min_height和title具有不同的图形设置。
from bokeh.plotting import figure, output_file, show
# Sample data
x = [1, 2, 3, 4, 5, 6]
y = [5, 4, 3, 2, 1, 0]
class BokehPlot(object):
def __init__(self, x: list, y: list):
self._p = self._generate_plot(x, y)
@property
def p(self):
return self._p
def _generate_plot(self, x: list, y: list):
graph = figure(title = "Bokeh Line Graph") #, min_height=800)
graph.line(x, y)
graph.min_height = 100
return graph
def show(self):
show(self._p)
def set_properties(self, attributes_dict: dict):
figure_props = dir(self._p)
for key, value in attributes_dict.items():
if key in figure_props:
setattr(self._p, key, value)
test = BokehPlot(x, y)
test.show()
test.set_properties({"min_height": 800, "title": "New title"})
test.show()https://stackoverflow.com/questions/66895561
复制相似问题