Matplotlib 子批的返回值
目前,我正在尝试熟悉matplotlib.pyplot库。在看过很多示例和教程之后,我注意到subplot函数也有一些返回值,这些值通常在以后使用。然而,在matplotlib网站上,我找不到关于返回内容的任何规范,并且没有一个示例是相同的(尽管它通常看起来是一个ax对象)。你们能给我一些关于返回什么以及我如何使用它的指示吗。提前谢谢 中,它表示Matplotlib 子批的返回值,matplotlib,python-2.6,Matplotlib,Python 2.6,目前,我正在尝试熟悉matplotlib.pyplot库。在看过很多示例和教程之后,我注意到subplot函数也有一些返回值,这些值通常在以后使用。然而,在matplotlib网站上,我找不到关于返回内容的任何规范,并且没有一个示例是相同的(尽管它通常看起来是一个ax对象)。你们能给我一些关于返回什么以及我如何使用它的指示吗。提前谢谢 中,它表示matplotlib.pyplot.subplot返回的实例和数组(或单个)(数组与否取决于子地块的数量) 常用的是: import matplotli
matplotlib.pyplot.subplot
返回的实例和数组(或单个)(数组与否取决于子地块的数量)
常用的是:
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.subplot()返回地物实例和对象或轴对象数组
由于您还没有发布您试图弄脏双手的代码,因此我将通过以下两个测试用例来完成:
案例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()
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()
正如您在这里看到的,因为我们已经给出了所需的子批次数量,(2,1),在这种情况下,这意味着行数,r=2,列数,c=1。
在这种情况下,子地块返回地物实例以及轴数组,其长度等于子地块的总数=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(2, 1)
axes[0].scatter(x, y)
axes[1].boxplot(x, y)
plt.tight_layout()
plt.show()
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()
在这种情况下,没有明确提到尺寸或维度,因此除了figure实例之外,只创建了一个子批次
您还可以使用挤压关键字控制子批次的尺寸。看见它是一个可选参数,默认值为True。实际上,“matplotlib.pyplot.subplot()”返回两个对象:
希望这会有帮助,让我知道你的想法。你是指
子地块()
还是子地块()
?我提到了子地块。我忘记了它们都存在plt.subplots()
返回一个图和另一个类型不是常量(标量或数组)的值,除非.Aha,我自己无法从文档中得到这个值。加油!