Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖
前言
matplotlib實際上是一套面向對象的繪圖庫,它所繪制的圖表中的每個繪圖元素,例如線條Line2D、文字Text、刻度等在內(nèi)存中都有一個對象與之對應。
為了方便快速繪圖matplotlib通過pyplot模塊提供了一套和MATLAB類似的繪圖API,將眾多繪圖對象所構成的復雜結構隱藏在這套API內(nèi)部。我們只需要調(diào)用pyplot模塊所提供的函數(shù)就可以實現(xiàn)快速繪圖以及設置圖表的各種細節(jié)。pyplot模塊雖然用法簡單,但不適合在較大的應用程序中使用。
為了將面向對象的繪圖庫包裝成只使用函數(shù)的調(diào)用接口,pyplot模塊的內(nèi)部保存了當前圖表以及當前子圖等信息。當前的圖表和子圖可以使用plt.gcf()和plt.gca()獲得,分別表示"Get Current Figure"和"Get Current Axes"。在pyplot模塊中,許多函數(shù)都是對當前的Figure或Axes對象進行處理,比如說:
plt.plot()實際上會通過plt.gca()獲得當前的Axes對象ax,然后再調(diào)用ax.plot()方法實現(xiàn)真正的繪圖。
可以在Ipython中輸入類似"plt.plot??"的命令查看pyplot模塊的函數(shù)是如何對各種繪圖對象進行包裝的。
1 matplotlib繪制3D圖形
matplotlib可以繪制3D圖形,有的版本中不具備該模塊,可以進入python環(huán)境,輸入from mpl_toolkits.mplot3d import Axes3D進行測試,如果導入成功則可以,否則需要安裝matplotlib其他版本,這里我用的是2.0.2版本。
2 繪制3D畫面圖
2.1 源碼
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() # 創(chuàng)建3d圖形的兩種方式 # ax = Axes3D(fig) ax = fig.add_subplot(111, projection='3d') # X, Y value X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) X, Y = np.meshgrid(X, Y) # x-y 平面的網(wǎng)格 R = np.sqrt(X ** 2 + Y ** 2) # height value Z = np.sin(R) # rstride:行之間的跨度 cstride:列之間的跨度 # rcount:設置間隔個數(shù),默認50個,ccount:列的間隔個數(shù) 不能與上面兩個參數(shù)同時出現(xiàn) #vmax和vmin 顏色的最大值和最小值 ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow')) # zdir : 'z' | 'x' | 'y' 表示把等高線圖投射到哪個面 # offset : 表示等高線圖投射到指定頁面的某個刻度 ax.contourf(X,Y,Z,zdir='z',offset=-2) # 設置圖像z軸的顯示范圍,x、y軸設置方式相同 ax.set_zlim(-2,2) plt.show()
2.2 效果圖
3 繪制散點圖
3.1 源碼
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) x = np.arange(0, 200) y = np.arange(0, 100) x, y = np.meshgrid(x, y) z = np.random.randint(0, 200, size=(100, 200)) y3 = np.arctan2(x,y) ax.scatter(x, y, z, c=y3, marker='.', s=50, label='') plt.show()
3.2 效果圖
4 繪制多邊形
4.1 源碼
from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d.art3d import Poly3DCollection,Line3DCollection fig = plt.figure() ax = fig.gca(projection='3d') # 正文體頂點和面 verts = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, 1), (0, 1, 1), (1, 1, 1), (1, 0, 1)] faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [0, 3, 7, 4]] # 四面體頂點和面 # verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1)] # faces = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]] # 獲得每個面的頂點 poly3d = [[verts[vert_id] for vert_id in face] for face in faces] # print(poly3d) # 繪制頂點 x, y, z = zip(*verts) ax.scatter(x, y, z) # 繪制多邊形面 ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.3)) # 繪制對變形的邊 ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.5, linestyles=':')) # 設置圖形坐標范圍 ax.set_xlabel('X') ax.set_xlim3d(-0.5, 1.5) ax.set_ylabel('Y') ax.set_ylim3d(-0.5, 1.5) ax.set_zlabel('Z') ax.set_zlim3d(-0.5, 1.5) plt.show()
4.2 效果圖
5 三個方向有等高線的3D圖
5.1 源碼
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from mpl_toolkits.mplot3d import axes3d fig = plt.figure() ax = fig.gca(projection='3d') X, Y, Z = axes3d.get_test_data(0.05) ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.5,color='b') cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm) ax.set_xlabel('X') ax.set_xlim(-40, 40) ax.set_ylabel('Y') ax.set_ylim(-40, 40) ax.set_zlabel('Z') ax.set_zlim(-100, 100) plt.show()
5.2 效果圖
6 三維柱狀圖
6.1 源碼
import random import matplotlib as mpl import matplotlib.dates as mdates from mpl_toolkits.mplot3d import Axes3D mpl.rcParams['font.size'] = 10 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for z in [2011, 2012, 2013, 2014]: xs = range(1,13) ys = 1000 * np.random.rand(12) color = plt.cm.Set2(random.choice(range(plt.cm.Set2.N))) ax.bar(xs, ys, zs=z, zdir='y', color=color, alpha=0.8) ax.xaxis.set_major_locator(mpl.ticker.FixedLocator(xs)) ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(ys)) ax.set_xlabel('Month') ax.set_ylabel('Year') ax.set_zlabel('Sales Net [usd]') plt.show()
6.2 效果圖
7 補充圖
7.1 源碼
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm n_angles = 36 n_radii = 8 # An array of radii # Does not include radius r=0, this is to eliminate duplicate points radii = np.linspace(0.125, 1.0, n_radii) # An array of angles angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False) # Repeat all angles for each radius angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) # Convert polar (radii, angles) coords to cartesian (x, y) coords # (0, 0) is added here. There are no duplicate points in the (x, y) plane x = np.append(0, (radii * np.cos(angles)).flatten()) y = np.append(0, (radii * np.sin(angles)).flatten()) # Pringle surface z = np.sin(-x * y) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0.2) plt.show()
7.2 效果圖
說明:內(nèi)容太多,這里都是做了源碼和效果圖展示,記得在使用中導入import matplotlib.pyplot as plt,否則會報錯;對于import numpy as np模塊根據(jù)實際情況導入,如果沒有使用該模塊構造數(shù)據(jù)的,可以不導入。
總結
到此這篇關于Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖的文章就介紹到這了,更多相關matplotlib模塊數(shù)據(jù)可視化3D圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)分析之Matplotlib數(shù)據(jù)可視化
- Python?matplotlib數(shù)據(jù)可視化圖繪制
- python數(shù)據(jù)可視化matplotlib繪制折線圖示例
- Python數(shù)據(jù)分析應用之Matplotlib數(shù)據(jù)可視化詳情
- Python中的數(shù)據(jù)可視化matplotlib與繪圖庫模塊
- Python數(shù)據(jù)可視化之matplotlib.pyplot繪圖的基本參數(shù)詳解
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表
- Python Matplotlib數(shù)據(jù)可視化模塊使用詳解
相關文章
python 計算兩個列表的相關系數(shù)的實現(xiàn)
這篇文章主要介紹了python 計算兩個列表的相關系數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Python CategoricalDtype自定義排序實現(xiàn)原理解析
這篇文章主要介紹了Python CategoricalDtype自定義排序實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09利用Tensorflow構建和訓練自己的CNN來做簡單的驗證碼識別方式
今天小編就為大家分享一篇利用Tensorflow構建和訓練自己的CNN來做簡單的驗證碼識別方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01