Leo
Leo

Reputation: 89

Set Bokeh plot properties using dict

I have a class that performs some calculations, and creates Bokeh figure using generate_plot(), and stores the plotting object to self.p. I would like to then be able to change the stored figure properties programmatically using a dictionary, yet I cannot find a way of doing this. The idea is as follows:

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

I want to be able to do this in order to change the properties from a parent script using the set_properties() function even after a plot has already been generated.

Any thoughts?

Upvotes: 0

Views: 240

Answers (1)

mxn
mxn

Reputation: 121

You could try the following: This will open two plots with different figure settings for the attribute min_height and title in the second one.

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

Upvotes: 1

Related Questions