Add Div() with p.add_layout() method

Hi,
Trying to add my Div() object to plot figure.
custom_legend = Div(text="<b>A Title</b>", style={'font-size': '200%', 'color': 'blue'})
p.add_layout(custom_legend, 'above')
it doesn’t work and returns ValueError:
expected an element of List(Instance(Renderer)), got seq with invalid items [Div(id='1097', ...)]
Please tell me how to add Div to figure correctly.

I’m not sure if Div can be added to a plot. The only way to add Div is to add it to as a widget. You can see it here - http://docs.bokeh.org/en/1.3.2/docs/user_guide/interaction/widgets.html#div

from bokeh.io import output_file, show
from bokeh.models.widgets import Div

output_file("div.html")

div = Div(text="""Your <a href="https://en.wikipedia.org/wiki/HTML">HTML</a>-supported text is initialized with the <b>text</b> argument.  The
remaining div arguments are <b>width</b> and <b>height</b>. For this example, those values
are <i>200</i> and <i>100</i> respectively.""",
width=200, height=100)

show(div)

If you want to edit the Title properties, then you should look here - https://docs.bokeh.org/en/latest/docs/reference/models/annotations.html#bokeh.models.annotations.Title

p.title.text_color = 'blue'
p.title.text_font_size = '200%' 
#or
p.title.text_font_size = '8pt'

Thanks, but that doesnt solves my issue. I need to make Save tool able to save as image plot and this Div. If Div not added to layout it doesnt appear at generated image.

Based on your original post, if you’re trying to update the title of the plot, then you could do p.title.text = "Title Text". So whatever text you’ve, add it to the title and format the title.

Since Div is a widget, I don’t think it can be added to a figure.

1 Like

@samirak93 is correct. Div represents a DOM element, so it cannot be added to a plot, which renders to an opaque HTML raster canvas. If you need text annotations on a Plot there are a variety of options: adding extra Titles, or Label and LabelSet, to name a few.

2 Likes