Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖
前言
matplotlib實(shí)際上是一套面向?qū)ο蟮睦L圖庫(kù),它所繪制的圖表中的每個(gè)繪圖元素,例如線條Line2D、文字Text、刻度等在內(nèi)存中都有一個(gè)對(duì)象與之對(duì)應(yīng)。
為了方便快速繪圖matplotlib通過(guò)pyplot模塊提供了一套和MATLAB類似的繪圖API,將眾多繪圖對(duì)象所構(gòu)成的復(fù)雜結(jié)構(gòu)隱藏在這套API內(nèi)部。我們只需要調(diào)用pyplot模塊所提供的函數(shù)就可以實(shí)現(xiàn)快速繪圖以及設(shè)置圖表的各種細(xì)節(jié)。pyplot模塊雖然用法簡(jiǎn)單,但不適合在較大的應(yīng)用程序中使用。
為了將面向?qū)ο蟮睦L圖庫(kù)包裝成只使用函數(shù)的調(diào)用接口,pyplot模塊的內(nèi)部保存了當(dāng)前圖表以及當(dāng)前子圖等信息。當(dāng)前的圖表和子圖可以使用plt.gcf()和plt.gca()獲得,分別表示"Get Current Figure"和"Get Current Axes"。在pyplot模塊中,許多函數(shù)都是對(duì)當(dāng)前的Figure或Axes對(duì)象進(jìn)行處理,比如說(shuō):
plt.plot()實(shí)際上會(huì)通過(guò)plt.gca()獲得當(dāng)前的Axes對(duì)象ax,然后再調(diào)用ax.plot()方法實(shí)現(xiàn)真正的繪圖。
可以在Ipython中輸入類似"plt.plot??"的命令查看pyplot模塊的函數(shù)是如何對(duì)各種繪圖對(duì)象進(jìn)行包裝的。
1 matplotlib繪制3D圖形
matplotlib可以繪制3D圖形,有的版本中不具備該模塊,可以進(jìn)入python環(huán)境,輸入from mpl_toolkits.mplot3d import Axes3D進(jìn)行測(cè)試,如果導(dǎo)入成功則可以,否則需要安裝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è)置間隔個(gè)數(shù),默認(rèn)50個(gè),ccount:列的間隔個(gè)數(shù) 不能與上面兩個(gè)參數(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' 表示把等高線圖投射到哪個(gè)面
# offset : 表示等高線圖投射到指定頁(yè)面的某個(gè)刻度
ax.contourf(X,Y,Z,zdir='z',offset=-2)
# 設(shè)置圖像z軸的顯示范圍,x、y軸設(shè)置方式相同
ax.set_zlim(-2,2)
plt.show()2.2 效果圖

3 繪制散點(diǎn)圖
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')
# 正文體頂點(diǎn)和面
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]]
# 四面體頂點(diǎn)和面
# 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]]
# 獲得每個(gè)面的頂點(diǎn)
poly3d = [[verts[vert_id] for vert_id in face] for face in faces]
# print(poly3d)
# 繪制頂點(diǎn)
x, y, z = zip(*verts)
ax.scatter(x, y, z)
# 繪制多邊形面
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.3))
# 繪制對(duì)變形的邊
ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.5, linestyles=':'))
# 設(shè)置圖形坐標(biāo)范圍
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 三個(gè)方向有等高線的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 補(bǔ)充圖
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 效果圖

說(shuō)明:內(nèi)容太多,這里都是做了源碼和效果圖展示,記得在使用中導(dǎo)入import matplotlib.pyplot as plt,否則會(huì)報(bào)錯(cuò);對(duì)于import numpy as np模塊根據(jù)實(shí)際情況導(dǎo)入,如果沒(méi)有使用該模塊構(gòu)造數(shù)據(jù)的,可以不導(dǎo)入。
總結(jié)
到此這篇關(guān)于Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖的文章就介紹到這了,更多相關(guān)matplotlib模塊數(shù)據(jù)可視化3D圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)分析之Matplotlib數(shù)據(jù)可視化
- Python?matplotlib數(shù)據(jù)可視化圖繪制
- python數(shù)據(jù)可視化matplotlib繪制折線圖示例
- Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情
- Python中的數(shù)據(jù)可視化matplotlib與繪圖庫(kù)模塊
- Python數(shù)據(jù)可視化之matplotlib.pyplot繪圖的基本參數(shù)詳解
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡(jiǎn)單圖表
- Python Matplotlib數(shù)據(jù)可視化模塊使用詳解
相關(guān)文章
OpenCV-Python實(shí)現(xiàn)輪廓擬合
本文將結(jié)合實(shí)例代碼,介紹OpenCV-Python實(shí)現(xiàn)輪廓擬合,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
Python中使用裝飾器來(lái)優(yōu)化尾遞歸的示例
這里我們用典型的斐波那契數(shù)列作為例子,來(lái)展示Python中使用裝飾器來(lái)優(yōu)化尾遞歸的示例,需要的朋友可以參考下2016-06-06
基于Python實(shí)現(xiàn)一鍵獲取電腦瀏覽器的賬號(hào)密碼
發(fā)現(xiàn)很多人在學(xué)校圖書館喜歡用電腦占座,而且出去的時(shí)候經(jīng)常不鎖屏,為了讓大家養(yǎng)成良好的習(xí)慣,本文將分享一個(gè)小程序,可以快速獲取你存儲(chǔ)在電腦瀏覽器中的所有賬號(hào)和密碼,感興趣的可以了解一下2022-05-05
python目標(biāo)檢測(cè)非極大抑制NMS與Soft-NMS
這篇文章主要weidajia?介紹了python目標(biāo)檢測(cè)非極大抑制NMS與Soft-NMS實(shí)現(xiàn)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python實(shí)現(xiàn)提取或替換PPT中文本與圖片的示例代碼
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)提取保存ppt中的圖片和替換ppt模板的文本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01
WIn10+Anaconda環(huán)境下安裝PyTorch(避坑指南)
這篇文章主要介紹了WIn10+Anaconda環(huán)境下安裝PyTorch(避坑指南),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python matplotlib繪圖建立畫布及坐標(biāo)系
這篇文章主要介紹了Python matplotlib繪圖建立畫布及坐標(biāo)系,建立畫布 figsize,它用width和height來(lái)控制畫布的寬和高,下面來(lái)一起倆姐更多內(nèi)容吧2021-12-12
python基于win32實(shí)現(xiàn)窗口截圖
這篇文章主要為大家詳細(xì)介紹了python基于win32api實(shí)現(xiàn)窗口截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03

