網(wǎng)站首頁 編程語言 正文
什么是Matplotlib?
Matplotlib是Python中的一個(gè)庫,用于創(chuàng)建靜態(tài)和動(dòng)態(tài)動(dòng)畫,并使用其內(nèi)置函數(shù)繪制。它有很多內(nèi)置特性和內(nèi)置分析工具,用于分析任何圖形或圖表。
如果我們想繪制任何三維圖形,那么我們可以使用Matplotlib庫。當(dāng)我們有一個(gè)巨大的三維變量數(shù)據(jù)集,我們繪制它的圖形時(shí),它看起來非常分散,這被稱為3D散點(diǎn)圖。我們將使用Matplotlib的matplot3d工具包繪制三維圖形。
有一把斧頭。函數(shù),它接受坐標(biāo)X、Y和Z的數(shù)據(jù)集。
根據(jù)我們想要賦予三維圖的屬性,需要更多的論證。
首次創(chuàng)建Matplotlib時(shí),只考慮二維繪圖。大約在1.0版本發(fā)布時(shí),通過在Matplotlib的二維顯示器上分層一些三維圖表工具,創(chuàng)建了一個(gè)實(shí)用的(盡管相當(dāng)有限)三維數(shù)據(jù)可視化工具集。通過導(dǎo)入mplot3d工具包(它是基本Matplotlib安裝的一部分),三維圖表成為可能。
最簡單的三維圖是由(x,y,z)三元組的線或簇組成的散點(diǎn)圖。這些可以用斧頭生產(chǎn)。plot3D和ax。scatter3D函數(shù),很像之前呈現(xiàn)的更典型的二維圖表。它們的呼叫特征與二維對(duì)應(yīng)物非常相似。
為了在頁面上創(chuàng)建深度錯(cuò)覺,散射點(diǎn)的透明度已經(jīng)改變。
示例1:
# importing the necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
# generating random dataset
z = np.random.randint(80, size =(55))
x = np.random.randint(60, size =(55))
y = np.random.randint(64, size =(55))
# Creating figures for the plot
fig = plt.figure(figsize = (10, 7))
ax = plt.axes(projection ="3d")
# Creating a plot using the random datasets
ax.scatter3D(x, y, z, color = "red")
plt.title("3D scatter plot")
# display the plot
plt.show()
輸出:
解釋:
在上面的示例中,我們使用ax創(chuàng)建了三維繪圖。scatter()函數(shù)。我們最初已經(jīng)導(dǎo)入了所需的所有庫,如numpy、matplotlib和mpl_toolkits。然后,我們使用randInt()函數(shù)創(chuàng)建了隨機(jī)數(shù)的x、y和z坐標(biāo)的數(shù)據(jù)集。在那之后,我們使用了斧頭。scatter3D()函數(shù),并輸入x、y和z坐標(biāo),我們?yōu)辄c(diǎn)取紅色。最后,我們使用show()函數(shù)顯示繪圖。
示例2:
# importing the necessary libraries
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
# Creating random dataset
z = 4 * np.tan(np.random.randint(10, size =(500))) + np.random.randint(100, size =(500))
x = 4 * np.cos(z) + np.random.normal(size = 500)
y = 4 * np.sin(z) + 4 * np.random.normal(size = 500)
# Creating figure
fig = plt.figure(figsize = (16, 12))
ax = plt.axes(projection ="3d")
# Add x, and y gridlines for the figure
ax.grid(b = True, color ='blue',linestyle ='-.', linewidth = 0.5,alpha = 0.3)
# Creating the color map for the plot
my_cmap = plt.get_cmap('hsv')
# Creating the 3D plot
sctt = ax.scatter3D(x, y, z,alpha = 0.8,c = (x + y + z),cmap = my_cmap,marker ='^')
plt.title("3D scatter plot in Python")
ax.set_xlabel('X-axis', fontweight ='bold')
ax.set_ylabel('Y-axis', fontweight ='bold')
ax.set_zlabel('Z-axis', fontweight ='bold')
fig.colorbar(sctt, ax = ax, shrink = 0.6, aspect = 5)
# display the plot
plt.show()
輸出:
解釋:
在上面的代碼中,我們用函數(shù)ax繪制了三維圖。scatter3D()函數(shù)。我們生成了x、y和z坐標(biāo)的隨機(jī)數(shù)據(jù)集,并使用標(biāo)記“^”繪制了它們。我們使用set_label函數(shù)為各個(gè)軸提供標(biāo)簽。
原文鏈接:https://blog.csdn.net/std7879/article/details/127804598
相關(guān)推薦
- 2022-07-11 Python內(nèi)建屬性getattribute攔截器使用詳解_python
- 2022-08-02 詳解Python?NumPy中矩陣和通用函數(shù)的使用_python
- 2023-01-20 python-try-except:pass的用法及說明_python
- 2022-09-04 Docker-Compose?容器集群的快速編排方法_docker
- 2022-10-21 Go錯(cuò)誤和異常CGO?fallthrough處理教程詳解_Golang
- 2023-03-01 shell?sleep睡眠命令的具體使用_linux shell
- 2022-08-30 DOM節(jié)點(diǎn)對(duì)象 、獲取節(jié)點(diǎn)、節(jié)點(diǎn)屬性、動(dòng)態(tài)操作DOM節(jié)點(diǎn)、toList1.0/toList2.0、
- 2023-06-05 python文件編譯為pyc后運(yùn)行的實(shí)現(xiàn)步驟_python
- 最近更新
-
- 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)證過濾器
- Spring Security概述快速入門
- 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)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支