for循环中的matplotlib子图

时间:2014-10-15 12:40:12

标签: python matplotlib

我有一个簇Ca并且坐标locX和locY

>>> Ca
{0: [0, 13, 14, 17, 19, 22, 30], 20: [1, 15, 18, 20, 24], 37: [4, 8, 9, 33, 37, 40, 47], 41: [3, 5, 7, 28, 32, 34, 36, 39, 41, 42, 44, 46], 26: [2, 6, 16, 21, 23, 25, 26, 27, 29, 31, 35], 45: [10, 11, 12, 38, 43, 45, 48, 49]}
>>> locX.T
array([[-1.        ,  0.27272727,  0.27272727,  0.09090909,  1.        ,
        -0.27272727, -0.63636364,  0.27272727,  1.        ,  0.45454545,
         0.27272727, -0.27272727, -0.81818182, -0.27272727, -0.45454545,
         0.63636364, -0.09090909, -0.63636364,  1.        , -0.81818182,
         0.81818182, -0.09090909, -1.        ,  0.45454545,  1.        ,
         0.09090909, -0.45454545, -0.81818182,  0.63636364, -0.63636364,
        -1.        , -0.27272727,  0.45454545,  0.81818182, -0.09090909,
        -0.81818182,  0.27272727,  1.        , -1.        ,  0.63636364,
         0.81818182, -0.09090909, -0.45454545, -1.        ,  0.09090909,
        -0.63636364, -0.09090909,  0.63636364, -0.45454545,  0.09090909]])
>>> locY.T
array([[ 0.61904762,  0.80952381,  0.52380952,  0.04761905,  0.04761905,
        -0.23809524,  0.42857143, -0.52380952, -0.61904762, -0.80952381,
        -1.        , -1.        , -0.42857143,  1.        ,  0.80952381,
         0.71428571,  0.71428571,  0.61904762,  0.61904762,  0.52380952,
         0.52380952,  0.42857143,  0.33333333,  0.33333333,  0.23809524,
         0.23809524,  0.23809524,  0.14285714,  0.14285714,  0.04761905,
        -0.04761905, -0.04761905, -0.04761905, -0.14285714, -0.14285714,
        -0.14285714, -0.23809524, -0.23809524, -0.33333333, -0.33333333,
        -0.42857143, -0.42857143, -0.52380952, -0.61904762, -0.61904762,
        -0.71428571, -0.71428571, -0.71428571, -0.80952381, -0.9047619 ]])

我已经定义了一个绘制集群的函数

def plot_cluster(C):
    colors = []

    for i in range(p):
        for k in C.keys():
            if i in C[k]:
                colors.append(k)

    print('colors=', colors)
    fig = pl.scatter(locX, locY, c=colors, s=60, cmap='spectral')
    fig = pl.xlabel(r'$X$', size=30)
    fig = pl.ylabel(r'$Y$', size=30)
    pl.show(fig)
    return fig

现在我希望有8个不同群集的子图(4 x 2)。像

这样的东西
for col in range(2):
   for row in range(4):
       C = new_cluster()
       plot_cluster(C) #in position (row, col).

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用pyplot.subplots()设置轴网格,然后使用axes.scatter()等单独绘制每个轴的网格。

答案 1 :(得分:0)

我已经解决了使用

pl.subplot2grid((n_row, n_col), (row, 0))
plot_cluster(Ca, a.compId_)
pl.subplot2grid((n_row, n_col), (row, 1))
plot_cluster(Cb, b.compId_)