创造一个革命的表面

问题描述 投票:1回答:1

我有一个磁盘的3d图,这里是代码:

ri = 100
ra = 300
h=20

# input xy coordinates
xy = np.array([[ri,0],[ra,0],[ra,h],[ri,h],[ri,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 30 steps
phi = np.linspace(0, 2*np.pi, 50)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 30 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.set_zlim(0,200)
ax.plot_surface(X, Y, Z, alpha=0.5, color='grey', rstride=1, cstride=1)

我得到了这个漂亮的情节:enter image description here

我还有这个情节:enter image description here

代码是:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

arr = np.array([[100, 15],
               [114.28, 17],
               [128.57, 18],
               [142.85, 19],
               [157.13, 22],
               [171.13, 24],
               [185.69, 25],
               [199.97, 27],
               [214.25, 28],
               [228.53, 30],
               [242.81, 31],
               [257.09, 35],
               [271.37, 36],
               [288.65, 37],
               [300, 38]])

#interpolating between the single values of the arrays
new_x = np.concatenate([np.linspace(arr[i,0],arr[i+1,0], num=50)
                        for i in range(len(arr)-1)])

new_y = np.interp(new_x, arr[:,0], arr[:,1])
t=np.arange(700)
p = plt.scatter(new_x,new_y,c=t, cmap="jet")

#inserting colorbar
cax, _ = mpl.colorbar.make_axes(plt.gca(), shrink=0.8)
cbar = mpl.colorbar.ColorbarBase(cax, cmap='jet', label='testvalues',
                       norm=mpl.colors.Normalize(15, 40))
plt.show()

现在我的问题是:有没有办法将这个2D图形绘制到我的3d环境中?还可以通过围绕中点旋转它们来创建这条线以外的曲面(点)吗?我尝试的方式与我用磁盘一样,但是我失败了,因为我认为我需要一个封闭的轮廓?这是一张更好地了解我想要的图片:

enter image description here

python matplotlib 3d surface
1个回答
2
投票

我不确定你想如何包含你的2D图,所以这就是你如何把它作为一个革命的表面。

你的new_x对应于径向距离,new_y对应于高度。所以我们需要生成一个角度数组,以生成“锥形”:

from matplotlib import cm

tmp_phi = np.linspace(0,2*np.pi,50)[:,None] # angle data
linesurf_x = new_x*np.cos(tmp_phi)
linesurf_y = new_x*np.sin(tmp_phi)
linesurf_z = np.broadcast_to(new_y, linesurf_x.shape)

linesurf_c = np.broadcast_to(t, linesurf_x.shape) # color according to t
colors = cm.jet(linesurf_c/linesurf_c.max()) # grab actual colors for the surface
ax.plot_surface(linesurf_x, linesurf_y, linesurf_z, facecolors=colors,
                rstride=1, cstride=1)

结果:

picture

© www.soinside.com 2019 - 2024. All rights reserved.