【发布时间】:2019-10-03 05:23:19
【问题描述】:
我想绘制一个 3D 散点图,其中数据按组着色。这是数据示例:
aa=pd.DataFrame({'a':[1,2,3,4,5],
'b':[2,3,4,5,6],
'c':[1,3,4,6,9],
'd':[0,0,1,2,3],
'e':['abc','sdf','ert','hgf','nhkm']})
这里,a、b、c 是轴 x、y、z。 e 是散点图中显示的文本。我需要 d 对数据进行分组并显示不同的颜色。
这是我的代码:
fig = plt.figure()
ax = fig.gca(projection='3d')
zdirs = aa.loc[:,'e'].__array__()
xs = aa.loc[:,'a'].__array__()
ys = aa.loc[:,'b'].__array__()
zs = aa.loc[:,'c'].__array__()
colors = aa.loc[:,'d'].__array__()
colors1=np.where(colors==0,'grey',
np.where(colors==1,'yellow',
np.where(colors==2,'green',
np.where(colors==3,'pink','red'))))
for i in range(len(zdirs)): #plot each point + it's index as text above
ax.scatter(xs[i],ys[i],zs[i],color=colors1[i])
ax.text(xs[i],ys[i],zs[i], '%s' % (str(zdirs[i])), size=10, zorder=1, color='k')
ax.set_xlabel('a')
ax.set_ylabel('b')
ax.set_zlabel('c')
plt.show()
但我不知道如何在情节上添加传奇。我希望我的传奇是这样的:
颜色和数字应匹配并有序。
谁能帮我自定义颜色条?
【问题讨论】:
标签: matplotlib categories legend colorbar scatter