下面的代碼創建并繪制了三個 3d 多邊形。在字典polygon_dict中,為每個Poly3DCollection藝術家創建并保存標簽。目標是使用其標簽參考特定多邊形,然后將其從圖中洗掉。我未能實作這一目標的嘗試(它僅適用于ax.plot()物件)在最后顯示注釋。任何幫助深表感謝。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
polygon_dict = {}
polygon_count = 0
def plot_polygon(vertices):
global polygon_count
polygon_count = 1
polygon_label = 'P' str(polygon_count)
polygon_dict[polygon_label] = ax.add_collection3d(Poly3DCollection([vertices]))
[plot_polygon(np.random.rand(3,3)) for i in range(3)]
print(polygon_dict)
plt.show()
# Delete polygon with label 'P2'. The following doesn't work.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# polygon_dict['P2'].pop(0)
# plt.show()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The resulting plot should not have polygon 'P2'.
uj5u.com熱心網友回復:
您可以洗掉藝術家:
...
print(polygon_dict)
polygon_dict["P2"].remove()
plt.show()
由于您的字典中仍然參考了多邊形P2,因此它不會被垃圾收集并繼續作為 Poly3DCollection 物件存在。因此,它與
....
print(polygon_dict)
polygon_dict["P2"].set_visible(False)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440773.html
標籤:Python python-3.x matplotlib 轴
上一篇:如何創建自己的cmap?
下一篇:如何平滑地繪制移動點
