How to plot 3D data with matplotlib

Below I define three arrays x, y, and z, but plotting only the x,y coordinates as shown in the previous notebook.

In [1]:
from matplotlib import pyplot as plt
import numpy


x=numpy.array([1,2,3,4,5])
y=numpy.array([5,4,7,1,7])
r=numpy.array([0.1,0.6,0.4,1.0,0.3])

plt.rcParams['axes.linewidth'] = 1.5
plt.rcParams['xtick.major.size'] = 8
plt.rcParams['xtick.minor.size'] = 4
plt.rcParams['ytick.major.size'] = 6
plt.rcParams['ytick.minor.size'] = 3
plt.rcParams['ytick.minor.size'] = 3


fig = plt.figure(figsize=(7,5))
ax = fig.add_axes([0.1,0.1,0.88,0.88])

# plot data
line1 = ax.plot(x,y,'ok')

#modify axes
ax.set_xlim(0,6)
ax.set_ylim(0,8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display
plt.show()
In []:

Using the scatter plot command to show 3D information

There are two ways of adding 3D information with the scatter plot. Either change the size of the markers according to the value (this plot) or the color of the maker (next plot0.

In [2]:
fig = plt.figure(figsize=(7,5))
ax = fig.add_axes([0.1,0.1,0.88,0.88])

# plot data
line1 = ax.scatter(x,y,s=r*200,c='b',marker='o') # size of point defined with s with size of points^2 on the image. 

#modify axes
ax.set_xlim(0,6)
ax.set_ylim(0,8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display or savefig
plt.show()

Very different color scheme are built into matplotlib and can be chosen you can see an overview of the available color maps at matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html

In [8]:
from matplotlib import cm

fig = plt.figure(figsize=(7,5))
ax = fig.add_axes([0.1,0.1,0.88,0.88])

# plot data
#cmap=cm.gray
#cmap=cm.hsv_r
cmap=cm.RdBu_r
line1 = ax.scatter(x,y,s=100,c=r,marker='o',cmap=cmap, vmin=0, vmax=1.0)

#modify axes
ax.set_xlim(0,6)
ax.set_ylim(0,8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display 
plt.show()

Adding a colorbar

Colorbars can be easily added to a figure. There are two ways of doing it. Either taking a fraction of space form the primary axis to be used for the coloarbar or define an additional axis somewhere to which the colorbar will be drawn into. The last option allows a lot of flexibility to define location of the color bar as shown below. Both option are part of the code, with the first one commented out.

In [4]:
from matplotlib import cm

fig = plt.figure(figsize=(7,5))
ax = fig.add_axes([0.1,0.1,0.88,0.88])

# plot data
cmap=cm.RdBu_r
line1 = ax.scatter(x,y,s=100,c=r,marker='o',cmap=cmap, vmin=0, vmax=1.0)

# colorbar using space of the primary axis
#cb = plt.colorbar(line1,ax=ax,fraction=0.1,pad=0.03,aspect=20)
#cb.ax.tick_params(width=2,labelsize=15)

# colorbar explicit
ax_cb = fig.add_axes([0.15,0.25,0.4,0.05])
cb = plt.colorbar(line1,cax=ax_cb,orientation='horizontal')
cb.set_label('color units',fontsize=14)


#modify axes
ax.set_xlim(0,6)
ax.set_ylim(0,8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display 
plt.show()

Using different normalizations

In [5]:
from matplotlib import cm

fig = plt.figure(figsize=(7,5))
ax = fig.add_axes([0.1,0.1,0.88,0.88])

# define normalization
from matplotlib import colors
linearNorm = colors.Normalize(vmin=0.0001,vmax=1.0)
logNorm = colors.LogNorm(vmin=0.0001,vmax=1.0)


# plot data
cmap=cm.RdBu_r
line1 = ax.scatter(x,y,s=100,c=r,marker='o',cmap=cmap, norm=logNorm)

# colorbar simple
cb = plt.colorbar(line1,ax=ax,fraction=0.1,pad=0.03,aspect=20)
cb.ax.tick_params(width=2,labelsize=15)


#modify axes
ax.set_xlim(0,6)
ax.set_ylim(0,8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display 
plt.show()

Real 3D plots

It is also possible to create "real" 3D plots. The command are always the same but the axis need to be created differently to which the data is plotted to. You can see the available 3D plots at http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

In [6]:
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(7,5))
ax = Axes3D(fig)

# plot data
line1 = ax.plot(x,y,r,'ok')

#modify axes
ax.set_xlim(0,6)
ax.set_ylim(0,8)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display 
plt.show()

Plotting images

Images can be plotted easily. Important is to change the origin of the array, the means you need say if the (0,0) index of the array should appear at the 'upper' corner (default) or 'lower' corner with the origin parameter. Since the array to plot is always in units of indices, one should define the extent of the array to tell matplotlib the boundaries of the image in physical units.

In [7]:
from matplotlib import cm
import pyfits

hdu = pyfits.open('/scratch/MUSE/NGC2906_test/NGC2906_MUSE_Ha_img.fits')
img_Ha = hdu[0].data


fig = plt.figure(figsize=(7,5))
ax = fig.add_axes([0.1,0.1,0.88,0.88])

# plot data
linearNorm = colors.Normalize(vmin=0.0001,vmax=20.0)
logNorm = colors.LogNorm(vmin=0.0001,vmax=20.0)

cmap=cm.RdBu_r
#img = ax.imshow(img_Ha)
img = ax.imshow(img_Ha,origin='lower',interpolation='nearest',cmap=cmap,extent=[0,60,0,60],norm=linearNorm,aspect='equal')

# colorbar simple
cb = plt.colorbar(img,ax=ax,fraction=0.1,pad=0.03,aspect=20)
cb.ax.tick_params(width=2,labelsize=15)

#modify axes
ax.set_xlim(5,55)
ax.set_ylim(10,50)
ax.minorticks_on()
ax.tick_params(axis='both',which='minor',length=5,width=2,labelsize=18)
ax.tick_params(axis='both',which='major',length=8,width=2,labelsize=18)

#display 
plt.show()