python 3D,点成球,螺旋线/面,等高线,莫比乌斯环
程序员文章站
2022-04-02 09:39:13
...
点成球
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
PC = []
x = np.arange(-10,11,1)
for i in x: #[1,2]:
y = list(np.arange(-np.sqrt(100-i**2),np.sqrt(100-i**2)+1 ))
for it in y:
PC.append([i,it])
#print(PC)
#print(np.array(PC).shape, PC[-1])
PC_a = []
for i,j in np.array(PC): #ii
# print(ii)
# i,j = ii
print(i,j,-np.sqrt(100-i**2-j**2), np.sqrt(100-i**2-j**2))
if i**2 + j**2 >= 100:
continue
z = list(np.arange(-np.sqrt(100-i**2-j**2), np.sqrt(100-i**2-j**2)+1 ))
for it in z:
PC_a.append([i,j,it])
print(PC_a)
print(np.array(PC_a).shape)
PC_a = np.array(PC_a)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(PC_a[:,0], PC_a[:,1], PC_a[:,2])
plt.show()
螺旋线、面
#%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)#plt.axes(projection='3d')
zline = np.linspace(0,150,1000)
xdata = np.sin(zline)
ydata = np.cos(zline)
ax.plot3D(xdata,ydata,zline,'r')
z = np.array(np.arange(0,150,150/1000))
x = z * np.sin(z)
y = z * np.cos(z)
print(z,x)
ax.scatter(x,y,z,'g')
plt.show()
面 + 莫比坞斯环
#%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)#plt.axes(projection='3d')
zline = np.linspace(0,150,1000)
xdata = np.sin(zline)
ydata = np.cos(zline)
ax.plot3D(xdata,ydata,zline,'r')
z = np.array(np.arange(0,150,150/1000))
x = z * np.sin(z)
y = z * np.cos(z)
print(z,x)
ax.scatter(x,y,z,'g')
# 2 contour
def sinBin(x,y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
X = np.linspace(-6,6,30)
Y = np.linspace(-6,6,30)
X,Y = np.meshgrid(X,Y)
Z = sinBin(X,Y)
fig2 = plt.figure()
ax = Axes3D(fig2)
ax.contour(X,Y,Z,50)#, cmap='binary')
# 3 wireframe 线框图,surface 表面图
fig3 = plt.figure()
ax = Axes3D(fig3)
ax.plot_wireframe(X,Y,Z)
ax.set_title('Wireframe')
ax.set_xlabel(' X ')
fig4 = plt.figure()
ax = Axes3D(fig4)
ax.plot_surface(X,Y,Z)
ax.set_title('surface')
# vector draw
r = np.linspace(0,6,20)
theta = np.linspace(-np.pi*0.9, np.pi*0.9,30)
r,theta = np.meshgrid(r,theta)
X = r * np.sin(theta)
Y = r * np.cos(theta)
Z = sinBin(X,Y)
print(X.shape,Y.shape,Z.shape)
fig5 = plt.figure()
ax = Axes3D(fig5)
ax.plot_surface(X,Y,Z,cmap='viridis')
# ravel, scatter --表面图用了网格坐标(面坐标),
# scatter用了向量坐标,可以用ravel,flatten来转化grid为vetor画图
theta = 2 * np.pi * np.random.random(1000)
r = 6 * np.random.random(1000)
x = np.ravel(r * np.sin(theta))
y = np.ravel(r * np.cos(theta))
z = sinBin(x, y)
print(r.shape,' x ',x.shape, ' y ', y.shape)
fig6 = plt.figure()
ax = Axes3D(fig6)
ax.scatter(x,y,z,cmap='viridis')
theta = 2 * np.pi * np.random.random(1000)
r = 6 * np.random.random(1000)
x = np.squeeze(np.reshape((r * np.sin(theta)),[len(r * np.sin(theta)),1]))
print(x.shape)
y = np.ravel(r * np.cos(theta)) #解开线团
z = sinBin(x, y)
print(r.shape,' x ',x.shape, ' y ', y.shape)
fig6 = plt.figure()
ax = Axes3D(fig6)
ax.scatter(x,y,z,cmap='viridis')
# 4-b trisurf通过三角法覆盖(相邻三个点连接为三角片,然后覆盖为面)
fig7 = plt.figure()
ax = Axes3D(fig7)
ax.plot_trisurf(x,y,z,cmap='viridis')
#结果肯定不像用网格绘制时那样干净,但是这种三角剖分的灵活性允许一些非常有趣的三维图。
# 例如,实际上可以使用此方法绘制三维莫比乌斯带,如下所示。
theta = np.linspace(0,2*np.pi, 30)
w = np.linspace(-0.25,0.25, 8) #环宽度0.25, 20
w, theta = np.meshgrid(w,theta)
# 环面旋转半圈; 精华了,一个是环面的旋转,180度; linspace(0,pi,30);
#另一个是正常的x-y面旋转,它是圆,但是由于环旋转,半径是变化的; 1 + 环长度的sin值
phi = 0.5 * theta
r = 2 + w * np.sin(phi) # r0调节环大小 + 环带宽度
x = np.ravel(r * np.cos(theta)) #矩阵,拉成向量画点
y = np.ravel(r * np.sin(theta))
z = np.ravel(w * np.cos(phi)) #0.25 * np.cos(phi)
print('mobius', x.shape,y.shape,z.shape)
from matplotlib.tri import Triangulation
tri = Triangulation(np.ravel(w), np.ravel(theta))
fig8 = plt.figure()
ax = Axes3D(fig8)
ax.plot_trisurf(x,y,z, triangles = tri.triangles,cmap='viridis')
#ax.scatter(x,y,z)
plt.show()