Python相当于R的布局()

问题描述 投票:0回答:1

Python 的什么相当于 R 的

layout()
函数,它可以创建任何形状的绘图网格?

考虑

layout()
制作的以下3个数字:

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

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

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

在Python中,如何制作与上面一模一样布局的组成地块图形?

python r matplotlib equivalent
1个回答
0
投票

最简单的对应物是

plt.subplot_mosaic
,它是制作自定义网格规格的便利包装器:

  • 每个字符为一列,每一行为一行
  • 每个独特的字母都被分组到一个子图中
  • 每个点
    .
    代表空白

所以你的 3 个例子对应于这 3 个马赛克:

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

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

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


完整示例:

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()

© www.soinside.com 2019 - 2024. All rights reserved.