【发布时间】:2023-03-05 14:50:01
【问题描述】:
我正在尝试使用 matplotlib 绘制 3D 散点图,但由于某种原因,输出没有显示在图例中。 我希望图例成为我的数据框列(类别)之一。
fig = plt.figure(figsize=(12,8))
ax = Axes3D(fig)
color_dict = { 'Beauty':'red', 'Kids':'green', 'Food':'blue', 'Jewelry':'yellow')}
names = df['category'].unique()
for s in names:
sc = ax.scatter(embedding[:,0], embedding[:,1], embedding[:,2], s=40,
color=[color_dict[i] for i in df_brands['category']], marker='x', label=names[s])
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.legend()
# plt.show()
plt.legend(*sc.legend_elements(), bbox_to_anchor=(1.05, 1), loc=2)
从下面的输出中可以看出,图例显示在右上角,所有类别的颜色都相同(红色)。 (我将代码简化为 4 种颜色,忽略情节中有更多颜色的事实)。
任何帮助将不胜感激。 谢谢!
【问题讨论】:
标签: python pandas matplotlib scatter-plot scatter3d