3D scatter plot legend error from KMeans quot;No handles with labels found to put in legendquot;(来自KMeansQuot;的3D散点图图例错误找不到要放入图例中的标签的句柄)
本文介绍了来自KMeans&Quot;的3D散点图图例错误找不到要放入图例中的标签的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为用于RFM分析的KMeans模型绘制了3D散点图。我使用了KMeans模型标签来表示"颜色"组。 当我使用Legend()时,它弹出一个错误,"找不到带标签的句柄放入图例"
from mpl_toolkits.mplot3d import Axes3D
%matplotlib notebook
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
xs = RFM['Recency'].dt.days
ys = RFM['Frequency']
zs = RFM['Value']
ax.scatter(xs, ys, zs, s=50, alpha=0.6, c=final_model.labels_, cmap='rainbow')
ax.set_xlabel('Recency(days)')
ax.set_ylabel('Frequency')
ax.set_zlabel('Value')
ax.legend()
推荐答案
您代码的问题是您没有为散乱的点提供任何label,因此没有理由legend可以工作。
按照matplotlib网站上的Scatter plots with a legend教程,下面的代码使用三个"类"随机化数据,并用图例绘制它们:
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
xs = np.random.normal(0, 1, (20,))
ys = np.random.normal(0, 1, (20,))
zs = np.random.normal(0, 1, (20,))
labels = np.random.choice(["First", "Second", "Third"], (20,))
for lbl in np.unique(labels):
indices = np.where(labels == lbl)
x = xs[indices]
y = ys[indices]
z = zs[indices]
print(x,y,z,lbl)
ax.scatter(x, y, z, s=50, alpha=0.6, label=str(lbl), cmap='rainbow')
ax.legend()
plt.show()
结果为:
这篇关于来自KMeans&Quot;的3D散点图图例错误找不到要放入图例中的标签的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:来自KMeans&Quot;的3D散点图图例错误找不到要放入图例中的标签的句柄
基础教程推荐
猜你喜欢
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
