How to create a color gradient using a third variable in Matlab?

thephysicsguy picture thephysicsguy · Aug 7, 2017 · Viewed 7.7k times · Source

How do you create a color gradient in Matlab such that you plot a 2D line plot of y=y(x), and you color it using another variable that also depends on x such that z=z(x). A scatter or point plot is also fine by me.

I would also like to have a colormap legend kind of thing showing the color gradient and it's actual representation of z. This stuff is quite common in visualisation tools such as VisIt and ParaView but I could not yet FIGURE it out in Matlab.

Answer

EBH picture EBH · Aug 7, 2017

If a scatter plot is fine, you can use the 4th input to scatter:

x = -10:0.01:10;
y = sinc(x);
z = sin(x);
scatter(x,y,[],z,'fill')

where z is the color.

enter image description here