[英]How to show legend in Pyplot Scatter 3D
我正在使用帶有浮點數的 arrays 制作 Scatter 3D pyplot
x = c_icrs.galactic.cartesian.x.value
y = c_icrs.galactic.cartesian.y.value
z = c_icrs.galactic.cartesian.z.value
其中 x,y,z 是:
x:array([ -65.1525587 , -1.76919179, 3.87621068, -193.69267564, -8.49586587])
y:array([ 99.25870537, -1.91258061, 1.04225814, -96.36986244, 9.24999572])
z:array([ 59.09744472, -0.40753885, 10.51755917, -64.43345929, 21.251354 ])
plot:
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(projection='3d')
ax.set_title("Title")
ax.set_xlabel('X [pc]')
ax.set_ylabel('Y [pc]')
ax.set_zlabel('Z [pc]')
scatter1 = ax.scatter3D(x,y,z, s = 400, marker = '*',c=[1,2,3,4,5])
legend1 = ax.legend(['a','b','c','d','e'], title="Legend")
scatter2 = ax.scatter3D(0,0,0, s = 200, color = 'orange')
如何在圖例上添加其他標記? 只有第一個顯示(a)
首先,將您的圖例聲明更改為以下legend1
。 然后,使用add_artist
將該圖例添加到ax
。 最后,將label
名稱添加到您的第二個散點 plot 並調用plt.legend()
以顯示兩者。 您還可以為每個元素圖例添加自定義標簽(如果您想要 abcde 而不是 12345)。 如果沒有,那么只需將*[scatter1.legend_elements()[0],['a','b','c','d','e']]
替換為*scatter1.legend_elements()
。
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(projection='3d')
ax.set_title("Title")
ax.set_xlabel('X [pc]')
ax.set_ylabel('Y [pc]')
ax.set_zlabel('Z [pc]')
scatter1 = ax.scatter3D(x,y,z, s = 400, marker = '*',c=[1,2,3,4,5])
legend1 = ax.legend(*[scatter1.legend_elements()[0],['a','b','c','d','e']],
title="Legend", loc='upper left')
ax.add_artist(legend1)
scatter2 = ax.scatter3D(0,0,0, s = 200, color = 'orange', label='Dot')
plt.legend()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.