简体   繁体   English

二维线 plot a 3D Numpy 矩阵/数组给定一个选定的轴

[英]2D line plot a 3D Numpy matrix / array given a chosen axis

How can I plot a 2D line from a chosen axis of a Numpy Array quickly?如何从 Numpy 阵列的选定轴快速 plot 一条二维线?

An analogy: when sum an arbitrary matrix sigma with respect to axis = 0 , I would write:一个类比:当相对于axis = 0对任意矩阵sigma sum时,我会写:

import numpy as np
import matplotlib.pyplot as plt

sigma = np.array([

       [[0. , 0.9, 0.6],
        [0. , 0. , 0.4],
        [0. , 0. , 0. ]],

       [[0. , 0.8, 0.5],
        [0. , 0. , 0.3],
        [0. , 0. , 0. ]],

       [[0. , 0.7, 0.4],
        [0. , 0. , 0.2],
        [0. , 0. , 0. ]]
        
        ])

np.sum(sigma, axis=0)

with result:结果:

array([[0. , 2.4, 1.5],
       [0. , 0. , 0.9],
       [0. , 0. , 0. ]])

I am seeking an equivalent straight forward method to plot axis=0 , suggestively similar to:我正在寻找与 plot axis=0等效的直接方法,暗示类似于:

plt.plot(sigma, axis=0)

This means, I will plot the depth of the matrix at each corresponding position.这意味着,我将 plot 矩阵的深度在每个对应的 position 处。 In the plot I will see three lines, one line starting at 0.9 in value at x =1, and 0.8 at x=2, and 0.7 at x-3.在 plot 中,我将看到三条线,一条线在 x = 1 时的值为 0.9,在 x = 2 时为 0.8,在 x-3 时为 0.7。 Similarly, for lines two and three, [0.6, 0.5, 0.4];同样,对于第二行和第三行,[0.6, 0.5, 0.4]; [0.4, 0.3, 0.2]. [0.4, 0.3, 0.2]。

I could find examples of plot 3d and a method (involving slice and len ) for plot 2d that would yield in a solution similar to:我可以找到plot 3d 的示例以及 plot 2d 方法(涉及slicelen ),这将在类似于以下的解决方案中产生:

plt.plot(sigma[:,:,2])

However, I cannot get it to plot against the x-axis (x = 1..3, representing each layer of array)但是,我无法在 x 轴上得到 plot(x = 1..3,代表阵列的每一层)

How do I do it?我该怎么做?

Update : a solutions seems to be:更新:一个解决方案似乎是:

plt.plot(sigma[:,:,:].reshape((3, 9)))

If I understood your question, your first dimension is a time, for which you have a 2D array at each time point, and you want to see how a given index in that 2D array evolves.如果我理解您的问题,您的第一个维度是时间,您在每个时间点都有一个 2D 数组,并且您想查看该 2D 数组中的给定索引如何演变。

One way to approach (so that you don't have to keep copying data, assuming you have a large dataset), is to flatten your original sigma array and index the 2D array locations.一种方法(这样您就不必继续复制数据,假设您有一个大数据集)是展平您的原始sigma数组并索引 2D 数组位置。

>> sigma.flatten()
array([0. , 0.9, 0.6, 0. , 0. , 0.4, 0. , 0. , 0. , 0. , 0.8, 0.5, 0. ,
       0. , 0.3, 0. , 0. , 0. , 0. , 0.7, 0.4, 0. , 0. , 0.2, 0. , 0. ,
       0. ])

Then, for each timestep in your 3x3 case, you could get the:然后,对于 3x3 案例中的每个时间步,您可以获得:

  • [0, 0] index by indexing the data at locations [0, 9, 18] [0, 0] 通过索引位置 [0, 9, 18] 的数据进行索引
  • [0, 1] index by indexing [1, 10, 19] [0, 1] 通过索引 [1, 10, 19] 进行索引

etc of the flattened array.等扁平化阵列。

A quick demo based on your example data:基于您的示例数据的快速演示:

import numpy as np
import matplotlib.pyplot as plt

sigma = np.array([
    [[0., 0.9, 0.6],
     [0., 0.,  0.4],
     [0., 0.,  0.]],

    [[0., 0.8, 0.5],
     [0., 0.,  0.3],
     [0., 0.,  0.]],

    [[0., 0.7, 0.4],
     [0., 0.,  0.2],
     [0., 0.,  0.]]
])

n, a, b = sigma.shape
n_ar = a * b  # the len of a 2D array

x = np.arange(n)  # The 2D array slice indices, [0, 1, 2]
sigma_flat = sigma.flatten()  # Flatten into 1D array and index for points

fig, ax = plt.subplots()  # Set up a figure and axes

for i in range(n_ar):
    idxs = x * n_ar + i  # Get indices of flattened array
    ax.plot(x+1, sigma_flat[idxs], label=f'Loc {i}')

fig.legend()
plt.show()

Returns:回报:

示例图

Is that what you were trying to do?那是你想要做的吗?

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

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