How to best update plot which uses GraphRenderer?

Hi everyone!

So I’m using graph renderer in plotting the data but whenever I make some changes on the plot, what I have to do is to reconstruct the whole plot and providing an updated graph renderer object. This manner seems a bit off to me since you can definitely see that it turns off then on showing it’s being refreshed every time there’s a change.

So in detail here’s what I’m doing to update the plot,

  • Constuct the graph renderer object using new data (depending on the activity) > make new plot using this new graph renderer object > replace one currently being shown.

So would like to ask if there’s a better to do this?

Additional Info:

I’m using v. 2.3.2. And here’s a simple illustration on how I’m implementing this,

class SampleClass:

    def layout(doc):
        data = { ‘x’: [1, 2, 3, 4, 5], ‘y’: [6, 7, 8, 9, 10] }
        graphrenderer = GraphRenderer()
        graphrenderer.node_renderer.data_source = ColumnDataSource(data)
        graphrenderer.node_renderer.glyph = Circle(x=“x”, y=“y”,                size=self.spinnerNode.value, fill_color=nodeColor, fill_alpha=0.5)
        plot = figure(width=400, height=400)
        plot.renderers.append(graphrenderer)
        button = Button(label=“Change Data”, button_type=“success”)

        def update_plot():
             newData = { ‘x’: [6, 7, 8, 9, 10] , ‘y’: [11, 12, 13, 14, 15] }
             graphrenderer = GraphRenderer()
             graphrenderer.node_renderer.data_source = ColumnDataSource(data)
             graphrenderer.node_renderer.glyph = Circle(x=“x”, y=“y”,                size=self.spinnerNode.value, fill_color=nodeColor, fill_alpha=0.5)
             plot = figure(width=400, height=400)
             plot.renderers.append(graphrenderer)

             layout[0] = plot

        button.on_click(update_plot())

        components = column(plot, button)
        doc.add_root(components)

        return doc

So, basically, to make this work, I have to reconstruct the whole thing to reflect the changes.

Hi @ian-cahila-maersk, you can find more info on modifying a ColumnDataSource here. In short, you should be able to replace the source.data attribute instead of defining a new source. If you’re simply appending/patching, there are methods for doing so in further down the article I linked.

graphrenderer.node_renderer.data_source.data = new_data