Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python equivalent to R's layout()

What’s Python’s equivalent to R’s layout() function which can create a plotting grid of any shape?

Consider the following 3 figures made by layout():

set.seed(123)
layout(t(matrix(c(
  1, 1, 2, 2, 3, 3,
  4, 5, 5, 6, 6, 7
), ncol = 2)), widths = rep(1, 6), heights = rep(1, 2))
par(mar = c(4, 5, 1, 1), family = "serif")
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 1
plot(x = runif(30), y = runif(30), cex.axis = 1.5, 
     bty = "L", xlab = "", ylab = "", las = 1) # 2
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 3
plot.new() # 4
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 5
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 6

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

set.seed(123)
layout(t(matrix(c(
  1, 1, 2, 2, 3, 3,
  4, 4, 4, 5, 5, 5
), ncol = 2)), widths = rep(1, 6), heights = rep(1, 2))
par(mar = c(4, 5, 1, 1), family = "serif")
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 1
plot(x = runif(30), y = runif(30), cex.axis = 1.5, 
     bty = "L", xlab = "", ylab = "", las = 1) # 2
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 3
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 4
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 5

enter image description here

set.seed(123)
layout(t(matrix(c(
  1, 1, 2, 2, 3, 3,
  4, 4, 4, 4, 5, 5
), ncol = 2)), widths = rep(1, 6), heights = rep(1, 2))
par(mar = c(4, 5, 1, 1), family = "serif")
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 1
plot(x = runif(30), y = runif(30), cex.axis = 1.5, 
     bty = "L", xlab = "", ylab = "", las = 1) # 2
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 3
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 4
plot(x = runif(30), y = runif(30), cex.axis = 1.5,
     bty = "L", xlab = "", ylab = "", las = 1) # 5

enter image description here

In Python, how to make figures of constituent plots with exactly the same layout as the above?

>Solution :

The simplest counterpart is plt.subplot_mosaic, which is a convenience wrapper to make custom gridspecs:

  • Each character is a column, and each line is a row
  • Each unique letter gets grouped into one subplot
  • Each dot . represents empty space

So your 3 examples correspond to these 3 mosaics:

fig, axs = plt.subplot_mosaic('''
    aabbcc
    .ddee.
''')

fig, axs = plt.subplot_mosaic('''
    aabbcc
    dddeee
''')

fig, axs = plt.subplot_mosaic('''
    aabbcc
    ddddee
''')


Full example:

import matplotlib.pyplot as plt
import numpy as np

mosaic = '''
    aabbcc
    .ddee.
'''
fig, axs = plt.subplot_mosaic(mosaic, figsize=(7, 4))

for label, ax in axs.items():
    ax.scatter(np.random.random(30), np.random.random(30), s=10)
    ax.set_title(label)

plt.tight_layout()
plt.show()

full example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading