Home > database >  How to change the colors and rename labels of a 3D plot
How to change the colors and rename labels of a 3D plot

Time:09-22

Background:

I'm processing text (dataset with 1000 documents - applying Doc2Vec using Gensim lib), at the end I have a 300 dimension matrix for each doc.

So, I did this to have a 3 dimensional matrix:

X_comp = TSNE( n_components = 3 ).fit_transform(X)

and this:

Y = df2['Classes']

tsne_x = X_comp[:-1,0]
tsne_y = X_comp[:-1,1]
tsne_z = X_comp[:-1,2]

My Y above have 5 classes, and I'm plotting 2D:

input:

f, ax = plt.subplots(figsize=(12, 12))

#plot
sns.scatterplot(x = tsne_x,
                y = tsne_y,
                hue = Y,
                palette = sns.color_palette('hls', n_colors=5),
                alpha = 0.8)

output:

t-SNE 2D plot

And 3D:

input:

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("whitegrid", {'axes.grid' : False})

fig = plt.figure(figsize=(12,12))

ax = Axes3D(fig) # Method 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", title="Classes", borderaxespad=-10, ncol=5)
ax.add_artist(legend)

plt.show()

output:

t-SNE 3D plot

Now, my questions:

  1. How can I change the name of the Classes in the legend, instead of numbers show the name of the classes?

  2. Is there a way to determinate the color for each Class? So the both plots have the exact same color for each class

Update:


Based on @user1740577 answer I change my code to this:

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.figure(figsize=(12,12))

ax = Axes3D(fig) 

colors = ['blue', 'orange', 'gray', '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',
                             'Class 4',
                             'Class 5'),loc='upper right')


plt.show()

But I'm getting this output: enter image description here

What I did wrong?

CodePudding user response:

Because I don't have your tsne_x, tsne_y, tsne_z. I send example. In Your code you need split base your Label and use this code.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

colors = ['b', 'c', 'y']
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='upper right')

plt.show()

Output:

enter image description here

EDIT base edited question:

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])
  • Related