简体   繁体   English

表面绘制二维数组

[英]Surface plot a 2d array

I'm trying to work from the surface example: 我正在尝试从表面示例:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-1, 20, 0.25)
Y = np.arange(0, 5, 0.25)

X, Y = np.meshgrid(X, Y)


R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
        linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

I have the following bidimensional array: 我有以下二维数组:

[[29607.0], [23774.0, 52621.0], [71861.0], [9540.0, 12.9], []]

Each array correspond to a position in Y and each value a position in X with its corresponding value in Z. For example: 每个数组对应一个Y位置,每个值对应一个X位置以及Z对应值。例如:

for Y= 0, I would have X=1 with Z=29607.0 for Y= 1, I would have X=1 with Z=23774.0 and X=2 with Z=52621.0 对于Y = 0,对于Y = 1,我将具有X = 1,对于Z = 29607.0,对于Z = 23774.0,我将具有X = 1,对于Z = 52621.0,X = 2

I've tried several things but all I get is errors. 我已经尝试了几件事,但是我得到的只是错误。

I'm not sure this helps with whatever problems you are having but you could convert your values to a more normal format [[x1,y1,z1],[x2,y2,z2]... using something like this 我不确定这对您遇到的任何问题是否有帮助,但是您可以将值转换为更普通的格式[[x1,y1,z1],[x2,y2,z2]...

arr = [[29607.0], [23774.0, 52621.0], [71861.0], [9540.0, 12.9]]
new_arr = []
for i, sub_arr in enumerate(arr):
  for j, z in enumerate(sub_arr):
    new_arr.append([i+1, j, z])

and get X,Y,Z as slices in axis=1 from the numpy version of that ie 并从该轴的numpy版本中获取X,Y,Z作为轴= 1的切片

new_arr = np.array(new_arr)
X = new_arr[:,0]

PS if arr was already an ndarray the you could do: PS如果arr已经是ndarray,则可以执行以下操作:

new_arr = np.array([[j+1, i, z] for (i, j), z in np.ndenumerate(arr)])

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

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