1

I am using scatter 3d plot in matplotlib in python for plotting following data,

      x              y            z        b2
3.28912855713 4.64604863545 3.95526335139 1.0
3.31252670518 4.65385917898 3.96834118896 1.0

When the plot is 2d,

fig = plt.figure()
ax = fig.add_subplot(111)
line =ax.scatter(x,y,c=b2,s=500,marker='*',edgecolors='none')
cb = plt.colorbar(line)

It generates following image, which is perfectly right. enter image description here

Now, I want to plot 3d points, for which the code is:

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
line = ax.scatter(x,y,z,c=b2,s=1500,marker='*',edgecolors='none',depthshade=0)
cb = plt.colorbar(line)

And I get following plot, which is not right since the color should be green like previous case (b2 doesn't change). enter image description here

Am I missing something ?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
HP AS
  • 99
  • 4
  • 13

1 Answers1

3

Probably not. I don't know what version of Matplotlib you are using but at some point a bug existed with this issue that apparently still has repercussions for the version I'm using today, i.e. 1.5.1 (check this question for more on this).

Adapting @Yann solution to your problem should resolve that issue. I tested it in my computer and the result is the following:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = [3.28912855713, 3.31252670518]
y = [4.64604863545, 4.65385917898]
z = [3.95526335139, 3.96834118896]
b2 = [1, 1]

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
line = ax.scatter(x, y ,z , c=b2, s=1500, marker='*', edgecolors='none', depthshade=0)
cb = plt.colorbar(line)

def forceUpdate(event):
    global line
    line.changed()

fig.canvas.mpl_connect('draw_event', forceUpdate)

plt.show()

Images of the plot at entry and after a rotation are:

3D matplotlib scatterplot with color

3D matplotlib scatterplot with color after rotation

Community
  • 1
  • 1
armatita
  • 12,825
  • 8
  • 48
  • 49