gpt4 book ai didi

python - fig, ax = plt.subplots() 意思

转载 作者:行者123 更新时间:2023-12-05 02:06:00 30 4
gpt4 key购买 nike

我已经使用 matplotlib 一段时间了,但我并不真正理解这一行的作用。

fig, ax = plt.subplots()

谁能解释一下?

最佳答案

plt.subplots() 基本上是一个(非常好的)初始化图形和子图轴的快捷方式。 See the docs here .特别是,

>>> fig, ax = plt.subplots(1, 1)

本质上等同于

>>> fig = plt.figure()
>>> ax = fig.add_subplot(1, 1)

但是 plt.subplots() 对于一次构造多个轴最有用,例如,

>>> fig, axes = plt.subplots(2, 3)

制作一个2行3列子图的图形,本质上等同于

>>> fig = plt.figure()
>>> axes = np.empty((2,3))
>>> for i in range(2):
... for j in range(3):
... axes[i,j] = fig.add_subplot(2, 3, (i*j)+j+1)

我说“本质上”是因为 plt.subplots() 也有一些不错的功能,比如 sharex=True 强制每个子图共享相同的 x 轴(即,相同的轴限制/比例等)。这是我最喜欢的初始化图窗的方法,因为它为您提供了图窗和所有轴在一条平滑线上的 handle 。

关于python - fig, ax = plt.subplots() 意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63039065/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com