matplotlib?3D模型繪制一朵小紅花
前言:
在github上看到一個(gè)有趣的代碼,雖然情人節(jié)已經(jīng)過(guò)了兩天,但還是想和大家分享^_^
1. 含苞待放
3D模型的繪制需要網(wǎng)格點(diǎn),關(guān)于網(wǎng)格點(diǎn)的作用,在基于python,Matplotlib繪制函數(shù)的等高線與三維圖像的博文中已經(jīng)介紹,這里不再贅述。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
x = np.linspace(0, 1, num=30)
t = np.linspace(0, 1, num=1200) * 20 * np.pi + 4 * np.pi
x, t = np.meshgrid(x, t)
p = 0.5 * np.pi * np.exp(-t / (8 * np.pi))
change = np.sin(15 * t) / 150
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi)**4 / 2 + change
y = 2 * (x**2 - x)**2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
# PiYG_r
# RdBu_r
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h,
? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.RdPu_r)
plt.savefig('img/img1.png')
plt.show()


2. 灼灼其華
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# plt.axis('off')
x = np.linspace(0, 1, num=30)
t = np.linspace(0, 1, num=1200) * 50 * np.pi - 4 * np.pi
x, t = np.meshgrid(x, t)
p = 0.5 * np.pi * np.exp(-t / (8 * np.pi))
change = np.sin(20 * t) / 50
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi)**4 / 2 + change
y = 2 * (x**2 - x)**2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
ax = ax.plot_surface(r * np.cos(t), r * np.sin(t), h,
? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.RdPu_r)
plt.savefig('img/img2.png')
plt.show()


有關(guān)mpl_toolkits.mplot3d的使用可以參考官方文檔;
更多的顏色搭配可參考matplotlib的colormap官方手冊(cè)。
到此這篇關(guān)于matplotlib 3D模型繪制一朵小紅花的文章就介紹到這了,更多相關(guān)3D模型繪制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文教會(huì)你用Python3獲取網(wǎng)頁(yè)源代碼
學(xué)了python后,之前一些我們常用的方法,也可以換一種思路用python中的知識(shí)來(lái)解決,下面這篇文章主要給大家介紹了關(guān)于如何使用Python3獲取網(wǎng)頁(yè)源代碼的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
python 異常的傳遞性及主動(dòng)拋出學(xué)習(xí)
這篇文章主要為大家介紹了python 異常的傳遞性及主動(dòng)拋出學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
使用Anaconda3建立虛擬獨(dú)立的python2.7環(huán)境方法
今天小編就為大家分享一篇使用Anaconda3建立虛擬獨(dú)立的python2.7環(huán)境方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python selenium實(shí)現(xiàn)微博自動(dòng)登錄的示例代碼
本篇文章主要介紹了Python selenium實(shí)現(xiàn)微博自動(dòng)登錄的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python實(shí)現(xiàn)密碼薄文件讀寫(xiě)操作
這篇文章主要介紹了Python實(shí)現(xiàn)密碼薄文件讀寫(xiě)操作,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
python?操作?mongodb?數(shù)據(jù)庫(kù)詳情
這篇文章主要介紹了python?操作?mongodb?數(shù)據(jù)庫(kù)詳情,通過(guò)鏈接數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)庫(kù)展開(kāi)內(nèi)容詳細(xì),具有一定的參考價(jià)值,需要的的小伙伴可以參考一下2022-04-04
YOLOv5車(chē)牌識(shí)別實(shí)戰(zhàn)教程(五)字符分割與識(shí)別
這篇文章主要介紹了YOLOv5車(chē)牌識(shí)別實(shí)戰(zhàn)教程(五)字符分割與識(shí)別,在這個(gè)教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車(chē)牌識(shí)別,幫助你快速掌握YOLOv5車(chē)牌識(shí)別技能,需要的朋友可以參考下2023-04-04

