Search code examples
pythonholoviewshvplotholoviz

Create a grid of plots with holoviews/hvplot and set the max number of columns


I'd like to plot several data into a grid using holoviews/hvplot, based on one dimension, which contains several unique data points.

Considering this example:

import seaborn as sns
import hvplot.pandas

iris = sns.load_dataset('iris')
plot = iris.hvplot.scatter(x="sepal_length", y="sepal_width", col="species")
hvplot.show(plot)

The above code creates several plots, based on the species part of the iris data set, resulting in the picture below:
hvplot gridspace example with too many columns

But now imagine there were not 3 different species, but twenty. The plot would get to wide so I'd like to break the line after a few plots. But I couldn't find any "maximum columns" parameter. A normal grid expects another column to define the rows which I don't have.

Any suggestions would help.


Solution

  • In your case I would not create a Gridspace (by using keyword 'row' and 'col') but a Layout.
    When you have a Layout you can adjust the number of columns easily with .cols(2).

    Using hvplot you have to use keyword 'by' and 'subplots=True' instead of 'col'.

    See the code below:

    iris.hvplot.scatter(
        x='sepal_length',
        y='sepal_width',
        by='species',
        subplots=True,
    ).cols(2)
    


    Resulting plot:

    creating a layout to show plots