最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Matplotlib?3D?繪制小紅花原理

 更新時(shí)間:2022年02月16日 11:25:31   作者:夏小悠  
這篇文章主要介紹了Matplotlib?3D?繪制小紅花原理,小編上一篇文章一家介紹了繪制小紅化,本篇博文主要介紹一下3D小紅花的繪制原理,看過上篇博文的朋友可以參考一下

 前言:

上篇博文中使用了matplotlib繪制了3D小紅花,本篇博客主要介紹一下3D小紅花的繪制原理。

1. 極坐標(biāo)系

對(duì)于極坐標(biāo)系中的一點(diǎn) P ,我們可以用極徑 r  和極角 θ 來表示,記為點(diǎn) P ( r , θ ) ,

使用matplotlib繪制極坐標(biāo)系:

import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
? ? # 極徑
? ? r = np.arange(10)
? ? # 角度
? ? theta = 0.5 * np.pi * r

? ? fig = plt.figure()
? ? plt.polar(theta, r, c='r', marker='o', ms=3, ls='-', lw=1)
? ? # plt.savefig('img/polar1.png')
? ? plt.show()

使用matplotlib繪制極坐標(biāo)散點(diǎn)圖:

import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
?? ?r = np.linspace(0, 10, num=10)
? ? theta = 2 * np.pi * r
? ? area = 3 * r ** 2

? ? ax = plt.subplot(111, projection='polar')
? ? ax.scatter(theta, r, c=theta, s=area, cmap='hsv', alpha=0.75)
? ? # plt.savefig('img/polar2.png')
? ? plt.show()

有關(guān)matplotlib極坐標(biāo)的參數(shù)更多介紹,可參閱官網(wǎng)手冊(cè)。

2. 極坐標(biāo)系花瓣

繪制r = s i n ( θ ) r=sin(\theta)r=sin(θ) 在極坐標(biāo)系下的圖像:

import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
?? ?fig = plt.figure()
? ? ax = plt.subplot(111, projection='polar')
? ? ax.set_rgrids(radii=np.linspace(-1, 1, num=5), labels='')

? ? theta = np.linspace(0, 2 * np.pi, num=200)
? ? r = np.sin(theta)
? ? ax.plot(theta, r)
? ? # plt.savefig('img/polar3.png')
? ? plt.show()

以 2 π 為一個(gè)周期,增加圖像的旋轉(zhuǎn)周期:

r = np.sin(2 * theta)

繼續(xù)增加圖像的旋轉(zhuǎn)周期:

r = np.sin(3 * theta)
r = np.sin(4 * theta)

然后我們可以通過調(diào)整極徑系數(shù)和角度系數(shù)來調(diào)整圖像:

import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
?? ?fig = plt.figure()
? ? ax = plt.subplot(111, projection='polar')
? ? ax.set_rgrids(radii=np.linspace(-1, 1, num=5), labels='')

? ? theta = np.linspace(0, 2 * np.pi, num=200)
? ? r1 = np.sin(4 * (theta + np.pi / 8))
? ? r2 = 0.5 * np.sin(5 * theta)
? ? r3 = 2 * np.sin(6 * (theta + np.pi / 12))

? ? ax.plot(theta, r1)
? ? ax.plot(theta, r2)
? ? ax.plot(theta, r3)
? ? # plt.savefig('img/polar4.png')
? ? plt.show()

3. 三維花瓣

現(xiàn)在可以將花瓣放置在三維空間上了,根據(jù)花瓣的生成規(guī)律,其花瓣外邊緣線在一條旋轉(zhuǎn)內(nèi)縮的曲線上,這條曲線的極徑 r 隨著角度的增大逐漸變小,其高度 h  逐漸變大。

其函數(shù)圖像如下:

這樣定義就滿足前面對(duì)花瓣外邊緣曲線的假設(shè)了,即 r 遞減, h 遞增。

現(xiàn)在將其放在三維空間中:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D


if __name__ == '__main__':
? ? fig = plt.figure()
? ? ax = Axes3D(fig)
? ? # plt.axis('off')

? ? x = np.linspace(0, 1, num=30)
? ? theta = np.linspace(0, 2 * np.pi, num=1200)
? ? theta = 30 * theta
? ? x, theta = np.meshgrid(x, theta)

? ? # f is a decreasing function of theta
? ? f = 0.5 * np.pi * np.exp(-theta / 50)

? ? r = x * np.sin(f)
? ? h = x * np.cos(f)

?? ?# 極坐標(biāo)轉(zhuǎn)笛卡爾坐標(biāo)
? ? X = r * np.cos(theta)
? ? Y = r * np.sin(theta)
? ? ax = ax.plot_surface(X, Y, h,
? ? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.cool)

? ? # plt.savefig('img/polar5.png')
? ? plt.show()

笛卡爾坐標(biāo)系(Cartesian coordinate system),即直角坐標(biāo)系。

然而,上述的表達(dá)仍然沒有得到花瓣的細(xì)節(jié),因此我們需要在此基礎(chǔ)之上進(jìn)行處理,以得到花瓣形狀。因此設(shè)計(jì)了一個(gè)花瓣函數(shù):

其是一個(gè)以 2 π 為周期的周期函數(shù),其值域?yàn)閇 0.5 , 1.0 ],圖像如下圖所示:

再次繪制:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D


if __name__ == '__main__':
?? ?fig = plt.figure()
? ? ax = Axes3D(fig)
? ? # plt.axis('off')

? ? x = np.linspace(0, 1, num=30)
? ? theta = np.linspace(0, 2 * np.pi, num=1200)
? ? theta = 30 * theta
? ? x, theta = np.meshgrid(x, theta)

? ? # f is a decreasing function of theta
? ? f = 0.5 * np.pi * np.exp(-theta / 50)

?? ?# 通過改變函數(shù)周期來改變花瓣的形狀
?? ?# 改變值域也可以改變花瓣形狀
?? ?# u is a periodic function
? ? u = 1 - (1 - np.absolute(np.sin(3.3 * theta / 2))) / 2
? ? r = x * u * np.sin(f)
? ? h = x * u * np.cos(f)
?? ?
?? ?# 極坐標(biāo)轉(zhuǎn)笛卡爾坐標(biāo)
? ? X = r * np.cos(theta)
? ? Y = r * np.sin(theta)
? ? ax = ax.plot_surface(X, Y, h,
? ? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.RdPu_r)

? ? # plt.savefig('img/polar6.png')
? ? plt.show()

4. 花瓣微調(diào)

  為了使花瓣更加真實(shí),使花瓣的形態(tài)向下凹,因此需要對(duì)花瓣的形狀進(jìn)行微調(diào),這里添加一個(gè)修正項(xiàng)和一個(gè)噪聲擾動(dòng),修正函數(shù)圖像為:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D


if __name__ == '__main__':
?? ?fig = plt.figure()
? ? ax = Axes3D(fig)
? ? # plt.axis('off')

? ? x = np.linspace(0, 1, num=30)
? ? theta = np.linspace(0, 2 * np.pi, num=1200)
? ? theta = 30 * theta
? ? x, theta = np.meshgrid(x, theta)

? ? # f is a decreasing function of theta
? ? f = 0.5 * np.pi * np.exp(-theta / 50)

? ? noise = np.sin(theta) / 30
? ? # u is a periodic function
? ? u = 1 - (1 - np.absolute(np.sin(3.3 * theta / 2))) / 2 + noise

? ? # y is a correction function
? ? y = 2 * (x ** 2 - x) ** 2 * np.sin(f)
? ? r = u * (x * np.sin(f) + y * np.cos(f))
? ? h = u * (x * np.cos(f) - y * np.sin(f))

? ? X = r * np.cos(theta)
? ? Y = r * np.sin(theta)
? ? ax = ax.plot_surface(X, Y, h,
? ? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.RdPu_r)

? ? # plt.savefig('img/polar7.png')
? ? plt.show()

修正前后圖像區(qū)別對(duì)比如下:

5. 結(jié)束語

3D花的繪制主要原理是極坐標(biāo),通過正弦/余弦函數(shù)進(jìn)行旋轉(zhuǎn)變形構(gòu)造,參數(shù)略微變化就會(huì)出現(xiàn)不同的花朵,有趣!

到此這篇關(guān)于Matplotlib 3D 繪制小紅花原理的文章就介紹到這了,更多相關(guān)Matplotlib 繪制小紅花內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python腳本,標(biāo)識(shí)符,變量使用,腳本語句,注釋,模塊引用詳解

    Python腳本,標(biāo)識(shí)符,變量使用,腳本語句,注釋,模塊引用詳解

    這篇文章主要為大家詳細(xì)介紹了Python腳本,標(biāo)識(shí)符,變量使用,腳本語句,注釋,模塊引用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python httpx庫入門指南(最新推薦)

    Python httpx庫入門指南(最新推薦)

    Httpx 是一個(gè)用于發(fā)送 HTTP 請(qǐng)求的 Python 庫,它提供了簡單易用的 API,可以輕松地發(fā)送 GET、POST、PUT、DELETE 等請(qǐng)求,并接收響應(yīng),下面介紹下Python httpx庫入門指南,感興趣的朋友一起看看吧
    2023-12-12
  • 解決python讀取幾千萬行的大表內(nèi)存問題

    解決python讀取幾千萬行的大表內(nèi)存問題

    今天小編就為大家分享一篇解決python讀取幾千萬行的大表內(nèi)存問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python中pyenv-win安裝與使用教程

    python中pyenv-win安裝與使用教程

    pyenv-win是一個(gè)在Windows系統(tǒng)上管理Python版本的工具,本文主要介紹了python中pyenv-win安裝與使用教程,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • OpenCV?NAO機(jī)器人輔助撿球丟球流程分析

    OpenCV?NAO機(jī)器人輔助撿球丟球流程分析

    這篇文章主要介紹了OpenCV?NAO機(jī)器人輔助撿球丟球,本項(xiàng)目使用NAO機(jī)器人識(shí)別球并撿起,然后將其扔到指定位置,主要涉及圖像的獲取、濾波、目標(biāo)物體定位和NAO機(jī)器人的運(yùn)動(dòng)控制,需要的朋友可以參考下
    2022-05-05
  • Python中__new__與__init__方法的區(qū)別詳解

    Python中__new__與__init__方法的區(qū)別詳解

    這篇文章主要介紹了Python中__new__與__init__方法的區(qū)別,是Python學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Python Pytorch深度學(xué)習(xí)之核心小結(jié)

    Python Pytorch深度學(xué)習(xí)之核心小結(jié)

    今天小編就為大家分享一篇關(guān)于Pytorch核心小結(jié)的文章,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-10-10
  • 如何使用python統(tǒng)計(jì)字符在文件中出現(xiàn)的次數(shù)

    如何使用python統(tǒng)計(jì)字符在文件中出現(xiàn)的次數(shù)

    在開發(fā)過程中很多時(shí)候我們有統(tǒng)計(jì)單個(gè)字符或者字符串在另一個(gè)字符串中出現(xiàn)次數(shù)的需求,下面這篇文章主要給大家介紹了關(guān)于如何使用python統(tǒng)計(jì)字符在文件中出現(xiàn)的次數(shù)的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • python中字符串String及其常見操作指南(方法、函數(shù))

    python中字符串String及其常見操作指南(方法、函數(shù))

    String方法是用來處理代碼中的字符串的,它幾乎能搞定你所遇到的所有字符串格式,下面這篇文章主要給大家介紹了關(guān)于python中字符串String及其常見操作(方法、函數(shù))的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • python numpy中multiply與*及matul 的區(qū)別說明

    python numpy中multiply與*及matul 的區(qū)別說明

    這篇文章主要介紹了python numpy中multiply與*及matul 的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論

晋江市| 乌兰浩特市| 云霄县| 喀喇| 宜阳县| 珲春市| 潼南县| 阜宁县| 兴安县| 汶川县| 大庆市| 新泰市| 湘潭市| 龙川县| 清河县| 翼城县| 朝阳县| 霍邱县| 进贤县| 富阳市| 翁牛特旗| 昆山市| 清水河县| 泸定县| 京山县| 班戈县| 慈利县| 凤阳县| 阿拉善左旗| 莲花县| 浦县| 凤翔县| 台南市| 大方县| 临沭县| 卓资县| 宣威市| 方城县| 阜平县| 洪雅县| 如皋市|