Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vary the line color of a matlab plot (like colormap)?

I have a 2D space in which a function value is defined (you can think of it as a manifold). Now I plotted the function value using contourf and changed the colormap to something softer than jet. So far it looks quite good.

Now I want to draw a line representing the state over time in my space. That is also possible using the the plot command. But I want some more improvements: There is an additional state that is hidden for now (value 0...50). I would like the line color to change according to this hidden state. So in a sense to apply a separate colormap to the line plotted by plot like for example in a waterfall plot.

Is this (or something similar) possible using matlab?

Thanks

like image 757
Christian Wolf Avatar asked Dec 19 '11 09:12

Christian Wolf


2 Answers

If you want to either use interpolated shading or have the colours change with the colour map, then you want to plot your data as a mesh and set the edgecolor property appropriately. Note that in order to plot it as a mesh, then you will need to duplicate it so that it has a size of at least 2 in each direction.

h = mesh([X(:) X(:)], [Y(:) Y(:)], [Z(:) Z(:)], [C(:) C(:)], ...
    'EdgeColor', 'interp', 'FaceColor', 'none');

You may also want to look at the MeshStyle property, if you want to plot multiple lines simultaneously.

This solution is also much nicer than the one used in cline because it only creates one graphics object, rather than n.

like image 200
Nzbuu Avatar answered Oct 13 '22 00:10

Nzbuu


Have a look into the cline.m function from file exchange, I think it is exactly what you need.

like image 37
silvado Avatar answered Oct 13 '22 01:10

silvado