matplotlib?3D模型繪制一朵小紅花
前言:
在github上看到一個有趣的代碼,雖然情人節(jié)已經(jīng)過了兩天,但還是想和大家分享^_^
1. 含苞待放
3D模型的繪制需要網(wǎng)格點,關(guān)于網(wǎng)格點的作用,在基于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官方手冊。
到此這篇關(guān)于matplotlib 3D模型繪制一朵小紅花的文章就介紹到這了,更多相關(guān)3D模型繪制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python解線性矩陣方程(numpy中的matrix類)
這篇文章主要介紹了基于python解線性矩陣方程(numpy中的matrix類),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
舉例區(qū)分Python中的淺復(fù)制與深復(fù)制
這篇文章主要介紹了舉例區(qū)分Python中的淺復(fù)制與深復(fù)制,是Python入門學(xué)習(xí)中的重要知識,需要的朋友可以參考下2015-07-07
python pip配置國內(nèi)鏡像源的方法(永久和臨時)
在使用 pip 安裝 Python 模塊時,默認(rèn)的國外鏡像源可能會導(dǎo)致下載速度緩慢甚至超時,為了解決這個問題,可以使用國內(nèi)的鏡像源來加速下載,以下是常用的國內(nèi)鏡像源以及臨時和永久的配置方法,需要的朋友可以參考下2025-04-04
Python 正則表達式 re.match/re.search/re.sub的使用解析
今天小編就為大家分享一篇Python 正則表達式 re.match/re.search/re.sub的使用解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

