首頁技術文章正文

Python大數(shù)據(jù)培訓實例:三維空間的星星

更新時間:2022-05-09 來源:黑馬程序員 瀏覽量:

  “一閃一閃亮晶晶,滿天都是小星星......”

  相信很多人都聽過這首《小星星》。Python大數(shù)據(jù)學習中,經常會碰到用3D散點圖實現(xiàn)數(shù)據(jù)的情況,下面就繪制包含若干個五角星的3D散點圖,并在不同的坐標范圍內顯示不同顏色的五角星,具體代碼如下。

# 01_stars_in_3d
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
# 生成測試數(shù)據(jù)
x = np.random.randint(0, 40, 30)
y = np.random.randint(0, 40, 30)
z = np.random.randint(0, 40, 30)
# 創(chuàng)建三維坐標系的繪圖區(qū)域,并在該區(qū)域中繪制3D散點圖
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for xx, yy, zz in zip(x, y, z):
    color = 'y'
    if 10 < zz <20:
        color = '#C71585'
    elif zz >=20:
        color = '#C71585'
    ax.scatter(xx, yy, zz, c=color, marker='*', s=160,
               linewidth=1, edgecolor='black')
ax.set_xlabel('X軸')
ax.set_ylabel('y軸')
ax.set_zlabel('z軸')
ax.set_title('3D散點圖', fontproperties='simhei', fontsize=14)
plt.tight_layout()
plt.show()

  運行程序,效果如圖7-3所示。

1652066343073_3D散點圖.png

  圖7-3 3D散點圖

分享到:
在線咨詢 我要報名
和我們在線交談!