Code covered by the BSD License  

Highlights from
Color coded 3D scatterplot

4.86842

4.9 | 41 ratings Rate this file 93 Downloads (last 30 days) File Size: 4.88 KB File ID: #9519
image thumbnail

Color coded 3D scatterplot

by

 

05 Jan 2006 (Updated )

Produces a 3D scatterplot of XYZ data with varying colors

| Watch this File

File Information
Description

A 3D scatter plot of a Cartesian data set is drawn. The data points are sorted by color so that plot3 is called once for each group of points that map to the same color. Execution time is significantly reduced 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.

   figure('color','white');
   [x,y,z] = peaks(101);
   c = gradient(z);
   k = hypot(x,y)<3;
   plot3k({x(k) y(k) z(k)},...
      'ColorData',c(k),'ColorRange',[-0.5 0.5],'Marker',{'o',2},...
      'Labels',{'Peaks','Radius','','Intensity','Lux'},...
      'PlotProps',{'FontSize',12});

Plot3k is based on plot3c by Uli Theune.

Acknowledgements

Plot3c inspired this file.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (49)
28 Aug 2014 samfort  
20 Aug 2014 Emre Kopanoglu  
28 Feb 2014 Wei Liu

Great job Ken!

28 Feb 2014 Wei Liu

Great job Ken!

24 Feb 2014 Dylan

Would there be a quick way to plot each individual point by RGB color value? I know scatter3 has this capability but it is extremely slow.

19 Dec 2013 Jon

I found the colorbar unnecessary for most of my 3-D arrays, but quite useful for plotting 4-D arrays. I added an element to the p structure called "ColorBar" which is checked before generating the colorbar. Then doing plot3k(...,'ColorBar',true,...) can be used to create the plot with the colorbar.

19 Nov 2013 Simon

Hi everyone,

I have a 4D dataset similar and want to use this commands. I am totally new to Matlab. I imported the data from excel and biuld a Matrix:
Points=[g psi microstrain f]:
Each letter has a column of datapoints. When I use the commands above I always get the error:
Undefined function for plot3k for input arguments of type cell.

I tried a lot and dont have a solution.
Can anyone help me?

10 Sep 2013 Mike

Hi Ken, I guess i'm just repeating previous comments but this is fantastic compared to scatter3. Any idea why it is so much faster? Is it because it plots multiple smaller series? With scatter3 plotting is a slow but working on the graph (rotating, editing properties, saving to jpeg) really brings my computer to its knees. Thanks again.

20 Aug 2013 Jon  
19 Aug 2013 Carlyle

Thanks a million Ken! :) I should have thought of that.

PS. I tried downloading the plot3k file again and its still the same version. I have added the necessary line on my end. Thanks!

19 Aug 2013 Carlyle  
17 Aug 2013 Ken Garrard

Carlyle, Points with a NaN color value can be excluded by setting the Z value for those points to NaN. For (x,y,z) data with color vector c, 'z(isnan(c))=NaN'. An updated plot3k has been submitted.

17 Aug 2013 Carlyle

Hi Ken, I was just using this code and noticed that it does not ignore NaN values for the colour-coded points. Is there a way to make the code NOT plot the (x,y,z) point if the associated colour-value for that point is NaN?

31 Jul 2013 Ken Garrard

Jackson, Use either 'hold on' or 'axes(h)' before calling plot3k to plot into the current axes or the axes with handle 'h'.

31 Jul 2013 Jackson Richards

Works great. One question, is it possible to place the plot on an existing set of axes?

26 Jun 2013 dberm22

MUCH quicker than scatter3(). What a lifesaver!

13 Jun 2013 Huaxi Zheng

great! Thanks for sharing, Ken!

10 Jun 2013 Nicholas

This is brilliant. Thanks, Ken

12 Feb 2013 Nick  
23 Mar 2012 Amir

works great!

08 Mar 2012 Ken Garrard

Thanks for the suggestion Val. I'll update the submission.

07 Mar 2012 Val Schmidt

This is a fantastic utility - something that should be incorporated into MATLAB proper, as scatter3 is awful.

I do recommend one change however. Currently, plot3k calls "hold on" prior to plotting anything which means you'll always add to whatever figure window is active. Since this is not the default behavior of MATLAB I find it a hassle.

The fix is to move 'hold on' on line 164 so the plotting loop looks like this:

-------snip----------
% plot data points in groups by color map index
s = 1; % index of 1st point in a color group
for k = 1:length(dLix) % loop over each non-empty color group
plot3(L(s:dLix(k),1), ... % call plot3 once for each color group
L(s:dLix(k),2), ...
L(s:dLix(k),3), ...
mark_ch, ...
'MarkerSize',mark_sz, ...
'MarkerEdgeColor',cmap(L(s,4),:), ... % same marker color from cmap
'MarkerFaceColor',cmap(L(s,4),:) ); % for all points in group
s = dLix(k)+1; % next group starts at next point
if k==1
hold on; % add points to one set of axes
end
end
hold off;
----------snip----------

16 Aug 2011 Alexander

Great code. One small improvement:
Change length(L) to size(L,1) on 96 and 141 lines. Otherwise, it's not working with one data point

13 Jun 2011 James  
12 Mar 2011 Navneet Viswan

Is it possible to use this code to scan images with laser stripe in it and plot out the 3D point cloud of the laser traced portion? with depth?

Kindly help me out

10 Feb 2011 daniel

After trying and failing to plot my data (3e6 points) using scatter3, this little guy did it for my lickety-split. Is a 4D set, so plotting first 3 dimensions as spatial, and fourth as color. Just had to make sure my nargs matched up via cells, so did {data(:,1) data(:,2) data(:,3)} as first arg to plot3k, and data(:,4) as second for color.

A+ rating from this matlab user.

24 Jan 2011 Joseph Miller

Simple, fast and clean. VERY good job! Thank you!

22 Jul 2010 Nathan  
22 Jul 2010 Nathan

minor grid does not show up on Z-axis when Zscale is set to log
(i.e. set(gca,'zscale','log');

any idea why this is occuring?

06 May 2010 Peng Huang

I would suggest to change "length(L)" to "size(L,1)" since when points are too less (i.e. less than 3), error will happen. Besides that, it is great! Thanks:)

23 Feb 2010 V V

Very good - its way quicker than scatter3. Uses much less memory :)

09 Feb 2010 Alvaro Canivell

Very useful indeed !

Something I was missing in it:

- A label for the color bar, that could be passed in the 'strs' together with the title and the rest of the labels.

If somebody wants that, just tell me, I added that

Thanks again !

02 Sep 2009 Le

very good job! thank you!

29 May 2009 aaaaa  
08 Dec 2008 Adam Pilchak

excellent file.
For those interested in additional functionality in terms of viewing contours and slices through 3d data, check out:
http://www.paraview.org

01 Dec 2008 Jveer

very nice indeed! any chance of further speed optimization?

03 Sep 2008 Shazux The Great

one that you should never miss!

30 May 2008 Abel Brown

Holy Jehosaphat!! worked outta the box! The funktion did exactly what i just spent the last hour trying to do! what a fantastic function for scientifik data! you're my hero!!!!

thanks x 10!

07 May 2008 Mike Ross

Exactly what I was looking for. Thank you.

22 Apr 2008 Esben Almkvist

Nice!
I would add a:
if nargout>0
g = gcf;
end
That way you get a cleaner function if you don't need the output.

13 Feb 2008 Tim A

Thanks a lot, very useful

12 Dec 2007 Joseph Marks

It "rocks"; it allows a "surf"-like plot without the hassles and inaccuracies of preppoing the dating over a regular grid!
First-rate work.

13 Jul 2007 Eric DeGiuli

Great, does just what I wanted.

12 Jul 2007 Alexander K.

Very useful function

15 Feb 2007 Matteo Tajana

thank you, I've been looking for such a function for ages! ;-)

05 Jun 2006 sylvio berlusconi

your are a genious

10 May 2006 Josiah Yoder

Just what I was looking for - works great.

05 Apr 2006 Daniel B

Great function. Work well and is fast.

07 Feb 2006 oh kwoun  
Updates
10 Nov 2008

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

14 Oct 2011

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

14 Oct 2011

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

09 Mar 2012

Updated treatment of NextPlot property as suggested by Val.

19 Aug 2013

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

Contact us