matplotlib在python上繪制3D散點圖實例詳解
更新時間:2017年12月09日 10:45:21 作者:wise南迦
這篇文章主要介紹了matplotlib在python上繪制3D散點圖實例詳解,首先介紹了官網(wǎng)的實例,然后分享了本文簡單代碼示例,具有一定借鑒價值,需要的朋友可以了解下。
大家可以先參考官方演示文檔:
效果圖:
''' ============== 3D scatterplot ============== Demonstration of a basic scatterplot in 3D. ''' from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np 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 c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zlow, zhigh) ax.scatter(xs, ys, zs, c=c, marker=m) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()
以上是官網(wǎng)上的代碼示例及演示結果,下面分享下本文代碼示例。
本實例需要導入第三包:
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D
然后繪圖:
ax = plt.figure().add_subplot(111, projection = '3d') #基于ax變量繪制三維圖 #xs表示x方向的變量 #ys表示y方向的變量 #zs表示z方向的變量,這三個方向上的變量都可以用list的形式表示 #m表示點的形式,o是圓形的點,^是三角形(marker) #c表示顏色(color for short) ax.scatter(xs, ys, zs, c = 'r', marker = '^') #點為紅色三角形 #設置坐標軸 ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') #顯示圖像 plt.show()
注:
上面的
ax = plt.figure().add_subplot(111, projection = '3d')
是下面代碼的略寫
fig = plt.figure() ax = fig.add_subplot(111, projection = '3d')
總結
以上就是本文關于matplotlib在python上繪制3D散點圖實例詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Python Multiprocessing多進程 使用tqdm顯示進度條的實現(xiàn)
這篇文章主要介紹了Python Multiprocessing多進程 使用tqdm顯示進度條的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08對Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解
今天小編就為大家分享一篇對Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python Numpy教程之排序,搜索和計數(shù)詳解
這篇文章主要為大家詳細介紹了Python?NumPy中排序,搜索和計數(shù)的實現(xiàn),文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考一下2022-08-08教你用python提取txt文件中的特定信息并寫入Excel
這篇文章主要給大家介紹了如何利用python提取txt文件中的特定信息并寫入Excel的相關資料,Python是一個強大的語言,解決這點問題非常簡單,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-11-11python 判斷網(wǎng)絡連通的實現(xiàn)方法
下面小編就為大家分享一篇python 判斷網(wǎng)絡連通的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04