File Exchange

image thumbnail

Colored line or scatter plot

version (1.49 KB) by Pekka Kumpulainen
Plot lines with CData, (actually surface)


Updated 07 Apr 2008

View License

plots a 2-D "line" with c-data as color
Makes a surface object and the colouring is controlled by the colormap.

x = 0:0.1:2*pi;

Cite As

Pekka Kumpulainen (2020). Colored line or scatter plot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Erwin Schoonderwaldt

Nicely done by using surface plot. Was easy to extend to 3D lines too.

Erwin Schoonderwaldt


I found this technique very useful. It seemed relevant to the discussion at
where I posted a comment referencing to this submission. Thanks.

Yilong Jia

Works pretty well for my system. Thanks!

Pekka Kumpulainen

^ works fine up to million elements (didn't try with more).
The function does NOT refresh the plot. It only adds a new colored line.
Maybe you should clear the figure (clf).


Unfortunately in my hands this function only seems to work for x shorter than 300 elements.

For example:

x = 1:301

never generates a plot(i.e. the plot window never refreshes.)

For x=1:300 it works beautifully though.
Is there a simply way to make it work for longer x (e.g. 2500 points).

Shinya Ito

Exactly what I was looking for! Thanks!


Doesn't seem to work with R2008b. The following is returned for the example:

Warning: size(CData) must equal size(ZData) or size(ZData)-1 for flat shading

Georg Stillfried

Nice. Just what I needed!

Tristan Desrues

Perfect !!
let you plot large dataset of coordinate, colored with velocity, in 1 line:

instead of
for i=1:numel(x)

And it's a lot faster!!

Carlos Adrian Vargas Aguilera

This is nice Pekka, I recommend you to include a screenshot.

My 'low' rating is because if the length of the series is short (10 elements for example) the colors are different for the same y-values. My suggestion is to generate colorbands in the y-axis and make linear interpolations in these bands.

Regards, Carlos

Ed Mackay

Useful function which avoids creating separate line segments for each color. The coloured line produced is a single MATLAB graphics object.

d luc

thanks--this is a useful routine for plotting 2-d vectors (e.g. U=[u,v]) on a [u,v] plane, where the color gives the sense of rotation (time evolution) of the vector.

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

Inspired: 3D colored line plot