首页 > 编程语言 >使用python画3D立体爱心

使用python画3D立体爱心

时间:2022-09-05 23:01:10浏览次数:60  
标签:10 python lim 爱心 points np ax append 3D

原理

1.使用python中的mtplotlib库。
2.立体爱心面公式

\[(x^2+\frac{9}{4}y^2+z^2-1)^3-\frac{9}{80}y^2*z^3-x^2*z^3=0 \]

点画法(实心)

代码

import matplotlib.pyplot as plt #导入绘图模块
from mpl_toolkits.mplot3d import Axes3D #3d绘图模块
import numpy as np #导入数值计算拓展模块

#start generating points
x_lim=np.linspace(-10,10,150)
y_lim=np.linspace(-10,10,150)
z_lim=np.linspace(-10,10,150)
X_points=[] #用来存放绘图点X坐标
Y_points=[] #用来存放绘图点Y坐标
Z_points=[] #用来存放绘图点Z坐标
for x in x_lim:
    for y in y_lim:
        for z in z_lim:
            if (x**2+(9/4)*y**2+z**2-1)**3-(9/80)*y**2*z**3-x**2*z**3<=0:
                X_points.append(x)
                Y_points.append(y)
                Z_points.append(z)

plt.style.use('seaborn')
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d') 
ax.scatter(X_points,Y_points,Z_points,color="red") 
plt.show()

运行效果

在这里插入图片描述

这个画法侧面看起来很无语。

在这里插入图片描述

点画法(空心)

代码

import matplotlib.pyplot as plt #导入绘图模块
from mpl_toolkits.mplot3d import Axes3D #3d绘图模块
import numpy as np #导入数值计算拓展模块

#start generating points
x_lim=np.linspace(-10,10,200)
y_lim=np.linspace(-10,10,200)
z_lim=np.linspace(-10,10,200)
X_points=[] #用来存放绘图点X坐标
Y_points=[] #用来存放绘图点Y坐标
Z_tmp=[]
Z_points=[] #用来存放绘图点Z坐标
for y in y_lim:
    for x in x_lim:
        for z in z_lim:
            k=(x**2+(9/4)*y**2+z**2-1)**3-(9/80)*y**2*z**3-x**2*z**3
            if k<=0 :
                Z_tmp.append(z)
                if y<=-0.55 or y>=0.55:
                    X_points.append(x)
                    Y_points.append(y)    
                    Z_points.append(z)
        if Z_tmp:
            X_points.append(x)
            Y_points.append(y)    
            Z_points.append(max(Z_tmp))
            X_points.append(x)
            Y_points.append(y)    
            Z_points.append(min(Z_tmp))
            Z_tmp.clear()

plt.style.use('seaborn')
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d') 
ax.set_zlim(-1, 1)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.scatter(X_points,Y_points,Z_points)

plt.show()

运行效果

在这里插入图片描述

折线画法 (线团)

代码

import matplotlib.pyplot as plt #导入绘图模块
from mpl_toolkits.mplot3d import Axes3D #3d绘图模块
import numpy as np #导入数值计算拓展模块

#start generating points
x_lim=np.linspace(-10,10,150)
y_lim=np.linspace(-10,10,150)
z_lim=np.linspace(-10,10,150)
X_points=[] #用来存放绘图点X坐标
Y_points=[] #用来存放绘图点Y坐标
Z_tmp=[]
Z_points=[] #用来存放绘图点Z坐标
for y in y_lim:
    for x in x_lim:
        for z in z_lim:
            k=(x**2+(9/4)*y**2+z**2-1)**3-(9/80)*y**2*z**3-x**2*z**3
            if k<=0 :
                Z_tmp.append(z)
                if y<=-0.55 or y>=0.55:
                    X_points.append(x)
                    Y_points.append(y)    
                    Z_points.append(z)
        if Z_tmp:
            X_points.append(x)
            Y_points.append(y)    
            Z_points.append(max(Z_tmp))
            X_points.append(x)
            Y_points.append(y)    
            Z_points.append(min(Z_tmp))
            Z_tmp.clear()

plt.style.use('seaborn')
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d') 
ax.set_zlim(-1, 1)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.plot(X_points,Y_points,Z_points)

plt.show()

运行效果

在这里插入图片描述

等高线画法(线框)

代码

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np


def heart_3d(x, y, z):
    return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3


def plot_implicit(fn, bbox=(-1.5, 1.5)):
    xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
    fig = plt.figure()
    ax = fig.add_subplot(projection='3d')
    A = np.linspace(xmin, xmax, 100)  # resolution of the contour
    B = np.linspace(xmin, xmax, 10)  # number of slices
    A1, A2 = np.meshgrid(A, A)  # grid on which the contour is plotted

    for z in B:  # plot contours in the XY plane
        X, Y = A1, A2
        Z = fn(X, Y, z)
        cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r',))

    for y in B:  # plot contours in the XZ plane
        X, Z = A1, A2
        Y = fn(X, y, Z)
        cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red',))

    for x in B:  # plot contours in the YZ plane
        Y, Z = A1, A2
        X = fn(x, Y, Z)
        cset = ax.contour(X+x, Y, Z, [x], zdir='x', colors=('red',))

    # must set plot limits because the contour will likely extend
    # way beyond the displayed level. Otherwise matplotlib extends the plot limits
    # to encompass all values in the contour.
    ax.set_zlim3d(zmin, zmax)
    ax.set_xlim3d(xmin, xmax)
    ax.set_ylim3d(ymin, ymax)

    plt.show()


if __name__ == '__main__':
    plot_implicit(heart_3d)

运行效果

在这里插入图片描述

这么好看所以并不是我自己写的代码。
抄的网上,出处懒得找了(自我保护)。

标签:10,python,lim,爱心,points,np,ax,append,3D
From: https://www.cnblogs.com/xianmasamasa/p/16659957.html

相关文章

  • 从零开始学python必看,“Python编程三剑客”,你值得拥有
    从0开始学Python,就问你一句:慌不慌?   第一本:《Python编程:从入门到实践》   第二本:《Python编程快速上手-让繁琐工作自动化》   第三本:《《Python极客......
  • python学习(元组、字典、set集合)
    (一)、列表 1、列表的嵌套 需求:输出数字9 解决:利用索引层级输出   2、列表的切片   (二)、元组:tuple1、列表与元组的区别?列表是可变的,元组是不可变的......
  • python(二)元组、字典、集合
    11.列表的嵌套##列表的嵌套、字符类型#list4=[1,'go','你好',1008.21,True['json','java','c++','go',[1,2,3,7]]]#print(list4[])##列表的切片,获取列表中指定范围的......
  • Python入门系列(十)一篇学会python文件处理
    文件处理在Python中处理文件的关键函数是open()函数。有四种不同的方法(模式)来打开一个文件"r"-读取-默认值。打开一个文件进行读取,如果文件不存在则出错。"a"-Ap......
  • pythonⅡ
    list1=[1,'xc',True,100.00,['sd','hi','jk',[1,0,2,3]]]print(list1[4][3][2])   '''列表的切片'''list1=[1,3,4,5,6,7,8,9,11,16,19]#开始与结束,包含开始,不含结......
  • Python读取PDF文档中的表格数据
    #-*-coding:utf-8-*-#在pdfplumber模块中提供了extract_tables()方法importpdfplumberimportpandasaspd#提取PDF文档中的表格defdemo1():withpd......
  • 用Python做一个中秋抢购月饼的脚本
    序言每逢佳节倍思亲,想买个东西给家里,结果发现手速不够,网速不够快,没有时间下单等等各种原因导致最后想买的东西售罄了…甚至跟你一起抢购的可能是脚本,太真实了!......
  • 深圳华锐视点:3D全景汽车虚拟VR体验给客户带来沉浸式的看车体验
    汽车VR展示能方便了客户在线观看汽车。客户可以查看汽车的内部结构和材料,以及汽车的外部颜色和尺寸。他们可以放大和缩小以查看汽车模型的详细显示。它可以给客户带来......
  • ubuntu16 安装 python-networkmanager 失败
    前言ubuntu16安装python-networkmanager失败解决方案sudoapt-getinstalllibdbus-1-devsudoapt-getinstalllibdbus-glib-1-devsudopip3installdbus-python......
  • 关于MicroPython mpremote工具 的一些用例,闲聊
    我尝试了很多不同的第三方micropython工具,有些是Windows应用程序,有些是VScode插件。但是当我尝试过MicroPython的mpremote工具后,我几乎决定它将成为我的首选工具。......