Matplotlib subplots column wrap

Question:

Does Matplotlib have a built-in column wrap method?

The following image shows an analogous method in Seaborn’s catplot.

Screenshot of Seaborn col_wrap usage in the catplot documentation.

Purpose…

I have a function that plots N images with plt.imshow(). The number of images can varies, from about 20 to 50. I am using subplots to tile them, and would like to automate the number of ‘rows’ by using a wrapping function such as the one mentioned above.

Asked By: Juancheeto

||

Answers:

I don’t think there’s built-in support for a column wrapper in matplotlib.

In fact, if you look at the Seaborn source code for FacetGrid, you can see that the author uses a simple calculation based on the col_wrap parameter to build the subplot layout.

if col_wrap is not None:
  if row is not None:
      err = "Cannot use `row` and `col_wrap` together."
      raise ValueError(err)
  ncol = col_wrap
  nrow = int(np.ceil(len(col_names) / col_wrap))

https://github.com/mwaskom/seaborn/blob/master/seaborn/axisgrid.py#L418

Answered By: privard
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.