1. 程式人生 > >plt繪製 2維、3維散點圖

plt繪製 2維、3維散點圖

# 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維
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()