简体   繁体   English

如何将 plot (22、3、2)矩阵列表列表作为散点 plot?

[英]How to plot a (22, 3, 2) matrix list of lists as a scatter plot?

I am trying to plot the below sensitivity matrix into a plot that consists of each list as sub-plot in it.我正在尝试将以下灵敏度矩阵 plot 转换为 plot ,其中包含每个列表作为其中的子图。 Preferably as a function and look like this one in seaborn tutorial.最好作为 function 并在 seaborn 教程中看起来像这个

I tried a similar way to plot the list of list but it only give me a single plot without subplots:我尝试了类似于 plot 列表列表的方法,但它只给了我一个没有子图的 plot :

def scatter_plot(sen1_obs):
    
    x = []

y = []
for i in sen1_obs:
    x.append(i[0])
    y.append(i[1])
plt.scatter(x,y)
plt.show()

And execute:并执行:

scatter_plot(sen1_obs)

My list look like this:我的清单如下所示:

sen1_obs
Out[2]: 
[array([[ 0.00040725,  0.00011072],
        [ 0.0008145 ,  0.00022144],
        [-0.00040725, -0.00011072]]),
 array([[ 8.32091781e-04, -8.06469105e-05],
        [ 1.66418356e-03, -1.61293821e-04],
        [-8.32091781e-04,  8.06469105e-05]]),
 array([[ 0.00121915,  0.00033146],
        [ 0.00243829,  0.00066291],
        [-0.00121915, -0.00033146]]),
 array([[ 0.00252797, -0.00024503],
        [ 0.00505593, -0.00049006],
        [-0.00252797,  0.00024503]]),
 array([[ 0.00202743,  0.00055121],
        [ 0.00405486,  0.00110242],
        [-0.00202743, -0.00055121]]),
 array([[ 0.00430218, -0.00041706],
        [ 0.00860436, -0.00083411],
        [-0.00430218,  0.00041706]]),
 array([[ 3.08460052e-04,  8.38668514e-05],
        [ 6.16920103e-04,  1.67733703e-04],
        [-3.08460052e-04, -8.38668514e-05]]),
 array([[ 0.00132295, -0.00012862],
        [ 0.0026459 , -0.00025724],
        [-0.00132295,  0.00012862]]),
 array([[ 0.00089098,  0.00024225],
        [ 0.00178196,  0.0004845 ],
        [-0.00089098, -0.00024225]]),
 array([[ 0.00402754, -0.00039161],
        [ 0.00805507, -0.00078323],
        [-0.00402754,  0.00039161]]),
 array([[ 0.00149702,  0.00040703],
        [ 0.00299405,  0.00081405],
        [-0.00149702, -0.00040703]]),
 array([[ 0.00665672, -0.00064722],
        [ 0.01331343, -0.00129443],
        [-0.00665672,  0.00064722]]),
 array([[ 0.00879632,  0.00016573],
        [ 0.01759264,  0.00033147],
        [-0.00879632, -0.00016573]]),
 array([[ 0.00728856, -0.00055898],
        [ 0.01457712, -0.00111797],
        [-0.00728856,  0.00055898]]),
 array([[ 0.00913137, -0.00029702],
        [ 0.01826274, -0.00059403],
        [-0.00913137,  0.00029702]]),
 array([[ 0.00463152,  0.00026766],
        [ 0.00926303,  0.00053532],
        [-0.00463152, -0.00026766]]),
 array([[ 0.00315   ,  0.00014372],
        [ 0.0063    ,  0.00028743],
        [-0.00315   , -0.00014372]]),
 array([[ 0.00607034,  0.00023261],
        [ 0.01214067, 

You can try something like this.你可以尝试这样的事情。 I don't have a great way to use your data so disregard my first few lines of code that are making the data I am working with.我没有很好的方法来使用您的数据,因此请忽略我正在使用的数据的前几行代码。 But, essentially what you are doing is making a 5x5 grid to house your 22 subplots.但是,基本上你正在做的是制作一个 5x5 的网格来容纳你的 22 个子图。 You then create a list of lists for their positions in the subplot (we'll call it pos ).然后,您为其在子图中的位置创建一个列表列表(我们将其称为pos )。 You then iterate over all the arrays in your variable en1_obs and all the lists within your arrays.然后遍历变量en1_obs中的所有 arrays 以及 arrays 中的所有列表。 Delete the left over subplots (25-22 so 3 unused subplots) and you have your figure:删除剩下的子图(25-22 个,所以 3 个未使用的子图),你就有了你的图:

######### This is just code to make a (22,3,2) matrix to work with #########
import random
en1_obs = []
for x in range(22):
    longlists = []
    for y in range(3):
        lists = []
        for z in range(2):
            lists.append(random.randint(1, 60))
        longlists.append(lists)
    en1_obs.append(np.array(longlists))
############################################################################

### This is the important code

plt.rcParams["figure.figsize"] = (20,10)    
width = 5
height = 5
fig, ax = plt.subplots(height,width)
pos = [[x,y] for x in range(height) for y in range(width)]

count = 0
for i in en1_obs:
    ax[pos[count][0],pos[count][1]].scatter(i[:, 0], i[:, 1])
    count +=1
fig.delaxes(ax[4,2])
fig.delaxes(ax[4,3])
fig.delaxes(ax[4,4])
plt.show()

在此处输入图像描述

The first point is to transform the data, so I am transforming it by changing the dimensions.第一点是转换数据,所以我通过改变维度来转换它。 The graph is drawn in a loop procedure with that transformed array.该图是使用该转换后的数组在循环过程中绘制的。 Empty graphs are manually deleted, so please adjust them.空图为手动删除,请自行调整。 The number of data presented does not match the number of graphs, but please adjust the number of graphs to match my code to your data.显示的数据数量与图表数量不匹配,但请调整图表数量以使我的代码与您的数据匹配。

fig, axs = plt.subplots(5, 4, figsize=(9,9))
fig.subplots_adjust(wspace=0.5,hspace=0.5)

def scatter_plot(data):
    for d,ax in zip(data, axs.ravel()):
        xy = d.flatten().reshape(2,3, order='F')
        ax.scatter(xy[0], xy[1])
    fig.delaxes(axs[4,1])
    fig.delaxes(axs[4,2])
    fig.delaxes(axs[4,3])
    plt.show()

scatter_plot(data)

在此处输入图像描述

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

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