轮廓的曲面图

时间:2021-04-05 13:50:52

标签: python matplotlib

我正在尝试绘制表面轮廓,但我只得到了这个。我不知道为什么它没有绘制表面轮廓。错在哪里?

enter image description here

我哪里出错了?我已经提到了所有的轴,但我得到的只是上图。有人可以帮忙解决吗?

dis=[10,40]
b=0
f=25/60
time=20
t_i=0.001
mean=0.03
std=0.00666
r=0.2
w=2*np.pi*200/60
v=2*np.pi*r*200/60
avg=0.03
high=2
xm=np.linspace(0,2,500)
ym=high
# fig,ax=plt.subplots(figsize=(12,2))
ax=fig.add_subplot(projection='3d')
for b in range(len(dis)):
    n=2*3.14*dis[b]*r/(avg*100)
    s=np.random.normal(mean,std,round(n))
    
    for i in s:   
        t=np.arange(0.1,time,t_i) 
        k=r+i
        c=v/k
        m=np.sqrt((k**2-(k-0.01)**2)/k)
        j=np.arcsin(m)
        x=k*(np.sin((c*t)+j))+f*t
        y=k*(np.cos((c*t)+j))
        ax.set_xlim([0.3,2])
        ax.set_ylim([y.min()-0.05,-0.1])
        reversed = False
        ind0=0
        while ind0 < len(x):
            ind1 = ind0 + 1
            if reversed:
                while ind1 < len(x) and x[ind1] <= x[ind1 - 1]:
                    ind1 += 1
                ym = np.minimum(ym, np.interp(xm, x[ind0:ind1][::-1], y[ind0:ind1][::-1], left=high, right=high))
            else:
                while ind1 < len(x) and x[ind1] >= x[ind1 - 1]:
                    ind1 += 1
                ym=np.minimum(ym, np.interp(xm, x[ind0:ind1], y[ind0:ind1], left=high, right=high))
            ind0 = ind1
            reversed = not reversed
    
    Y=np.linspace(0,30,200)
    z=np.outer(ym[:200],np.ones(200))
    xs,ys=np.meshgrid(xm[:200],Y)
    fig=plt.figure(figsize=(15,8))
    ax.set_xlabel('x (mm)')
    ax.set_ylabel('y (mm)')
    ax.plot_surface(xs, ys,z,cmap='viridis')

1 个答案:

答案 0 :(得分:0)

您在创建 ax 对象之前创建了一个 fig 对象,而您正在 for 循环内创建新的 fig 对象。将 fig 的创建移到 for 循环之外。

dis=[10,40]
b=0
f=25/60
time=20
t_i=0.001
mean=0.03
std=0.00666
r=0.2
w=2*np.pi*200/60
v=2*np.pi*r*200/60
avg=0.03
high=2
xm=np.linspace(0,2,500)
ym=high
# fig,ax=plt.subplots(figsize=(12,2))
fig=plt.figure(figsize=(15,8))
ax=fig.add_subplot(projection='3d')
for b in range(len(dis)):
    n=2*3.14*dis[b]*r/(avg*100)
    s=np.random.normal(mean,std,round(n))
    
    for i in s:   
        t=np.arange(0.1,time,t_i) 
        k=r+i
        c=v/k
        m=np.sqrt((k**2-(k-0.01)**2)/k)
        j=np.arcsin(m)
        x=k*(np.sin((c*t)+j))+f*t
        y=k*(np.cos((c*t)+j))
        # ax.set_xlim([0.3,2])
        # ax.set_ylim([y.min()-0.05,-0.1])
        reversed = False
        ind0=0
        while ind0 < len(x):
            ind1 = ind0 + 1
            if reversed:
                while ind1 < len(x) and x[ind1] <= x[ind1 - 1]:
                    ind1 += 1
                ym = np.minimum(ym, np.interp(xm, x[ind0:ind1][::-1], y[ind0:ind1][::-1], left=high, right=high))
            else:
                while ind1 < len(x) and x[ind1] >= x[ind1 - 1]:
                    ind1 += 1
                ym=np.minimum(ym, np.interp(xm, x[ind0:ind1], y[ind0:ind1], left=high, right=high))
            ind0 = ind1
            reversed = not reversed
    
    Y=np.linspace(0,30,200)
    z=np.outer(ym[:200],np.ones(200))
    xs,ys=np.meshgrid(xm[:200],Y)
    ax.set_xlabel('x (mm)')
    ax.set_ylabel('y (mm)')
    ax.plot_surface(xs, ys,z,cmap='viridis')

surface plot