import numpy as np from matplotlib import pyplot as plt
positive_data = arr_feature_pca[y_dbscan_pred != -1, :] negative_data = arr_feature_pca[y_dbscan_pred == -1, :] # --------------------------------------- 定义绘图函数 ----------------------------------- def point3d(ax, theta1, theta2): # 绘制 3D 散点 ax.scatter(positive_data[:, 0], positive_data[:, 1], positive_data[:, 2], s=5, c='g', marker='o', label='Normal') ax.scatter(negative_data[:, 0], negative_data[:, 1], negative_data[:, 2], s=5, c='r', marker='o', label='Abnormal') # 设置坐标轴标签 ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') # 设置视角 # ax.view_init(elev=30, azim=135) # # 显示图例 # ax.legend() # 调整视角 ax.view_init(elev=theta1, # 仰角 azim=theta2 # 方位角 ) from pylab import mpl # 使图表元素中正常显示中文 mpl.rcParams['font.sans-serif'] = 'SimHei' # 使坐标轴刻度标签正常显示负号 mpl.rcParams['axes.unicode_minus'] = False fig = plt.figure(figsize=(32, 28), facecolor='lightyellow' ) # 创建 3D 坐标系 axes = fig.subplots(nrows=4, ncols=4, subplot_kw=dict(fc='whitesmoke', projection='3d', ), ) elevs = [0, 30, 60, 90] azims = [0, 30, 60, 90] for i, theta1 in enumerate(elevs): for j, theta2 in enumerate(azims): ax = axes[i, j] point3d(ax, theta1, theta2) ax.set_title(f'仰角:{theta1} 方位角:{theta2}') # 显示图形 plt.show()
说明:arr_feature_pca是特征数据,应用特征数据进行异常检测,得到预测结果为y_dbscan_pred,结果中-1为异常点,其他为正常点,绘制特征数据散点图,正常点和异常点用不同颜色显示。