I am trying to plot the surface profile but I am getting this only. I don’t know why it’s not plotting the surface profile. Where is the mistake?
Where am I going wrong ? I have already mentioned all the axes but all I am getting is the above figure. Can anybody help in figuring it out?
JavaScript
x
53
53
1
dis=[10,40]
2
b=0
3
f=25/60
4
time=20
5
t_i=0.001
6
mean=0.03
7
std=0.00666
8
r=0.2
9
w=2*np.pi*200/60
10
v=2*np.pi*r*200/60
11
avg=0.03
12
high=2
13
xm=np.linspace(0,2,500)
14
ym=high
15
# fig,ax=plt.subplots(figsize=(12,2))
16
ax=fig.add_subplot(projection='3d')
17
for b in range(len(dis)):
18
n=2*3.14*dis[b]*r/(avg*100)
19
s=np.random.normal(mean,std,round(n))
20
21
for i in s:
22
t=np.arange(0.1,time,t_i)
23
k=r+i
24
c=v/k
25
m=np.sqrt((k**2-(k-0.01)**2)/k)
26
j=np.arcsin(m)
27
x=k*(np.sin((c*t)+j))+f*t
28
y=k*(np.cos((c*t)+j))
29
ax.set_xlim([0.3,2])
30
ax.set_ylim([y.min()-0.05,-0.1])
31
reversed = False
32
ind0=0
33
while ind0 < len(x):
34
ind1 = ind0 + 1
35
if reversed:
36
while ind1 < len(x) and x[ind1] <= x[ind1 - 1]:
37
ind1 += 1
38
ym = np.minimum(ym, np.interp(xm, x[ind0:ind1][::-1], y[ind0:ind1][::-1], left=high, right=high))
39
else:
40
while ind1 < len(x) and x[ind1] >= x[ind1 - 1]:
41
ind1 += 1
42
ym=np.minimum(ym, np.interp(xm, x[ind0:ind1], y[ind0:ind1], left=high, right=high))
43
ind0 = ind1
44
reversed = not reversed
45
46
Y=np.linspace(0,30,200)
47
z=np.outer(ym[:200],np.ones(200))
48
xs,ys=np.meshgrid(xm[:200],Y)
49
fig=plt.figure(figsize=(15,8))
50
ax.set_xlabel('x (mm)')
51
ax.set_ylabel('y (mm)')
52
ax.plot_surface(xs, ys,z,cmap='viridis')
53
Advertisement
Answer
You are creating an ax
object before you have created a fig
object, while you are creating new fig
objects inside of your for loop. Move the creation of fig
outside of the for loop.
JavaScript
1
53
53
1
dis=[10,40]
2
b=0
3
f=25/60
4
time=20
5
t_i=0.001
6
mean=0.03
7
std=0.00666
8
r=0.2
9
w=2*np.pi*200/60
10
v=2*np.pi*r*200/60
11
avg=0.03
12
high=2
13
xm=np.linspace(0,2,500)
14
ym=high
15
# fig,ax=plt.subplots(figsize=(12,2))
16
fig=plt.figure(figsize=(15,8))
17
ax=fig.add_subplot(projection='3d')
18
for b in range(len(dis)):
19
n=2*3.14*dis[b]*r/(avg*100)
20
s=np.random.normal(mean,std,round(n))
21
22
for i in s:
23
t=np.arange(0.1,time,t_i)
24
k=r+i
25
c=v/k
26
m=np.sqrt((k**2-(k-0.01)**2)/k)
27
j=np.arcsin(m)
28
x=k*(np.sin((c*t)+j))+f*t
29
y=k*(np.cos((c*t)+j))
30
# ax.set_xlim([0.3,2])
31
# ax.set_ylim([y.min()-0.05,-0.1])
32
reversed = False
33
ind0=0
34
while ind0 < len(x):
35
ind1 = ind0 + 1
36
if reversed:
37
while ind1 < len(x) and x[ind1] <= x[ind1 - 1]:
38
ind1 += 1
39
ym = np.minimum(ym, np.interp(xm, x[ind0:ind1][::-1], y[ind0:ind1][::-1], left=high, right=high))
40
else:
41
while ind1 < len(x) and x[ind1] >= x[ind1 - 1]:
42
ind1 += 1
43
ym=np.minimum(ym, np.interp(xm, x[ind0:ind1], y[ind0:ind1], left=high, right=high))
44
ind0 = ind1
45
reversed = not reversed
46
47
Y=np.linspace(0,30,200)
48
z=np.outer(ym[:200],np.ones(200))
49
xs,ys=np.meshgrid(xm[:200],Y)
50
ax.set_xlabel('x (mm)')
51
ax.set_ylabel('y (mm)')
52
ax.plot_surface(xs, ys,z,cmap='viridis')
53