pythonbokehpymc3arviz

Arviz and Bokeh: Trying to change the title of the plot using backend kwargs


I am trying to change the title of the plot I'm creating with Arviz. Usually I have done this using the backend_kwargs but it doesn't seem to work for certain plots: in this case the plot_density.

This is the code i'm using:

kwg = dict(title="prior_1", height=500)
plot = az.plot_density(
                   data_m[0], 
                   group='posterior', 
                   var_names='switchpoint', 
                   backend='bokeh',
                   shade=.5, 
                   backend_kwargs=kwg
                   )

It recognises the height change in the kwargs so its not like its not picking up on them being there.

I have also tried other variations such as plot_title= and it will produce an error specifying it needs to be title=


Solution

  • ArviZ initializes Bokeh Plot instances using the alias figure. As you can see reading the docs on Plot, title is a valid argument. For several reasons however, ArviZ does not use this argument when creating the plots, it sets the title afterwards. Thus, your argument is being recognized and used only to be overwritten.

    plot_density sets the title of each plot to the variable name and, if multidimensional, its coordinates. Moreover, in addition to multiple variables (each of which can be multidimensional), it also supports plotting multiple models (thus different InferenceData objects) at the same time. You can use the argument data_labels to set the legend labels. An example of this can be seen in ArviZ's example gallery, you can even click on the legend to hide models.

    Therefore, to achieve the desired result, you have to manually modify the title after calling plot_density, which can be done with a couple extra lines. You have to call plot_density with show=False so that the figure can be modified afterwards. Then the title can be edited from the elements stored in the array returned by plot_density.

    kwg = dict(height=500)
    axes = az.plot_density(
        data_m[0],
        group="posterior",
        var_names="switchpoint",
        backend="bokeh",
        shade=0.5,
        backend_kwargs=kwg,
        show=False
    )
    axes[0,0].title.text = "prior_1"
    az.plots.backends.show_layout(axes)
    

    If you had multiple plots with several multidimensional variables you would have to edit the title for each one by looping over the array.