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

Community
  • 1
  • 1
user2741831
  • 2,120
  • 2
  • 22
  • 43

2 Answers2

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

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • thanks , that works. But the colors are terrible, yours are much better:https://i.imgur.com/qpRkOE6.png – user2741831 May 31 '20 at 13:21
  • You can change the colors by changing the colormap (see `cmap=` parameter). See https://matplotlib.org/tutorials/colors/colormaps.html?highlight=colormap – Diziet Asahi May 31 '20 at 13:23
  • thanks. I personally prefer seaborn though, it just kind of easier to work with when it works. Even comes with a legend by default – user2741831 May 31 '20 at 13:31
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/

snehil
  • 586
  • 6
  • 12