You can use a surface
which looks like a line
by replicating all the 2D input vectors (x
and y
) in order to have matrices. These matrices can be used to generate a surface
object, which allow a lot more control over the line and point colours, thanks to the CData
property.
First I have to generate sample data:
%% // sample gplot data (from matlab documentation example)
reset(groot) %// optional, only if you modified the default groot properties previoulsly
k = 1:30;
[B,XY] = bucky;
[xt, yt] = gplot( B(k,k),XY(k,:) ) ;
%% // sample custom properties (point/line colors)
nColor = 16 ; %// to start with low number of colors
c = randi([1 nColor],size(xt)) ; %// random colors for each point
Now we have a set of points and a scalar value (intensity or anything else) to be color coded in the display. We can use a surface
object to take all these parameters into account in one single graphic object:
%% // create 'Matrix' style input from 2D line data
X = [xt,xt] ; %// replicate column vector "xt"
Y = [yt,yt] ; %// same for "yt"
C = [c c] ; %// replicate column vector "c"
Z = zeros(size(X)) ; %// Z plane = 0
%% // DISPLAY - Surf only
figure
hs = surf(X,Y,Z,C,'EdgeColor','interp','FaceColor','none','Marker','*','LineWidth',1.5) ;
colormap(hsv(nColor)) %// choose a more distintive colormap (any other colormap will work)
colorbar
view(2)
Of course, you can choose the colormap that best fit your needs (very progressive colormap or very distinct). These 2 types of colormap will lend themselves more to a different type of shading.
You can also choose how the colour information will be handled for each line:
shading flat
=> One solid color for the full line, or
shading interp
=> a gradient of color between the 2 anchor points.
Below is an example with 16 colors for 2 different colormap (hsv
and gray
), with the different shading settings.

This should give you the maximum control over the appearance of your lines, with only one graphic object to handle (of course you could always draw each line separately and set the custom properties but you will need a loop and a lot of graphic objects).
If you want even further control for the points, you can forget about the markers of the surface
object, and superimpose a scatter
plot to display the points. This would give you extra options on the point appearance. Below is a quick example on how to do that, check the scatter
documentation for more usage example.
%% // DISPLAY - Surf and Scatter
figure
hs = surf(X,Y,Z,C,'EdgeColor','interp','FaceColor','none','Marker','none') ;
hold on
hp = scatter(xt,yt,25,c,'filled','LineWidth',1.5) ;
colormap(hsv(nColor)) %// choose a more distintive colormap (any other colormap will work)
colorbar
view(2)
Documentation: