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

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

问题描述

所以我正在学习 Python,目前我正在制作事物的 3D 绘图.为了让事情变得有趣,我想绘制 Klein Bottle 的情节,但不知何故它根本不是在职的.我尝试了两种表面参数化(一种在 Wolfram 上,另一种在随机网站上)都给出了一个圆环形状的人物.

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.

所以我想知道我的代码是否有误.任何人都可以看看并告诉我我是否做得对(如果您碰巧知道克莱因瓶的参数化,那么也欢迎:P)

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()

推荐答案

您的 Python 代码格式正确,但参数化中似乎存在一些错误.这是由不同的参数化产生的克莱因瓶:

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天全站免登陆