溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用Python+Matplotlib繪制三維折線圖

發(fā)布時(shí)間:2023-03-21 11:13:36 來(lái)源:億速云 閱讀:126 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了怎么用Python+Matplotlib繪制三維折線圖的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇怎么用Python+Matplotlib繪制三維折線圖文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

1.0簡(jiǎn)介

三維圖像技術(shù)是現(xiàn)在國(guó)際最先進(jìn)的計(jì)算機(jī)展示技術(shù)之一,任何普通電腦只需要安裝一個(gè)插件,就可以在網(wǎng)絡(luò)瀏覽器中呈現(xiàn)三維的產(chǎn)品,不但逼真,而且可以動(dòng)態(tài)展示產(chǎn)品的組合過(guò)程,特別適合遠(yuǎn)程瀏覽。

立體圖視覺(jué)上層次分明色彩鮮艷,具有很強(qiáng)的視覺(jué)沖擊力,讓觀看的人駐景時(shí)間長(zhǎng),留下深刻的印象。立體圖給人以真實(shí)、栩栩如生,人物呼之欲出,有身臨其境的感覺(jué),有很高的藝術(shù)欣賞價(jià)值。

2.0三維圖畫(huà)法與類(lèi)型

首先要安裝Matplotlib庫(kù)可以使用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')

怎么用Python+Matplotlib繪制三維折線圖

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()

效果如下:

怎么用Python+Matplotlib繪制三維折線圖

2、散點(diǎn)繪制(Scatter plots)

基本語(yǔ)法:

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)為T(mén)rue,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()

效果:

怎么用Python+Matplotlib繪制三維折線圖

3、線框圖(Wireframe plots)

基本用法:ax.plot_wireframe(X, Y, Z, *args, **kwargs)

  • X,Y,Z:輸入數(shù)據(jù)

  • rstride:行步長(zhǎng)

  • cstride:列步長(zhǎng)

  • 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()

怎么用Python+Matplotlib繪制三維折線圖

4、三角表面圖(Tri-Surface plots)

基本用法:ax.plot_trisurf(*args, **kwargs)

ax.plot_trisurf(*args, **kwargs)

X,Y,Z:數(shù)據(jù)

其他參數(shù)類(lèi)似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)行效果圖:

怎么用Python+Matplotlib繪制三維折線圖

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()

效果:

怎么用Python+Matplotlib繪制三維折線圖

關(guān)于“怎么用Python+Matplotlib繪制三維折線圖”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么用Python+Matplotlib繪制三維折線圖”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI