简体   繁体   English

在Python Matplotlib中创建子图

[英]Creating Subplots in Python Matplotlib

When I'm creating subplots in Python's Maplotlib, I have to do it this way: 当我在Python的Maplotlib中创建子图时,我必须这样做:

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)

Why, instead, doesn't the following work, 相反,为什么不做以下工作,

fig, ax1, ax2, ax3, ax4 = plt.subplots(nrows=2, ncols=2)

I'm trying to figure out exactly how plt.subplots. 我正在试图弄清楚plt.subplots究竟是怎样的。 I read the documentation but I am still uncertain as to what is going on. 我阅读了文档,但我仍然不确定发生了什么。 Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

plt.subplots() returns a tuple, (fig,axarr), where axarr is an array of axis objects. plt.subplots()返回一个元组,(fig,axarr),其中axarr是一个轴对象数组。 So if you have two rows and two columns, axarr is a [2,2] array of axes, but the subplots method only will return two objects, not five. 因此,如果您有两行和两列,axarr是[2,2]轴的数组,但子图方法只返回两个对象,而不是五个。 This is true for any tuple, and not limited plt.subplots(). 对于任何元组都是如此,并且不限于plt.subplots()。 So the following will run: 所以以下将运行:

a = 'banana'
b = np.array([1,2,3,4])
tup = (a,b)
fruit,(h,i,j,k) = tup

But fruit,h,i,j,k = tup will not. fruit,h,i,j,k = tup不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM