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

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

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

 前言:

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

1. 極坐標系

對于極坐標系中的一點 P ,我們可以用極徑 r  和極角 θ 來表示,記為點 P ( r , θ ) ,

使用matplotlib繪制極坐標系:

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繪制極坐標散點圖:

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極坐標的參數(shù)更多介紹,可參閱官網(wǎng)手冊。

2. 極坐標系花瓣

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

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 π 為一個周期,增加圖像的旋轉(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ù)圖像如下:

這樣定義就滿足前面對花瓣外邊緣曲線的假設(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)

?? ?# 極坐標轉(zhuǎn)笛卡爾坐標
? ? 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()

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

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

其是一個以 2 π 為周期的周期函數(shù),其值域為[ 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)
?? ?
?? ?# 極坐標轉(zhuǎn)笛卡爾坐標
? ? 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)

  為了使花瓣更加真實,使花瓣的形態(tài)向下凹,因此需要對花瓣的形狀進行微調(diào),這里添加一個修正項和一個噪聲擾動,修正函數(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ū)別對比如下:

5. 結(jié)束語

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

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

相關(guān)文章

  • Python腳本,標識符,變量使用,腳本語句,注釋,模塊引用詳解

    Python腳本,標識符,變量使用,腳本語句,注釋,模塊引用詳解

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

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

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

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

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

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

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

    OpenCV?NAO機器人輔助撿球丟球流程分析

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

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

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

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

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

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

    在開發(fā)過程中很多時候我們有統(tǒng)計單個字符或者字符串在另一個字符串中出現(xiàn)次數(shù)的需求,下面這篇文章主要給大家介紹了關(guān)于如何使用python統(tǒng)計字符在文件中出現(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ū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評論

泽州县| 同心县| 横山县| 鄂尔多斯市| 留坝县| 延边| 双流县| 吉安县| 华蓥市| 临海市| 临海市| 东阿县| 马公市| 二手房| 武义县| 新田县| 射洪县| 禄丰县| 通化市| 凉城县| 通榆县| 石楼县| 曲沃县| 沂源县| 济阳县| 商水县| 无锡市| 虎林市| 安国市| 林周县| 磴口县| 偃师市| 保康县| 扎赉特旗| 石嘴山市| 五峰| 闽清县| 扎鲁特旗| 隆林| 仲巴县| 靖安县|