如何在matplotlib中创建具有不同wspace的子图?

4
我们如何创建具有不同wspace的子图。例如,我正在绘制5个图形,第一行有2个,第二行有3个。我希望第一行有不同的wspace,而最后3个wspace = 0。我知道可以使用gridspec实现,但我无法理解它,那么是否有其他方法在subplot中实现,或者有人可以解释一下gridspec是如何工作的,语法以及参数如何调整大小并将它们放置在与其他图形特定位置上。
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])

gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])

我不理解如何操作。
(gs1[:-1, :])
(gs1[-1, :-1])
(gs1[-1, -1])

工作正常。谢谢。

1个回答

0

你有gridspec.GridSpec(3, 3),这意味着3行3列,总共9个子图,索引为gs1[i,j],i表示行,j表示列

看看这个例子:

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print A[:-1, :] # get first and second rows (that is -1 means does not include last elements (last row) 
print A[-1, :-1] # from last row get all columns except last
print A[-1, -1] # get last element of array A (that is A[2][2])

输出:

[[1 2 3]
 [4 5 6]]
[7 8]
9

阅读有关Python中数组切片的文章:http://structure.usc.edu/numarray/node26.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接