三維圖像技術是現在國際最先進的電腦展示技術之一,任何普通電腦只需要安裝一個插件,就可以在網路瀏覽器中呈現三維的產品,不但逼真,而且可以動態展示產品的組合過程,特別適合遠端瀏覽。
立體圖視覺上層次分明色彩鮮豔,具有強烈的視覺衝擊力,讓觀看的人駐景時間長,留下深刻的印象。立體圖給人真實、栩栩如生,人物呼之欲出,有身臨其境的感覺,有很高的藝術欣賞價值。
首先要安裝Matplotlib函式庫可以使用pip:
pip install matplotlib
假設已經安裝了matplotlib工具包。
利用matplotlib.figure.Figure建立一個圖框:
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
#基本用法: 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 、散點繪製(Scatter plots)基本語法:ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True, *args , *kwargs)程式碼大意為:
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)
範例程式碼:
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()
4、三角表面圖(Tri-Surface plots)
基本用法:ax.plot_trisurf(*args, **kwargs)
ax.plot_trisurf(*args, **kwargs)
X,Y,Z:資料
其他參數類似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()
運行效果圖:
#5.隨機散佈圖
##利用scatter產生隨機散佈圖。 函數定義:範例程式碼:
#函數定義
matplotlib.pyplot.scatter(x, y,
s=None, #散佈點的大小array scalar
# c=None, #顏色序列 array、sequency
marker=None, #點的樣式
cmap=None, #colormap 顏色樣式
vmin=None, vmax=None, #對應上面的歸一化範圍
alpha=None, #透明度
linewidths =None, #邊緣顏色
data=None,
**kwargs
)
import numpy as np import matplotlib.pyplot as plt #定义坐标轴 fig4 = plt.figure() ax4 = plt.axes(projection='3d') #生成三维数据 xx = np.random.random(20)*10-5 #取100个随机数,范围在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))) #生成散点.利用c控制颜色序列,s控制大小 plt.show()效果:
範例程式碼:
rrreee###效果:####### ######以上是如何使用Python和Matplotlib建立三維折線圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!