Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib 3D plot use colormap

I am trying to use ax.scatter to plot a 3D scattering plot. I've read the data from a fits file and stored data from three column into x,y,z. And I have made sure x,y,z data are the same size. z has been normolized 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 am trying to achieve is use color to indicate the of size of z. Like higher value of z will get darker color. But I am keep getting a plot without the colormap I want, they are all the same default blue color. What did I do wrong? Thanks.

like image 669
frankgut Avatar asked Aug 13 '15 14:08

frankgut


People also ask

Can matplotlib be used for 3D plotting?

In order to plot 3D figures use matplotlib, we need to import the mplot3d toolkit, which adds the simple 3D plotting capabilities to matplotlib. Once we imported the mplot3d toolkit, we could create 3D axes and add data to the axes. Let's first create a 3D axes. The ax = plt.

How do I choose a colormap?

The best colormap for any given data set depends on many things including: Whether representing form or metric data ([Ware]) Your knowledge of the data set (e.g., is there a critical value from which the other values deviate?) If there is an intuitive color scheme for the parameter you are plotting.

What is PLT CLA ()?

cla() Function. The cla() function in pyplot module of matplotlib library is used to clear the current axes. Syntax: matplotlib.pyplot.cla()


2 Answers

You can use the c keyword in the scatter command, to tell it how to color the points.

You don't need to set zdir, as that is for when you are plotting a 2d set

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

I've modified your example to use some random data rather than 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

like image 96
tmdavison Avatar answered Oct 13 '22 17:10

tmdavison


As per the pyplot.scatter documentation, the points specified to be plotted must be in the form of an array of floats for cmap to apply, otherwise the default colour (in this case, jet) will continue to apply.

As an aside, simply stating cmap='hot' will work for this code, as the colour map hot is a registered colour map in matplotlib.

like image 3
Lenford Avatar answered Oct 13 '22 18:10

Lenford