Color coded 3D scatterplot

Produces a 3D scatterplot of XYZ data with varying colors


Updated 25 Dec 2014

View License

A 3D scatter plot of a Cartesian data set is drawn. The data points are sorted by color and plot3 is called once for each group of points that map to the same color. This reduces execution time significantly for large data sets. By default the points are colored according to their distance from the XY plane. This can changed with the 'ColorData' property. Additionally the upper and lower limits of the color range can be specified. This is useful for creating a series of plots with the same coloring.
The example plot is produced by the following code.

[x,y,z] = peaks(101);
c = gradient(z);
k = hypot(x,y)<3;
plot3k({x(k) y(k) z(k)}, ...
'Plottype','stem','FontSize',12, ...
'ColorData',c(k),'ColorRange',[-0.5 0.5],'Marker',{'o',2}, ...

Plot3k is based on plot3c by Uli Theune.

Cite As

Ken Garrard (2023). Color coded 3D scatterplot (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: plot3c

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Added a stem3 style with colored lines from the xy plane
Updated for R2014b graphics

This update excludes points with NaN color values from the plot.

Updated treatment of NextPlot property as suggested by Val.

Bug fix for 'ColorData', 'ColorRange' properties.

Optional arguments are property,value pairs. Input data can be a cell array, vector or matrix.

The color range and markersize arguments have been added. The figure handle is not returned unless an output argument is given.