背景:
我正在處理文本(有1000個檔案的資料集--使用Gensim lib應用Doc2Vec),最后我為每個檔案建立了一個300維矩陣。
因此,我這樣做是為了得到一個3維的矩陣:
X_comp = TSNE( n_components = 3 ) .fit_transform(X)
還有這個:
Y = df2['classes']
tsne_x = X_comp[:-1,0]
tsne_y = X_comp[:-1,1]
tsne_z = X_comp[:-1,2]
我上面的Y有5個類,我正在繪制2D:
輸入:f, ax = plt.subplots(figsize=(12, 12)
#plot[/span]。
sns.scatterplot(x = tsne_x,
y = tsne_y,
hue = Y,
palette = sns.color_palette('hls', n_colors=5) 。
alpha = 0.8)
輸出:
和3D:
輸入:
import re, seaborn as sns, numpy as np, pandas as pd, random
from pylab import *
from matplotlib.pyplot import plot, show, draw, figure, cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sns.set_style("whiteegrid", {'axes.grid' : False})
fig = plt.fig(figsize=(12,12)
ax = Axes3D(fig) # 方法1
x = tsne_x
y = tsne_y
z = tsne_z
g = ax.scatter(x, y, z, c=Y, marker='o', cmap = sns. color_palette('hls', n_colors=5 ,as_cmap=True)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
legend = ax.legend(*g.legend_elements(), loc="lower center"/span>, title="Classes"/span>, borderaxespad=-10, ncol=5)
ax.add_artist(legend)
plt.show()
輸出:
現在,我的問題:
我的問題是
我怎樣才能改變圖例中的類的名稱,而不是用數字來顯示類的名稱?
是否有辦法確定每個類別的顏色?這樣,兩個圖的每個類的顏色就完全一樣了
有沒有辦法確定每個類的顏色?
更新:
基于 @user1740577 的回答,我把我的代碼改成了這樣:
import re, seaborn as sns, numpy as np, pandas as pd, random
from pylab import *
from matplotlib.pyplot import plot, show, draw, figure, cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.fig(figsize=(12,12)
ax = Axes3D(fig)
colors = ['blue', 'orange', 'grey', 'cyan', 'purple']
x = tsne_x
y = tsne_y
z = tsne_z
c1 = ax.scatter(x, y, z, color=colors[0] )
c2 = ax.scatter(x, y, z, color=colors[1] )
c3 = ax.scatter(x, y, z, color=colors[2] )
c4 = ax.scatter(x, y, z, color=colors[3] )
c5 = ax.scatter(x, y, z, color=colors[4] )
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.legend((c1, c2, c3, c4, c5), ('Class 1',
'Class 2',
'Class 3',
'4級'。
'Class 5'),loc=' upper right')
plt.show()
我做錯了什么?
uj5u.com熱心網友回復:
因為我沒有你的tsne_x, tsne_y, tsne_z。我發了一個例子。在你的代碼中,你需要拆分你的Label并使用這個代碼。
importmatplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
顏色 = ['b'/span>, 'c'/span>, 'y'/span>]
np.random.seed(365)
c1 = ax.scatter(np.random.rand(100), np.random.rand(100), np.random.rand(100), color=colors[0] )
c2 = ax.scatter(np.random.rand(100), np.random.rand(100), np.random.rand(100), color=colors[1] )
c3 = ax.scatter(np.random.rand(100), np.random.rand(100), np.random.rand(100), color=colors[2] )
plt.legend((c1, c2, c3),('class one', 'class two', 'class three'), loc='up right')
plt.show()
輸出:
編輯基地編輯的問題:
Y = df2['Classes']
c1 = ax.scatter(x[Y==1], y[Y==1], z[Y==1], color=colors[0] )
c2 = ax.scatter(x[Y==2], y[Y==2], z[Y==2], color=colors[1] )
c3 = ax.scatter(x[Y==3], y[Y==3], z[Y==3], color=colors[2] )
c4 = ax.scatter(x[Y==4], y[Y==4], z[Y==4], color=colors[3] )
c5 = ax.scatter(x[Y==5], y[Y==5], z[Y==5], color=colors[4] )
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/328925.html
標籤:
上一篇:時間序列組之間的相似性




