Matplotlib不同大小的子图

Jas*_*pel 206 python plot matplotlib figure

我需要在图中添加两个子图.一个子图需要大约是第二个(相同高度)的三倍.我使用GridSpeccolspan论证完成了这个,但我想这样做,figure所以我可以保存为PDF.我可以使用figsize构造函数中的参数调整第一个数字,但是如何更改第二个图的大小?

Hag*_*gne 321

另一种方法是使用该subplots函数并传递宽度比gridspec_kw:

import numpy as np
import matplotlib.pyplot as plt 

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
a0.plot(x, y)
a1.plot(y, x)

f.tight_layout()
f.savefig('grid_figure.pdf')
Run Code Online (Sandbox Code Playgroud)

  • 其实我最喜欢这个选项,我很高兴我滚动到底部:) (36认同)
  • 我喜欢subscots比gridspec更好,因为你不必再处理轴列表的设置了(使用gridspec,你仍然需要逐个制作轴和图).因此,子图更清晰,使用更快 (2认同)
  • 如果我希望一行中的两个图的高度也不同怎么办?更改`height_ratio`似乎会影响整个行相对于其他行。 (2认同)

bmu*_*bmu 209

你可以使用gridspecfigure:

import numpy as np
import matplotlib.pyplot as plt 
from matplotlib import gridspec

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
fig = plt.figure(figsize=(8, 6)) 
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) 
ax0 = plt.subplot(gs[0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[1])
ax1.plot(y, x)

plt.tight_layout()
plt.savefig('grid_figure.pdf')
Run Code Online (Sandbox Code Playgroud)

结果情节


end*_*ith 30

可能最简单的方法是使用subplot2grid,在使用GridSpec定制子图的位置中描述.

ax = plt.subplot2grid((2, 2), (0, 0))
Run Code Online (Sandbox Code Playgroud)

等于

import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])
Run Code Online (Sandbox Code Playgroud)

所以bmu的例子变成了:

import numpy as np
import matplotlib.pyplot as plt

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
fig = plt.figure(figsize=(8, 6))
ax0 = plt.subplot2grid((1, 3), (0, 0), colspan=2)
ax0.plot(x, y)
ax1 = plt.subplot2grid((1, 3), (0, 2))
ax1.plot(y, x)

plt.tight_layout()
plt.savefig('grid_figure.pdf')
Run Code Online (Sandbox Code Playgroud)


Jas*_*pel 27

我使用pyplotaxes对象手动调整大小而不使用GridSpec:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# definitions for the axes
left, width = 0.07, 0.65
bottom, height = 0.1, .8
bottom_h = left_h = left+width+0.02

rect_cones = [left, bottom, width, height]
rect_box = [left_h, bottom, 0.17, height]

fig = plt.figure()

cones = plt.axes(rect_cones)
box = plt.axes(rect_box)

cones.plot(x, y)

box.plot(y, x)

plt.show()
Run Code Online (Sandbox Code Playgroud)

  • 对于我们这些仍然在matplotlib 0.99没有gridspec的人有用! (2认同)
  • 对于那些gridspec不合适的人很有用 (2认同)

tom*_*mjn 27

中添加了一种很好的方法来做到这一点matplotlib 3.3.0subplot_mosaic

您可以使用“ASCII 艺术”风格制作漂亮的布局。

例如

fig, axes = plt.subplot_mosaic("ABC;DDD")
Run Code Online (Sandbox Code Playgroud)

将为您提供顶行的三个轴和底行的一个跨越整个宽度的轴,如下所示

在此输入图像描述

此方法的一个好处是,axes从函数返回的是一个包含您定义的名称的字典,使得更容易跟踪什么是什么,例如

axes["A"].plot([1, 2, 3], [1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

subplot_mosaic如果您想使用更长的名称,您还可以将列表的列表传递给

fig, axes = plt.subplot_mosaic(
    [["top left", "top centre", "top right"],
     ["bottom row", "bottom row", "bottom row"]]
)

axes["top left"].plot([1, 2, 3], [1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

将产生相同的数字

在此输入图像描述


小智 6

以简单的方式,不同大小的子绘图也可以在没有gridspec

plt.figure(figsize=(12, 6))
ax1 = plt.subplot(2,3,1)
ax2 = plt.subplot(2,3,2)
ax3 = plt.subplot(2,3,3)
ax4 = plt.subplot(2,1,2)
axes = [ax1, ax2, ax3, ax4]
Run Code Online (Sandbox Code Playgroud)

输出