首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用dict设置Bokeh图属性

使用dict设置Bokeh图属性
EN

Stack Overflow用户
提问于 2021-04-01 05:27:39
回答 1查看 38关注 0票数 0

我有一个执行某些计算的类,使用generate_plot()创建Bokeh figure,并将绘图对象存储到self.p。我希望然后能够使用字典以编程方式更改存储的图形属性,但我找不到这样做的方法。其思路如下:

代码语言:javascript
复制
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()函数更改父脚本中的属性。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2021-05-09 06:06:30

您可以尝试执行以下操作:这将打开两个绘图,第二个绘图中的attribute min_height和title具有不同的图形设置。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66895561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档