Python的:一个克莱因瓶的3D图 [英] Python: 3D plot of a Klein Bottle

查看:2224
本文介绍了Python的:一个克莱因瓶的3D图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在学习Python和我目前在做的事情3D绘图。为了使事情变得有趣我要让克莱因瓶,但不知何故,是不是在所有加工。我试图(对,一个在随机网站的)这两个给了面两个参数化圆环十岁上下的身影。

所以我在想,如果我也许code是错误的。任何人可以看看,并告诉我,如果我这样做是正确的(如果你碰巧知道一个克莱因瓶的参数化,那么这是值得欢迎的太:P)

 从mpl_toolkits.mplot3d进口Axes3D
进口matplotlib.pyplot为PLT
从matplotlib进口厘米
进口numpy的为NP

高清冲浪(U,V):
    X =(3 +(1 + np.sin(ⅴ))+ 2 *(1  -  np.cos(ⅴ)/ 2)* np.cos(U))* np.cos(v)的
    Y =(4 + 2 *(1  -  np.cos(ⅴ)/ 2)* np.cos(U))* np.sin(v)的
    Z = -2 *(1-np.cos(ⅴ)/ 2)* np.sin(u)的
    返回X,Y,Z
UX,VX = np.meshgrid(np.linspace(0,2 * np.pi,20),
                      np.linspace(0,2 * np.pi,20))
X,Y,Z =冲浪(UX,VX)

图= plt.figure()
斧= fig.gca(投影=三维)

积= ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,CMAP = cm.jet,
                       线宽= 0,抗锯齿=假)

plt.show()
 

解决方案

您的Python code具有正确的形式,但它看起来像有可能是参数化的一些错误。下面是不同的参数化:

 进口mpl_toolkits.mplot3d.axes3d为axes3d
进口matplotlib.pyplot为PLT
进口numpy的为NP

COS = np.cos
罪= np.sin
开方= np.sqrt
PI = np.pi

高清冲浪(U,V):
    
    http://paulbourke.net/geometry/klein/
    
    半=(0℃= U)及(U< PI)
    R = 4 *(1  -  COS(U)/ 2)
    X = 6 * COS(U)*(1 + SIN(u)的)+ R *为cos(V + PI)的
    X [一半] =(
        (6 * COS(U)*(1 +罪(U))+ R * COS(U)* COS(V))[一半])
    Y = 16 * SIN(U)
    Y [一半] =(16 *罪(U)+ R * SIN(U)* COS(V))[一半]
    Z = R *罪(五)
    返回X,Y,Z

U,V = np.linspace(0,2 * PI,40),np.linspace(0,2 * PI,40)
UX,VX = np.meshgrid(U,V)
X,Y,Z =冲浪(UX,VX)

图= plt.figure()
AX = fig.gca(投影='3D')
积= ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,CMAP = plt.get_cmap('喷射'),
                       线宽= 0,抗锯齿=假)

plt.show()
 

So i'm learning python and I am currently on making 3D plots of things. To keep things interesting I want to make a plot of Klein Bottle, but somehow it is not at all working. And i tried two parametrizations of the surface (one on Wolfram and one on a random website) both gave a torus-ish figure.

So I was wondering if maybe my code is wrong. Could anybody take a look and tell me if i am doing it right (and if you happen to know the parametrisation of a Klein bottle, then that is welcome too :P)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

def surf(u, v):
    X = (3+(1+np.sin(v)) + 2*(1 - np.cos(v)/2)*np.cos(u))*np.cos(v)
    Y = (4+2*(1 - np.cos(v)/2) * np.cos(u))*np.sin(v)
    Z = -2*(1-np.cos(v)/2)*np.sin(u)
    return X,Y,Z
ux, vx =  np.meshgrid(np.linspace(0, 2*np.pi, 20),
                      np.linspace(0, 2*np.pi, 20))
x,y,z = surf(ux, vx)

fig = plt.figure()
ax = fig.gca(projection="3d")

plot = ax.plot_surface(x,y,z, rstride=1, cstride=1, cmap=cm.jet,
                       linewidth=0, antialiased=False)

plt.show()

解决方案

Your Python code has the correct form, but it looks like there might be some error in the parametrization. Here is the Klein bottle produced by a different parametrization:

import mpl_toolkits.mplot3d.axes3d as axes3d
import matplotlib.pyplot as plt
import numpy as np

cos = np.cos
sin = np.sin
sqrt = np.sqrt
pi = np.pi

def surf(u, v):
    """
    http://paulbourke.net/geometry/klein/
    """
    half = (0 <= u) & (u < pi)
    r = 4*(1 - cos(u)/2)
    x = 6*cos(u)*(1 + sin(u)) + r*cos(v + pi)
    x[half] = (
        (6*cos(u)*(1 + sin(u)) + r*cos(u)*cos(v))[half])
    y = 16 * sin(u)
    y[half] = (16*sin(u) + r*sin(u)*cos(v))[half]
    z = r * sin(v)
    return x, y, z

u, v = np.linspace(0, 2*pi, 40), np.linspace(0, 2*pi, 40)
ux, vx =  np.meshgrid(u,v)
x, y, z = surf(ux, vx)

fig = plt.figure()
ax = fig.gca(projection = '3d')
plot = ax.plot_surface(x, y, z, rstride = 1, cstride = 1, cmap = plt.get_cmap('jet'),
                       linewidth = 0, antialiased = False)

plt.show()

这篇关于Python的:一个克莱因瓶的3D图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆