網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.散點(diǎn)圖
代碼
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
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 m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
輸出:
輸入的數(shù)據(jù)格式
這個(gè)輸入的三個(gè)維度要求是三列長(zhǎng)度一致的數(shù)據(jù),可以理解為3個(gè)length相等的list。
用上面的scatter或者下面這段直接plot也可以。
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(h, z, t, '.', alpha=0.5)
plt.show()
輸出:
2.三維表面 surface
代碼
x = [12.7, 12.8, 12.9]
y = [1, 2, 3, 4]
temp = pd.DataFrame([[7,7,9,9],[2,3,4,5],[1,6,8,7]]).T
X,Y = np.meshgrid(x,y) # 形成網(wǎng)格化的數(shù)據(jù)
temp = np.array(temp)
fig = plt.figure(figsize=(16, 16))
ax = fig.gca(projection='3d')
ax.plot_surface(Y,X,temp,rcount=1, cmap=cm.plasma, linewidth=1, antialiased=False,alpha=0.5) #cm.plasma
ax.set_xlabel('zone', color='b', fontsize=20)
ax.set_ylabel('h2o', color='g', fontsize=20)
ax.set_zlabel('Temperature', color='r', fontsize=20)
output:
輸入的數(shù)據(jù)格式
這里x和y原本都是一維list,通過(guò)np.meshgrid可以將其形成4X3的二維數(shù)據(jù),如下圖所示:
而第三維,得是4X3的2維的數(shù)據(jù),才能進(jìn)行畫(huà)圖
scatter + surface圖形展示
3. 三維瀑布圖waterfall
代碼
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
axes=plt.axes(projection="3d")
def colors(arg):
return mcolors.to_rgba(arg, alpha=0.6)
verts = []
z1 = [1, 2, 3, 4]
x1 = np.arange(0, 10, 0.4)
for z in z1:
y1 = np.random.rand(len(x1))
y1[0], y1[-1] = 0, 0
verts.append(list(zip(x1, y1)))
# print(verts)
poly = PolyCollection(verts, facecolors=[colors('r'), colors('g'), colors('b'),
colors('y')])
poly.set_alpha(0.7)
axes.add_collection3d(poly, zs=z1, zdir='y')
axes.set_xlabel('X')
axes.set_xlim3d(0, 10)
axes.set_ylabel('Y')
axes.set_ylim3d(-1, 4)
axes.set_zlabel('Z')
axes.set_zlim3d(0, 1)
axes.set_title("3D Waterfall plot")
plt.show()
輸出:
輸入的數(shù)據(jù)格式
這個(gè)的輸入我還沒(méi)有完全搞懂,導(dǎo)致我自己暫時(shí)不能復(fù)現(xiàn)到其他數(shù)據(jù),等以后懂了再回來(lái)補(bǔ)充。
4. 3d wireframe
code
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(
2, 1, figsize=(8, 12), subplot_kw={'projection': '3d'})
# Get the test data
X, Y, Z = axes3d.get_test_data(0.05)
# Give the first plot only wireframes of the type y = c
ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=0)
ax1.set_title("Column (x) stride set to 0")
# Give the second plot only wireframes of the type x = c
ax2.plot_wireframe(X, Y, Z, rstride=0, cstride=10)
ax2.set_title("Row (y) stride set to 0")
plt.tight_layout()
plt.show()
output:
輸入的數(shù)據(jù)格式
與plot_surface的輸入格式一樣,X,Y原本為一維list,通過(guò)np.meshgrid形成網(wǎng)格化數(shù)據(jù)。Z為二維數(shù)據(jù)。其中注意調(diào)節(jié)rstride、cstride這兩個(gè)值實(shí)現(xiàn)行列間隔的調(diào)整。
自己試了下:
原文鏈接:https://blog.csdn.net/weixin_46870583/article/details/125318214
相關(guān)推薦
- 2022-08-04 淺析.net?core?拋異常對(duì)性能影響_實(shí)用技巧
- 2022-07-11 pandas實(shí)現(xiàn)按照多列排序-ascending_python
- 2022-12-29 Android開(kāi)發(fā)中用Kotlin編寫(xiě)LiveData組件教程_Android
- 2022-06-19 python繪制橫豎條形圖的方法_python
- 2023-08-13 element表單組件的trigger表單驗(yàn)證邏輯規(guī)則
- 2022-05-27 .Net動(dòng)態(tài)生成controller遇到的坑_實(shí)用技巧
- 2023-11-19 urllib2.HTTPError: HTTP Error 403: Forbidden; in h
- 2022-10-13 Go?Excelize?API源碼閱讀SetSheetViewOptions示例解析_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支