Matplotlib:径向绘制2d阵列以制作3d Scatterplot

时间:2016-11-07 11:58:31

标签: python matplotlib plot 3d

我希望制作一个类似于found here的情节,简单的区别是我想设置每个点距离中心的距离。即,如果一块图是一个圆圈,我希望每个点距离中心有一个可定义的距离。

我的开头是什么,给出了前面提到的答案的简单修改:

class_definition

enter image description here 我想绘制的内容(为模糊草图道歉):  enter image description here

我尝试使用interp2d添加上面注释掉的from mpl_toolkits.mplot3d import Axes3D import matplotlib import numpy as np from scipy.interpolate import interp1d from matplotlib import cm from matplotlib import pyplot as plt step = 0.04 maxval = 1.0 fig = plt.figure() ax = Axes3D(fig) # u here would define the desired distance from radial axis # u=np.array([0,1,2,1,0,2,4,6,4,2,1]) v=np.array([4,4,6,3,6,4,1,4,4,4,4]) r=np.array([0,1,2,3,4,5,6,7,8,9,10]) f=interp1d(r,u) # walk along the circle p = np.linspace(0,2*np.pi,len(r)) R,P = np.meshgrid(r,p) # transform them to cartesian system X,Y = R*np.cos(P),R*np.sin(P) Z=f(R) ax.scatter(X, Y, Z)#, rstride=1, cstride=1, cmap=cm.jet) ax.set_xticks([]) fig.savefig(str(output_prefix + '3d..png'), dpi=(200)) 变量,但没有运气。将u更改为数组Z会导致X,Y和Z必须具有相同大小的错误(u,因为X和Y现在已被插值,这是可以理解的)我需要什么做?任何提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

我不确切地知道你在问题中的意思。 我将v设为x轴中圆心的偏移量。

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.interpolate import interp1d
from matplotlib import pyplot as plt
step = 0.04
maxval = 1.0
fig = plt.figure()
ax = Axes3D(fig)
# v here would define the desired distance from radial axis
u=np.array([0,1,2,1,0,2,4,6,4,2,1])
v=np.array([4,4,6,3,6,4,1,4,4,4,4])
r=np.array([0,1,2,3,4,5,6,7,8,9,10])
f=interp1d(r,u)

# walk along the circle
V = np.tile(v, (len(u), 1))
p = np.linspace(0,2*np.pi,len(r))
R,P = np.meshgrid(r,p)
# transform them to cartesian system
X,Y = V + R*np.cos(P),R*np.sin(P)

Z=f(R)

ax.scatter(X, Y, Z)#, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xticks([])
plt.show()