如何獲取 mplot3d(matplotlib + python)中所選物件的屬性?


要獲取 matplotlib 3d 中所選物件的屬性,我們可以採取以下步驟。

步驟

  • 設定圖形大小,並調整子圖之間和周圍的邊距。

  • 建立新圖形或啟用現有圖形。

  • 'ax'新增至圖形作為子圖排列的一部分。

  • 對隨機資料點進行散點圖繪製。

  • 將函式*pick_event_method*繫結到事件*pick_event*

  • 列印x、yz事件座標。

  • 要顯示圖形,請使用Show()方法。

示例

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

# Scatter plot
ax.scatter(np.random.rand(10), np.random.rand(10), np.random.rand(10), c=np.random.rand(10),
   cmap='hot', picker=5, s=100)


# pick_event_method
def pick_event_method(event):
   ind = event.ind[0]
   x, y, z = event.artist._offsets3d
   print(x[ind], y[ind], z[ind])


# Connect pick_event_method with pick_event
fig.canvas.mpl_connect('pick_event', pick_event_method)

plt.show()

輸出

它將產生以下輸出 -

現在,單擊圖形中的物件,它將顯示控制檯上的這些點座標。

0.29471404722373373 0.7272382336952506 0.551701540876738
0.7393059098968329 0.880733225356321 0.20733995579556608
0.4055966753557102 0.9709122739514328 0.10116103589732084
0.2781962334047674 0.48531626106129566 0.8573607199598575

更新於: 19-Oct-2021

297 次瀏覽

開啟你的 事業

完成課程以獲得認證

開始
廣告
© . All rights reserved.