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

时间:2016-06-14 09:19:33

标签: python matplotlib

我们如何创建具有不同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 :(得分:0)

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

看一下例子:

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

相关问题