Matplotlib 3D Graphics GUI

I am trying to use ax.scatter to plot a 3D scatter. I read the data from the fits file and saved the data from the three columns in x, y, z. And I made sure that the data x, y, z have the same size. z was normalized between 0 and 1.

import numpy as np import matplotlib from matplotlib import pylab,mlab,pyplot,cm plt = pyplot import pyfits as pf from mpl_toolkits.mplot3d import Axes3D import fitsio data = fitsio.read("xxx.fits") x=data["x"] y=data["y"] z=data["z"] z = (z-np.nanmin(z)) /(np.nanmax(z) - np.nanmin(z)) Cen3D = plt.figure() ax = Cen3D.add_subplot(111, projection='3d') cmap=cm.ScalarMappable(norm=z, cmap=plt.get_cmap('hot')) ax.scatter(x,y,z,zdir=u'z',cmap=cmap) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show() 

What I'm trying to achieve is to use color to indicate z size. Like a higher z value, it gets darker. But I keep getting the plot without the color map I want, they are all the same by default in blue. What have I done wrong? Thanks.

+5
source share
2 answers

You can use the c keyword in the scatter command to tell you how to color the dots.

You do not need to install zdir as which is intended to create a two-line set

As @Lenford noted, you can use cmap='hot' in this case too, since you have already normalized your data.

I modified your example to use some random data, not your fits file.

 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.random.rand(100) y = np.random.rand(100) z = np.random.rand(100) z = (z-np.nanmin(z)) /(np.nanmax(z) - np.nanmin(z)) Cen3D = plt.figure() ax = Cen3D.add_subplot(111, projection='3d') ax.scatter(x,y,z,cmap='hot',c=z) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show() 

enter image description here

+6
source

According to the pyplot.scatter documentation , the points indicated for construction should be in the form of an array of floats for cmap to apply, otherwise the default color (in this case, jet) will continue to be applied.

As an aside, just specifying cmap='hot' will work for this code, since the hot color map is the registered color map in matplotlib.

+2
source

All Articles