Search code examples
pythonholoviewshvplotholoviz

Change fontsize in holoviews or hvplot


How do I change fontsizes using holoviews or hvplot?

Here's a simple example of the plot I have:

# import libraries
import numpy as np
import pandas as pd

import holoviews as hv
import hvplot.pandas
hv.extension('bokeh', logo=False)

# create sample dataframe
df = pd.DataFrame({
    'col1': np.random.normal(size=30),
    'col2': np.random.normal(size=30),
})

# plot data with holoviews
my_plot = hv.Scatter(df, label='Scattering around')

Solution

  • 1) You can change fontsizes by using .opts(fontsize={}) on your plot, like so:

    # change fontsizes for different parts of plot
    my_plot.opts(fontsize={
        'title': 15, 
        'labels': 14, 
        'xticks': 10, 
        'yticks': 10,
    })
    


    2) With version >= 1.13 of holoviews you can scale all the fonts all at once and make legend, title, xticks larger with .opts(fontscale=1.5):

    # make fonts all at once larger for your plot by the same scale
    # here I've chosen fontscale=2, which makes all fonts 200% larger
    my_plot.opts(fontscale=2)
    


    3) If you would like to scale your fonts and you have a bokeh backend for your holoviews plot, you can do:

    # scale fontsizes with 200%
    my_plot.opts(fontsize={
        'title': '200%',
        'labels': '200%', 
        'ticks': '200%', 
    })
    

    More info on all the possible parts of which you can change the fontsize can be found here:
    http://holoviews.org/user_guide/Customizing_Plots.html#Font-sizes