0

I want to plot a 3D matrix using the first column as the x coordinate, second as the y coordinate and the third column as the color.

So I tried it simply like this:

scat=np.matrix([[0,1,2],[10,11,12],[20,21,22]])

ax = sns.scatterplot(data=scat)

enter image description here

Buf for some strange reason instead of scatterplotting it, it plots it like a line plot on the x axis from left to right (from 0 to 2) for each row of data.

So how can I plot matrices how I described using seaborn

0

2 Answers 2

3

Do not use seaborn's scatterplot() if you are not using dataframes and if you don't need the functionality that it provides. Use matplotlib's scatter() directly instead.

PS: np.matrix seems to be deprecated, use np.array instead.

scat=np.array([[0,1,2],[10,11,12],[20,21,22]])
fig, ax = plt.subplots()
ax.scatter(np.squeeze(scat[:,0]), np.squeeze(scat[:,1]), c=np.squeeze(scat[:,2]))

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

thanks , that works. But the colors are terrible, yours are much better:i.imgur.com/qpRkOE6.png
You can change the colors by changing the colormap (see cmap= parameter). See matplotlib.org/tutorials/colors/…
thanks. I personally prefer seaborn though, it just kind of easier to work with when it works. Even comes with a legend by default
1

does this works?

scat  =np.array([[0,1,2],[10,11,12],[20,21,22]])
a  =scat.T
a  =pd.DataFrame({'x':a[0], 'y':a[1] , 'z':a[2]})
ax =sns.scatterplot(data=a, x='x', y='y', hue='z',palette="deep")

edit: you can use palette , you can refer https://seaborn.pydata.org/tutorial/color_palettes.html or https://chrisalbon.com/python/data_visualization/seaborn_color_palettes/

2 Comments

yep it does, thanks, unfortunately it gives me really crappy colors:i.imgur.com/WwgkMNS.png
you can try different palettes , and choose which suits you most.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.