Python3D繪圖Axes3D-scatterplot

繪製3D散點圖的程序


import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

label_font = {
    'color' : 'c' ,
    'size' : 15,
    'weight' : 'bold'
}

def randrange(n, vmin, vmax):
    r = np.random.rand(n)   #隨機生成n 個介於0-1之間的數
    return (vmax-vmin) * r + vmin   #得到n個[vmin,vmax]之間的隨機數


fig = plt.figure(figsize=(16,12))   # 參數依然是圖片大小
ax = fig.add_subplot(111, projection='3d')      #確定子座標軸,111表示1行1列的第一個圖   要同時畫好幾個圖的時候可以用這個

#準備數據
n = 200
for zlow, zhigh, c, m, l in [(4, 15, 'r', 'o', 'posirtive'), (13, 40, 'g', '*', 'negative')]:  #用兩個tuple(畫筆),是爲了將形狀和顏色區別開來
    x = randrange(n, 15, 40)
    y = randrange(n, -5, 25)
    z = randrange(n, zlow, zhigh)
    ax.scatter(x, y, z, c=c, marker=m, label=l, s=z * 10)   # marker的尺寸和z的大小成正比

ax.set_xlabel("X axis", fontdict=label_font)
ax.set_ylabel("Y axis", fontdict=label_font)
ax.set_zlabel("Z axis", fontdict=label_font)
ax.set_title("Scatter plot", alpha=0.6, color="b", size=25, weight='bold', backgroundcolor="y")   #子圖(其實就一個圖)的title
ax.legend(loc="upper left")   #legend的位置位於左上


plt.show()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章