Sirmione
Sirmione

Reputation: 311

3D scatter color by categorical (x,y) coordinates?

I am trying to make the scatter dots have the same color if they have the same (x,y) values but with a different Z value.

so far I managed to use one variable either x or y, but I cant "zip" them together somehow.

df["X"]=pd.Categorical(df["X"])
df["Y"]=pd.Categorical(df["Y"])
df["X"].cat.codes
df["Y"].cat.codes
bx.scatter(xs,ys,zs, zdir=zs, c=df["X"].cat.codes,cmap="Set1", alpha=1)

I tried to zip them individually, I tried making an array out of them...If I try with

df["cat"]=pd.Categorical(zip(df["X"],df["Y"]))
df["cat"].cat.codes

all I get is one category code, so everything is the same.

Any ideas? enter image description here

image for clarification

At first I thought the dates should be the category, but that doesnt makes sense because every day ( since the Zaxes is in datetime format) would have a different a color. Insstead, every possible (x,y) pair ie. (1,-1), (2,1) etc should be a category(is it the right word to use?) by itself so then every pair should have its own color for example (1,-1) is black, (2,1) is red independently of its Z coordinate.

Upvotes: 1

Views: 1039

Answers (1)

Sirmione
Sirmione

Reputation: 311

the solution was quite easy in fact, it just took some tinkering round

Original DF has X,Y,Date columns.

xs=df.X
ys=df.Y
zs=np.array(df_dates2num)
N=len(df)

val_x=df["X"].tolist()
val_y=df["Y"].tolist()

df["pairs"]=pd.Series(list(zip(val_x,val_y)), index=df.index) #make(x,y) pairs

df["pairs"]=df["pairs"].astype("category") 
df["pairs"]=df.pairs.cat.codes

bx.scatter(xs,ys,zs, zdir=zs, c=df["pairs"], alpha=1, s=50) #scatter dots

and it actually works, the only thing missing is to use a more discrete color palette.

Upvotes: 1

Related Questions