繁体   English   中英

for循环中的matplotlib子图

[英]matplotlib subplot in a for loop

我有一个簇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).

我怎样才能做到这一点?

您可以使用pyplot.subplots()设置一个轴网格,然后使用axes.scatter()等在每个轴上分别进行axes.scatter()

我已经解决了

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_)

暂无
暂无

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

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