[英]Plot 3rd axis of a 3D numpy array
I have a 3D numpy array that is a stack of 2D (m,n) images at certain timestamps, t. 我有一个3D numpy数组,它是在某些时间戳t上的2D(m,n)图像堆栈。 So my array is of shape (t, m, n).
所以我的数组是形状(t,m,n)。 I want to plot the value of one of the pixels as a function of time.
我想绘制一个像素值作为时间的函数。
eg: 例如:
import numpy as np
import matplotlib.pyplot as plt
data_cube = []
for i in xrange(10):
a = np.random(100,100)
data_cube.append(a)
So my (t, m, n) now has shape (10,100,100). 所以我的(t,m,n)现在的形状为(10,100,100)。 Say I wanted a 1D plot the value of index [12][12] at each of the 10 steps I would do:
假设我要在我要执行的10个步骤中的每个步骤上绘制一维索引[12] [12]的值:
plt.plot(data_cube[:][12][12])
plt.show()
But I'm getting index out of range errors. 但是我得到索引超出范围错误。 I thought I might have my indices mixed up, but every plot I generate seems to be in the 'wrong' axis, ie across one of the 2D arrays, but instead I want it 'through' the vertical stack.
我以为我可能会混合使用索引,但是我生成的每个图似乎都在“错误”轴上,即跨2D数组之一,但是我希望它“通过”垂直堆栈。 Thanks in advance!
提前致谢!
Here is the solution: Since you are already using numpy
, convert you final list to an array and just use slicing. 解决方案如下:由于您已经在使用
numpy
,因此将最终列表转换为数组,而仅使用切片。 The problem in your case was two-fold: 您的问题有两个方面:
First: Your final data_cube
was not an array. 第一:您最终的
data_cube
不是数组。 For a list, you will have to iterate over the values 对于列表,您将不得不遍历值
Second: Slicing was incorrect. 第二:切片不正确。
import numpy as np
import matplotlib.pyplot as plt
data_cube = []
for i in range(10):
a = np.random.rand(100,100)
data_cube.append(a)
data_cube = np.array(data_cube) # Added this step
plt.plot(data_cube[:,12,12]) # Modified the slicing
Output 产量
A less verbose version that avoids iteration: 一个不太冗长的版本,避免了迭代:
data_cube = np.random.rand(10, 100,100)
plt.plot(data_cube[:,12,12])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.