从三维数组生成彩色图。

huangapple go评论53阅读模式
英文:

Color plot from 3D arrays

问题

我有一个包含数组的NumPy数组,例如:
Y[:,0] 是表示x轴坐标的列表。对于这个轴上的每个点,由索引 indx 标记,存在以下情况:
Y[indx,1][0] 是y轴坐标的列表,而
Yindx,1 是z轴数据的列表。
我现在可以使用下面的代码绘制3D线条数组。但是,我想绘制一个2D图,其中z数据代表x和y坐标上的颜色映射(类似于小波变换图的输出)。

#虚拟数据
Y=[0]*10
for i in range(10):
    temp=np.array([[indx,np.cos(indx)] for indx in range(50)])
    Y[i]=np.array([i,temp.transpose()])
Y=np.array(Y,dtype=object)

#在3D中绘制
ax = plt.figure().add_subplot(projection='3d')
for indx in range(len(Y[:,0])):
    ax.plot(Y[indx,1][0], Y[indx,1][1], zs=Y[:,0][indx], zdir='x')

这给我了一个3D图。我如何获得我描述的2D图?

英文:

I have a numpy array of arrays such as:
Y[:,0] is a list representing the x axis coordinates. For each point on this axis, marked by the index indx, there is
Y[indx,1][0] is a list of y axis coordinates, and
Yindx,1 is a list of of the z axis data
I can now use the code below to plot an array of lines in 3D. However, I would like to plot a 2D plot where the z data represents a color map on the x and y coordinates (something that looks like the output of a wavelet transform plot).

#dummy data
Y=[0]*10
for i in range(10):
    temp=np.array([[indx,np.cos(indx)] for indx in range(50)])
    Y[i]=np.array([i,temp.transpose()])
Y=np.array(Y,dtype=object)

#plotting in 3D
ax = plt.figure().add_subplot(projection='3d')
for indx in range(len(Y[:,0])):
    ax.plot(Y[indx,1][0], Y[indx,1][1], zs=Y[:,0][indx], zdir='x')

This gives me the 3D plot. How can I get the 2D plot as I described?

答案1

得分: 2

y轴坐标对于每个x轴坐标是否相同,例如 Y[0,1][0] = Y[1,1][0]?此外,y轴坐标是否均匀间隔?下面的解决方案假设这两者为真。

解决方案

创建一个与每个 (x, y) 位置相关联的z值数组。然后调用 plt.imshow(...),将新数组作为参数。甚至可以包括一个色标!

从三维数组生成彩色图。

注意:使用虚拟数据时,颜色图谱并不令人印象深刻,但它确实适当显示了你要求的图。

代码

# 虚拟数据
Y=[0]*10
for i in range(10):
    temp=np.array([[indx,np.cos(indx)] for indx in range(50)])
    Y[i]=np.array([i,temp.transpose()],dtype=object)

Y=np.array(Y,dtype=object)

xVals = Y.T[0] # x轴坐标(未使用,但是是一个有用的信息)
yVals = Y.T[1][1][0].astype(int) # y轴坐标作为整数数组(也未使用)

# 创建zVals数组
zVals = np.concatenate(Y.T[1])[1::2]

# 在3D中绘制
fig = plt.figure()
ax3d = fig.add_subplot(211,projection='3d')

for indx in range(len(Y[:,0])):
    ax3d.plot(Y[indx,1][0], Y[indx,1][1], zs=Y[:,0][indx], zdir='x')

# 在2D中绘制
ax = fig.add_subplot(212)
im = plt.imshow(zVals)
cb = plt.colorbar()
英文:

Are the y-coordinates going to be the same for every x-coordinate e.g. Y[0,1][0] = Y[1,1][0]? Also, will the y-coordinates be evenly spaced? The solution below assumes both of these to be true.

Solution

Create an array of z-values associated with each (x,y) location. Then call plt.imshow(...) using the new array as an argument. You can even include a colorbar!

从三维数组生成彩色图。

Note: The colormap is nothing impressive when using the dummy data, but it appropriately shows the plot you asked for.

Code

#dummy data
Y=[0]*10
for i in range(10):
    temp=np.array([[indx,np.cos(indx)] for indx in range(50)])
    Y[i]=np.array([i,temp.transpose()],dtype=object)

Y=np.array(Y,dtype=object)

xVals = Y.T[0] # x axis coordinates (not used, but a useful piece of information)
yVals = Y.T[1][1][0].astype(int) # y axis coordinates as an integer array (also not used)

# create the array of zVals
zVals = np.concatenate(Y.T[1])[1::2]

#plotting in 3D
fig = plt.figure()
ax3d = fig.add_subplot(211,projection='3d')

for indx in range(len(Y[:,0])):
    ax3d.plot(Y[indx,1][0], Y[indx,1][1], zs=Y[:,0][indx], zdir='x')

#plotting in 2D
ax = fig.add_subplot(212)
im = plt.imshow(zVals)
cb = plt.colorbar()

huangapple
  • 本文由 发表于 2023年6月2日 05:33:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定