1. 程式人生 > >Python篇:三維散點圖scatter介紹

Python篇:三維散點圖scatter介紹

 

 

##畫個簡單三維圖
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')
  #當scatter後面引數中陣列的使用方法,如s,當s是同x大小的陣列,表示x中的每個點對應s中一個大小,其他如c,等用法一樣,如:
  #當scatter,如s,屬性s是設定散點大小
#(1)不同大小
#匯入必要的模組 
import numpy as np 
import matplotlib.pyplot as plt 
#產生測試資料 
x = np.arange(1,10) 
y = x 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
#設定標題 
ax1.set_title('Scatter Plot') 
#設定X軸標籤 
plt.xlabel('X') 
#設定Y軸標籤 
plt.ylabel('Y') 
#畫散點圖 
sValue = x*10 
ax1.scatter(x,y,s=sValue,c='r',marker='x') 
#設定圖示 
plt.legend('x1') 
#顯示所畫的圖 
plt.show()  
#(2)不同顏色
#匯入必要的模組 
import numpy as np 
import matplotlib.pyplot as plt 
#產生測試資料 
x = np.arange(1,10) 
y = x 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
#設定標題 
ax1.set_title('Scatter Plot') 
#設定X軸標籤 
plt.xlabel('X') 
#設定Y軸標籤 
plt.ylabel('Y') 
#畫散點圖 
cValue = ['r','y','g','b','r','y','g','b','r'] 
ax1.scatter(x,y,c=cValue,marker='s') 
#設定圖示 
plt.legend('x1') 
#顯示所畫的圖 
plt.show()  
(3)、線寬linewidths
#匯入必要的模組 
import numpy as np 
import matplotlib.pyplot as plt 
#產生測試資料 
x = np.arange(1,10) 
y = x 
fig = plt.figure() 
ax1 = fig.add_subplot(111) 
#設定標題 
ax1.set_title('Scatter Plot') 
#設定X軸標籤 
plt.xlabel('X') 
#設定Y軸標籤 
plt.ylabel('Y') 
#畫散點圖 
lValue = x 
ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o') 
#設定圖示 
plt.legend('x1') 
#顯示所畫的圖 
plt.show()