return 子图的值
return values of subplot
目前我正在尝试熟悉 matplotlib.pyplot 库。在看了很多示例和教程之后,我注意到 subplots 函数也有一些 returns 值,这些值通常在以后使用。但是,在 matplotlib 网站上我无法找到关于返回什么的任何规范,并且 none 个示例是相同的(尽管它通常看起来是一个 ax 对象)。你们能给我一些关于返回什么以及我如何使用它的指示吗?提前致谢!
在documentation it says that matplotlib.pyplot.subplots
return an instance of Figure
and an array of (or a single) Axes
中(数组与否取决于子图的个数)。
常用的是:
import matplotlib.pyplot as plt
import numpy as np
f, axes = plt.subplots(1,2) # 1 row containing 2 subplots.
# Plot random points on one subplots.
axes[0].scatter(np.random.randn(10), np.random.randn(10))
# Plot histogram on the other one.
axes[1].hist(np.random.randn(100))
# Adjust the size and layout through the Figure-object.
f.set_size_inches(10, 5)
f.tight_layout()
通常,matplotlib.pyplot.subplots() returns 一个图形实例和一个对象或数组轴对象。
由于您还没有发布您试图亲自动手的代码,我将通过 2 个测试用例来完成:
案例 1:当提到需要的子图数量(维度)时
import matplotlib.pyplot as plt #importing pyplot of matplotlib
import numpy as np
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
fig, axes = plt.subplots(2, 1)
axes[0].scatter(x, y)
axes[1].boxplot(x, y)
plt.tight_layout()
plt.show()
正如您在这里看到的,因为我们已经给出了所需的子图数量,在这种情况下 (2,1) 表示否。行,r = 2 和没有。列数,c = 1。
在这种情况下,子图 returns 图形实例以及一个轴数组,其长度等于总数。子图的 = r*c ,在这种情况下 = 2。
情况2:未提及子图数(维度)时
import matplotlib.pyplot as plt #importing pyplot of matplotlib
import numpy as np
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
fig, axes = plt.subplots()
#size has not been mentioned and hence only one subplot
#is returned by the subplots() method, along with an instance of a figure
axes.scatter(x, y)
#axes.boxplot(x, y)
plt.tight_layout()
plt.show()
在这种情况下,没有明确提及大小或维度,因此除了图形实例之外,只创建了一个子图。
您还可以使用 squeeze 关键字来控制子图的尺寸。参见 documentation。它是一个可选参数,默认值为 True。
实际上,'matplotlib.pyplot.subplots()' 返回两个对象:
- 图形实例。
- 'axes'.
'matplotlib.pyplot.subplots()' 需要很多参数。已在下面给出:
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw =None, **fig_kw)
前两个参数是:nrows:我想在子图网格中创建的行数,ncols:子图网格中应包含的列数。但是,如果 'nrows' 和 'ncols' 没有被显式清除,默认情况下会取值 1。
现在,来看看已经创建的对象:
(1)图形实例只不过是抛出一个包含所有图的图形。
(2)'axes'对象将包含每个子图的所有信息。
我们通过一个例子来理解:
这里,在(0,0),(0,1),(1,0),(1,1)的位置创建了4个子图。
现在,假设在位置 (0,0) 处,我想要一个散点图。我会做什么:我会将散点图合并到“axes[0,0]”对象中,该对象将保存有关散点图的所有信息并将其反映到图形实例中。
其他三个位置也会发生同样的事情。
希望这会有所帮助,让我知道您对此的看法。
目前我正在尝试熟悉 matplotlib.pyplot 库。在看了很多示例和教程之后,我注意到 subplots 函数也有一些 returns 值,这些值通常在以后使用。但是,在 matplotlib 网站上我无法找到关于返回什么的任何规范,并且 none 个示例是相同的(尽管它通常看起来是一个 ax 对象)。你们能给我一些关于返回什么以及我如何使用它的指示吗?提前致谢!
在documentation it says that matplotlib.pyplot.subplots
return an instance of Figure
and an array of (or a single) Axes
中(数组与否取决于子图的个数)。
常用的是:
import matplotlib.pyplot as plt
import numpy as np
f, axes = plt.subplots(1,2) # 1 row containing 2 subplots.
# Plot random points on one subplots.
axes[0].scatter(np.random.randn(10), np.random.randn(10))
# Plot histogram on the other one.
axes[1].hist(np.random.randn(100))
# Adjust the size and layout through the Figure-object.
f.set_size_inches(10, 5)
f.tight_layout()
通常,matplotlib.pyplot.subplots() returns 一个图形实例和一个对象或数组轴对象。
由于您还没有发布您试图亲自动手的代码,我将通过 2 个测试用例来完成:
案例 1:当提到需要的子图数量(维度)时
import matplotlib.pyplot as plt #importing pyplot of matplotlib
import numpy as np
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
fig, axes = plt.subplots(2, 1)
axes[0].scatter(x, y)
axes[1].boxplot(x, y)
plt.tight_layout()
plt.show()
正如您在这里看到的,因为我们已经给出了所需的子图数量,在这种情况下 (2,1) 表示否。行,r = 2 和没有。列数,c = 1。 在这种情况下,子图 returns 图形实例以及一个轴数组,其长度等于总数。子图的 = r*c ,在这种情况下 = 2。
情况2:未提及子图数(维度)时
import matplotlib.pyplot as plt #importing pyplot of matplotlib
import numpy as np
x = [1, 3, 5, 7]
y = [2, 4, 6, 8]
fig, axes = plt.subplots()
#size has not been mentioned and hence only one subplot
#is returned by the subplots() method, along with an instance of a figure
axes.scatter(x, y)
#axes.boxplot(x, y)
plt.tight_layout()
plt.show()
在这种情况下,没有明确提及大小或维度,因此除了图形实例之外,只创建了一个子图。
您还可以使用 squeeze 关键字来控制子图的尺寸。参见 documentation。它是一个可选参数,默认值为 True。
实际上,'matplotlib.pyplot.subplots()' 返回两个对象:
- 图形实例。
- 'axes'.
'matplotlib.pyplot.subplots()' 需要很多参数。已在下面给出:
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw =None, **fig_kw)
前两个参数是:nrows:我想在子图网格中创建的行数,ncols:子图网格中应包含的列数。但是,如果 'nrows' 和 'ncols' 没有被显式清除,默认情况下会取值 1。
现在,来看看已经创建的对象: (1)图形实例只不过是抛出一个包含所有图的图形。
(2)'axes'对象将包含每个子图的所有信息。
我们通过一个例子来理解:
这里,在(0,0),(0,1),(1,0),(1,1)的位置创建了4个子图。
现在,假设在位置 (0,0) 处,我想要一个散点图。我会做什么:我会将散点图合并到“axes[0,0]”对象中,该对象将保存有关散点图的所有信息并将其反映到图形实例中。 其他三个位置也会发生同样的事情。
希望这会有所帮助,让我知道您对此的看法。