Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I achieve a multiline title for a holoviews plot?

I would like more than one line as an option for a title of a holoviews plot object. Only strings are allowed, and it seems what works for a print statement does not work for a title.

import numpy as np
import holoviews as hv
hv.extension('bokeh')
from holoviews import opts

plot_title = 'Line 1 \nLine 2'
plot_title
# printing results in two lines, the \n is recognized
print(plot_title)

# the \n is ignored when the string is used for a title
points = [(0.1*i, np.sin(0.1*i)) for i in range(100)]
hv.Curve(points).opts(title=plot_title)

simple plot

like image 546
Marinna Martini Avatar asked Oct 17 '25 16:10

Marinna Martini


1 Answers

HoloViews uses bokeh as a plotting backend in your example and this feature, i.e multi-line title, isn't supported yet by bokeh. Note that your question is similar to this one: How to create a multi-line plot title in bokeh? and that there is now (27/04/2020) a related open issue on bokeh https://github.com/bokeh/bokeh/issues/7317.

I see two ways of getting multilines title for your plot.

The first one is adapted from the other SO question and makes use of bokeh directly to add titles as a layout:

import numpy as np
import holoviews as hv
import bokeh.io
from bokeh.models import Title
hv.extension('bokeh')

points = [(0.1*i, np.sin(0.1*i)) for i in range(100)]

hv_curve = hv.Curve(points)
bk_curve = hv.render(hv_curve)
bk_curve.add_layout(Title(text="Sub-Title", text_font_style="italic"), 'above')
bk_curve.add_layout(Title(text="Title", text_font_size="16pt", text_font_style="bold"), 'above')
bokeh.io.show(bk_curve)

bokeh solution

The second one makes use of panel (a dependency of HoloViews now) to display the titles as two Markdown panes (it could also be one HTML pane) both centered in a Column layout above the curve:

import numpy as np
import holoviews as hv
hv.extension('bokeh')
import panel as pn
pn.extension()

points = [(0.1*i, np.sin(0.1*i)) for i in range(100)]

hv_curve = hv.Curve(points)
panel_layout pn.Column(
    pn.pane.Markdown("**Title**", align="center"),  # bold
    pn.pane.Markdown("*Sub-title*", align="center"),  # italic
    hv_curve
)
panel_layout

panel solution

If you run panel_layout.pprint() you'll be able to check the structure of that layout.

Column
    [0] Markdown(str, align='center')
    [1] Markdown(str, align='center')
    [2] HoloViews(Curve)

Note: Run with Holoviews 1.13.2, bokeh 2.0.1 and panel 0.9.5.

like image 108
MaximeL Avatar answered Oct 20 '25 06:10

MaximeL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!