plt绘制 2维、3维散点图

本文通过使用Python的matplotlib和sklearn库,展示了如何生成3D和2D的数据可视化图表。具体包括了如何创建分类数据,以及如何根据不同类别用不同颜色在3D和2D空间中展示这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 3维
import
numpy as np import matplotlib.pyplot as plt from sklearn.datasets.samples_generator import make_classification from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) data,labels=make_classification(n_samples=1000,n_features=3,n_redundant=0,n_informative=2, random_state=1,n_clusters_per_class=2) unique_lables=set(labels) colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables))) for k,col in zip(unique_lables,colors): x_k=data[labels==k] ax.scatter3D(x_k[:,0],x_k[:,1],x_k[:, 2], c=col) # 开始绘制,x_k[:,0] 表示取第一维 plt.title('data by make_classification()') plt.show()

 

# 2维

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
data,labels=make_classification(n_samples=100,n_features=2,n_redundant=0,n_informative=2,
                             random_state=5,n_clusters_per_class=2)
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
    x_k=data[labels==k]
    plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
    markersize=10)
plt.title('data by make_classification()')
plt.show()

 

转载于:https://www.cnblogs.com/callyblog/p/10083885.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值