[英]How to show legend in Pyplot Scatter 3D
I am making a Scatter 3D pyplot using arrays with float numbers我正在使用带有浮点数的 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
Where x,y,z are:其中 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 ])
The plot: 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')
How do I add the other markers on the legend?如何在图例上添加其他标记? only the first one is showing (a)
只有第一个显示(a)
First, change your legend declaration to the following legend1
.首先,将您的图例声明更改为以下
legend1
。 Then, add that legend to the ax
with add_artist
.然后,使用
add_artist
将该图例添加到ax
。 Lastly, add a label
name to your second scatter plot and call plt.legend()
to show both.最后,将
label
名称添加到您的第二个散点 plot 并调用plt.legend()
以显示两者。 You can also add custom labels to each element legend (if you want abcde instead of 12345).您还可以为每个元素图例添加自定义标签(如果您想要 abcde 而不是 12345)。 If not, then just replace
*[scatter1.legend_elements()[0],['a','b','c','d','e']]
with *scatter1.legend_elements()
.如果没有,那么只需将
*[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.