Python使用Matplotlib繪制三維散點圖詳解流程
什么是Matplotlib?
Matplotlib是Python中的一個庫,用于創(chuàng)建靜態(tài)和動態(tài)動畫,并使用其內置函數(shù)繪制。它有很多內置特性和內置分析工具,用于分析任何圖形或圖表。
如果我們想繪制任何三維圖形,那么我們可以使用Matplotlib庫。當我們有一個巨大的三維變量數(shù)據(jù)集,我們繪制它的圖形時,它看起來非常分散,這被稱為3D散點圖。我們將使用Matplotlib的matplot3d工具包繪制三維圖形。
有一把斧頭。函數(shù),它接受坐標X、Y和Z的數(shù)據(jù)集。
根據(jù)我們想要賦予三維圖的屬性,需要更多的論證。
首次創(chuàng)建Matplotlib時,只考慮二維繪圖。大約在1.0版本發(fā)布時,通過在Matplotlib的二維顯示器上分層一些三維圖表工具,創(chuàng)建了一個實用的(盡管相當有限)三維數(shù)據(jù)可視化工具集。通過導入mplot3d工具包(它是基本Matplotlib安裝的一部分),三維圖表成為可能。
最簡單的三維圖是由(x,y,z)三元組的線或簇組成的散點圖。這些可以用斧頭生產。plot3D和ax。scatter3D函數(shù),很像之前呈現(xiàn)的更典型的二維圖表。它們的呼叫特征與二維對應物非常相似。
為了在頁面上創(chuàng)建深度錯覺,散射點的透明度已經(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)導入了所需的所有庫,如numpy、matplotlib和mpl_toolkits。然后,我們使用randInt()函數(shù)創(chuàng)建了隨機數(shù)的x、y和z坐標的數(shù)據(jù)集。在那之后,我們使用了斧頭。scatter3D()函數(shù),并輸入x、y和z坐標,我們?yōu)辄c取紅色。最后,我們使用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坐標的隨機數(shù)據(jù)集,并使用標記“^”繪制了它們。我們使用set_label函數(shù)為各個軸提供標簽。
到此這篇關于Python使用Matplotlib繪制三維散點圖詳解流程的文章就介紹到這了,更多相關Python繪制三維散點圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決import tensorflow as tf 出錯的原因
這篇文章主要介紹了解決import tensorflow as tf 出錯的原因,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Windows下使Python2.x版本的解釋器與3.x共存的方法
這篇文章主要介紹了Windows下使Python2.x版本的解釋器與3.x共存的方法,命令行中調用起來很方便,需要的朋友可以參考下2015-10-10
python使用nibabel和sitk讀取保存nii.gz文件實例
這篇文章主要介紹了python使用nibabel和sitk讀取保存nii.gz文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
django通過ajax發(fā)起請求返回JSON格式數(shù)據(jù)的方法
這篇文章主要介紹了django通過ajax發(fā)起請求返回JSON格式數(shù)據(jù)的方法,較為詳細的分析了django處理ajax請求的技巧,需要的朋友可以參考下2015-06-06

