Python+Matplotlib實(shí)現(xiàn)繪制三維折線圖
1.0簡(jiǎn)介
三維圖像技術(shù)是現(xiàn)在國際最先進(jìn)的計(jì)算機(jī)展示技術(shù)之一,任何普通電腦只需要安裝一個(gè)插件,就可以在網(wǎng)絡(luò)瀏覽器中呈現(xiàn)三維的產(chǎn)品,不但逼真,而且可以動(dòng)態(tài)展示產(chǎn)品的組合過程,特別適合遠(yuǎn)程瀏覽。
立體圖視覺上層次分明色彩鮮艷,具有很強(qiáng)的視覺沖擊力,讓觀看的人駐景時(shí)間長,留下深刻的印象。立體圖給人以真實(shí)、栩栩如生,人物呼之欲出,有身臨其境的感覺,有很高的藝術(shù)欣賞價(jià)值。
今天我們就通過這篇文章來了解如何用python中的matplotlib庫繪制漂亮的三位論文圖吧!秀翻你的朋友!
2.0三維圖畫法與類型
首先要安裝Matplotlib庫可以使用pip:
pip install matplotlib
假設(shè)已經(jīng)安裝了matplotlib工具包。
利用matplotlib.figure.Figure創(chuàng)建一個(gè)圖框:
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
1、直線繪制(Line plots)
基本用法:ax.plot(x,y,z,label=' ')
代碼如下:
import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.add_subplot(projection='3d') theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r = z ** 2 + 1 x = r * np.sin(theta) y = r * np.cos(theta) ax.plot(x, y, z, label='parametric curve') ax.legend()
效果如下:
2、散點(diǎn)繪制(Scatter plots)
基本語法:
ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True, *args, *kwargs)
代碼大意為:
- xs,ys,zs:輸入數(shù)據(jù);
- s:scatter點(diǎn)的尺寸
- c:顏色,如c = 'r’就是紅色;
- depthshase:透明化,True為透明,默認(rèn)為True,F(xiàn)alse為不透明
- *args等為擴(kuò)展變量,如maker = ‘o’,則scatter結(jié)果為’o‘的形狀
示例代碼:
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np def randrange(n, vmin, vmax): ''' Helper function to make an array of random numbers having shape (n, ) with each number distributed Uniform(vmin, vmax). ''' return (vmax - vmin)*np.random.rand(n) + vmin fig = plt.figure() ax = fig.add_subplot(111, projection='3d') n = 100 # For each set of style and range settings, plot n random points in the box # defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh]. for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zlow, zhigh) ax.scatter(xs, ys, zs, c=c, marker=m) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()
效果:
3、線框圖(Wireframe plots)
基本用法:ax.plot_wireframe(X, Y, Z, *args, **kwargs)
- X,Y,Z:輸入數(shù)據(jù)
- rstride:行步長
- cstride:列步長
- rcount:行數(shù)上限
- ccount:列數(shù)上限
示例代碼:
from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(100, projection='3d') # Grab some test data. X, Y, Z = axes3d.get_test_data(0.12) # Plot a basic wireframe. ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) plt.show()
有點(diǎn)丑。。。大家可以自行更改繪圖數(shù)據(jù),美化圖像。
4、三角表面圖(Tri-Surface plots)
基本用法:ax.plot_trisurf(*args, **kwargs)
ax.plot_trisurf(*args, **kwargs)
X,Y,Z:數(shù)據(jù)
其他參數(shù)類似surface-plot
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np n_radii = 8 n_angles = 36 radii = np.linspace(0.125, 1.0, n_radii) angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False) angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) # points in the (x, y) plane. x = np.append(0, (radii*np.cos(angles)).flatten()) y = np.append(0, (radii*np.sin(angles)).flatten()) z = np.sin(-x*y) fig = plt.figure() ax = fig.add_subplot(projection='3d') ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True) plt.show()
運(yùn)行效果圖:
5.隨機(jī)散點(diǎn)圖
利用scatter生成隨機(jī)散點(diǎn)圖。
函數(shù)定義:
#函數(shù)定義
matplotlib.pyplot.scatter(x, y,
s=None, #散點(diǎn)的大小 array scalar
c=None, #顏色序列 array、sequency
marker=None, #點(diǎn)的樣式
cmap=None, #colormap 顏色樣式
norm=None, #歸一化 歸一化的顏色camp
vmin=None, vmax=None, #對(duì)應(yīng)上面的歸一化范圍
alpha=None, #透明度
linewidths=None, #線寬
verts=None, #
edgecolors=None, #邊緣顏色
data=None,
**kwargs
)
示例代碼:
import numpy as np import matplotlib.pyplot as plt #定義坐標(biāo)軸 fig4 = plt.figure() ax4 = plt.axes(projection='3d') #生成三維數(shù)據(jù) xx = np.random.random(20)*10-5 #取100個(gè)隨機(jī)數(shù),范圍在5~5之間 yy = np.random.random(20)*10-5 X, Y = np.meshgrid(xx, yy) Z = np.sin(np.sqrt(X**2+Y**2)) #作圖 ax4.scatter(X,Y,Z,alpha=0.3,c=np.random.random(400),s=np.random.randint(10,20,size=(20, 20))) #生成散點(diǎn).利用c控制顏色序列,s控制大小 plt.show()
效果:
到此這篇關(guān)于Python+Matplotlib實(shí)現(xiàn)繪制三維折線圖的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制三維折線圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)http服務(wù)器(http.server模塊傳參?接收參數(shù))實(shí)例
這篇文章主要為大家介紹了Python實(shí)現(xiàn)http服務(wù)器(http.server模塊傳參?接收參數(shù))實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Python性能加速器__slots__屬性優(yōu)化內(nèi)存使用實(shí)例探索
Python中的__slots__屬性是一個(gè)特殊的屬性,允許程序員顯式地定義類的屬性,提供了一種方式來優(yōu)化類的內(nèi)存占用和提高訪問速度,本文將深入探討__slots__屬性的概念、應(yīng)用和性能優(yōu)勢(shì)2024-01-01Python的Twisted框架上手前所必須了解的異步編程思想
Twisted是Python世界中人氣最高的framework之一,異步的工作模式使其名揚(yáng)天下,這里為大家總結(jié)了Python的Twisted框架上手前所必須了解的異步編程思想,需要的朋友可以參考下2016-05-05- 這篇文章主要介紹了在 Python 中借助日志記錄庫使用 Log4j,本文解釋了什么是 log4j,它是如何工作的,以及我們?yōu)槭裁匆褂盟?,需要的朋友可以參考?/div> 2023-07-07
python實(shí)現(xiàn)的自動(dòng)發(fā)送消息功能詳解
這篇文章主要介紹了python實(shí)現(xiàn)的自動(dòng)發(fā)送消息功能,涉及Python基于requests、itchat庫的數(shù)據(jù)請(qǐng)求與信息處理相關(guān)操作技巧,需要的朋友可以參考下2019-08-08python函數(shù)enumerate,operator和Counter使用技巧實(shí)例小結(jié)
這篇文章主要介紹了python函數(shù)enumerate,operator和Counter使用技巧,結(jié)合實(shí)例形式總結(jié)分析了python內(nèi)置函數(shù)enumerate,operator和Counter基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-02-02最新評(píng)論