Matplotlib学习---用mplot3d画莫比乌斯环(Mobius strip)
mplot3d是matplotlib里用于绘制3D图形的一个模块。关于mplot3d 绘图模块的介绍请见:https://blog.csdn.net/dahunihao/article/details/77833877。
莫比乌斯环(mobius strip)是一种只有一个曲面的拓扑结构。把一个纸条扭转180°后,两头再粘接起来,这样的纸带只有一个面(即单侧曲面),一只小虫可以爬遍整个曲面而不必跨过它的边缘。
莫比乌斯环是一个二维的紧致流形 (即一个有边界的面),可以嵌入到三维或更高维的流形中 。那么如何在3D空间上画莫比乌斯环呢?可以利用现有的方程式,将莫比乌斯环的两个参数(角度,宽度)映射到三维空间中。以下摘自维基百科(https://en.wikipedia.org/wiki/M%C3%B6bius_strip):
"""
One way to represent the Möbius strip as a subset of three-dimensional Euclidean space is using the parametrization:
where 0 ≤ u < 2π and −1 ≤ v ≤ 1. This creates a Möbius strip of width 1 whose center circle has radius 1, lies in the xyplane and is centered at (0, 0, 0). The parameter u runs around the strip while v moves from one edge to the other.
"""
大概的意思就是:这个方程组可以创造一个宽度为1,中心圆半径为1的莫比乌斯环,其所处位置为x-y平面,中心为(0,0,0)。参数u绕整个环转圈,参数v则从一个边缘移动到另一个边缘。
下面让我们一步一步来画莫比乌斯环。首先导入numpy,matplotlib和其3D模块,创建一个图形和一个三维坐标轴:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection='3d')
其次,定义u,v参数(u为绕环一圈的角度,v为环的宽度):
u = np.linspace(0, 2*np.pi, endpoint=True, num=50)
v = np.linspace(-1, 1, endpoint=True, num=10)
然后,按照方程式,将u,v参数映射到三维空间(x轴,y轴和z轴):
u,v=np.meshgrid(u,v)
u=u.flatten()
v=v.flatten()
x = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)
y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)
z = 0.5 * v * np.sin(u / 2.0)
最后,选一个好看的配色方案,画出图像:
ax.plot_trisurf(x,y,z,cmap="cool")
此时图像如下:

可以看出图像已基本成型,但是部分地方还有缺角。这是因为三角剖分还不正确。可以使用matplotlib现成的方法定义三角剖分:
import matplotlib.tri as mtri
tri = mtri.Triangulation(u, v)
最后的最后,把图像变大一点,把x轴,y轴和z轴的上下限修改一下,这样图像看起来更好一些。
完整代码如下:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10,6))
ax = plt.axes(projection='3d') # u, v are parameterisation variables
u = np.linspace(0, 2*np.pi, endpoint=True, num=50)
v = np.linspace(-1, 1, endpoint=True, num=10) # This is the Mobius mapping, taking a u, v pair and returning an x, y, z
# triple
u,v=np.meshgrid(u,v) #用meshgrid函数来产生三维绘图时的矩阵
u=u.flatten() #把u展开,变成一维数组
v=v.flatten() #把v展开,变成一维数组
x = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)
y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)
z = 0.5 * v * np.sin(u / 2.0) # Triangulate parameter space to determine the triangles
import matplotlib.tri as mtri
tri = mtri.Triangulation(u, v) # Plot the surface. The triangles in parameter space determine which x, y, z
# points are connected by an edge.
ax.plot_trisurf(x,y,z,cmap="cool",triangles=tri.triangles)
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-1.1, 1.1)
ax.set_zlim(-1, 1) plt.show()
图像如下:

参考:https://matplotlib.org/gallery/mplot3d/trisurf3d_2.html?highlight=mobius
Matplotlib学习---用mplot3d画莫比乌斯环(Mobius strip)的更多相关文章
- Matplotlib学习---用seaborn画联合分布图(joint plot)
有时我们不仅需要查看单个变量的分布,同时也需要查看变量之间的联系,这时就需要用到联合分布图. 这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图 ...
- Matplotlib学习---用wordcloud画词云(Word Cloud)
画词云首先需要安装wordcloud(生成词云)和jieba(中文分词). 先来说说wordcloud的安装吧,真是一波三折.首先用pip install wordcloud出现错误,说需要安装Vis ...
- Matplotlib学习---用seaborn画矩阵图(pair plot)
矩阵图非常有用,人们经常用它来查看多个变量之间的联系. 下面用著名的鸢尾花数据来画一个矩阵图.从sklearn导入鸢尾花数据,然后将其转换成pandas的DataFrame类型,最后用seaborn画 ...
- Matplotlib学习---用seaborn画直方图,核密度图(histogram, kdeplot)
由于直方图受组距(bin size)影响很大,设置不同的组距可能会产生完全不同的可视化结果.因此我们可以用密度平滑估计来更好地反映数据的真实特征.具体可参见这篇文章:https://blog.csdn ...
- Matplotlib学习---用matplotlib画直方图/密度图(histogram, density plot)
直方图用于展示数据的分布情况,x轴是一个连续变量,y轴是该变量的频次. 下面利用Nathan Yau所著的<鲜活的数据:数据可视化指南>一书中的数据,学习画图. 数据地址:http://d ...
- Matplotlib学习---用matplotlib画箱线图(boxplot)
箱线图通过数据的四分位数来展示数据的分布情况.例如:数据的中心位置,数据间的离散程度,是否有异常值等. 把数据从小到大进行排列并等分成四份,第一分位数(Q1),第二分位数(Q2)和第三分位数(Q3)分 ...
- Matplotlib学习---matplotlib的一些基本用法
Matplotlib有两种接口,一种是matlab风格接口,一种是面向对象接口.在这里,统一使用面向对象接口.因为面向对象接口可以适应更复杂的场景,在多图之间进行切换将变得非常容易. 首先导入matp ...
- 数学图形之莫比乌斯带(mobius)
莫比乌斯带,又被译作:莫比斯环,梅比斯環或麦比乌斯带.是一种拓扑学结构,它只有一个面(表面),和一个边界.即它的正反两面在同一个曲面上,左右两个边在同一条曲线上.看它的名字很洋气,听它的特征很玄乎,实 ...
- 神奇的莫比乌斯带(mobius)
1.禅师和青年之间的对话 2.制作一个莫比乌斯带 3.神奇的莫比乌斯带 4.对莫比乌斯带进行简单的数学建模 1.禅师和青年之间的对话 青年问禅师:“大师,我很爱我的女朋友,她也有很多优点,但是总有几个 ...
随机推荐
- NanoFabric-ServiceFabric 操作手册
service-fabric-52abp-ocelot A Service Fabric sample with a Frontend, one API Gateway and 52abp Micro ...
- flask实现子域名
什么是子域名? 子域名,类似于xxx.douban.com的形式,如book.douban.com,music.douban.com,movie.douban.com等 用flask怎么实现子域名? ...
- Acceleration for ML 论文导读
Energy efficient parallel neuromorphic architectures with approximate arithmetic on FPGA Motivation ...
- ubuntu中更改apache默认目录的方法
如上,在这两个文件中,我都改为/home/www 及/home/www/html
- Nagios 系统监控
Nagios 系统监控 Nagios 是一款免费的开源 IT 基础设施监控系统,功能强大,灵活性强,能有效监控 Windows.Linux.VMware 和 Unix 主机状态,交换机.路由器等网络设 ...
- ssh无密码登录多台机器,并让所有远程机执行相同命令
问题集锦 其实标题的内容就是很常见的集群操作,当有一个脚本或者一个命令需要很多个机器同时(或者说接近同时)执行时,就涉及到几个问题: 怎么通知每个主机? 每个主机收到通知后,怎么让主机执行命令? 怎么 ...
- javascript内置函数:toString()
不同对象有不同的实现方式. 1.Number对象: 语法:numberObject.toString([radix]) 参数:radix,可选/Number类型,指定的基数(进制数),支持[2,36] ...
- [转帖]Windows和Linux对决(多进程多线程)
Windows和Linux对决(多进程多线程) https://blog.csdn.net/world_2015/article/details/44920467 太长了 还没看完.. 还是没太理解好 ...
- C# Note1:深入浅出WPF-MVVM篇
一.资源说明 (1)配套视频:深入浅出WPF 讲的不错! 待更!
- javap -v没有显示LocalVaribleTable
时隔多日,终于找到为什么javap -v .class文件没有LocalVariableTable出现 因为默认的javac编译没有生成相关的调试信息,这里我们可以通过javac -help查看指令帮 ...