6

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.

thephysicsguy
  • 403
  • 1
  • 4
  • 15

4 Answers4

5

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

EBH
  • 10,350
  • 3
  • 34
  • 59
  • This actually worked just fine! I may not select best answer as they are multiple ways to solve this and I would like to know them, but upvoted. Shame that Matlab does not have a cool function for this seeing how much money they charge. – thephysicsguy Aug 07 '17 at 22:21
  • Well, in the case of scatter-plot they _do_ have a function. As for a line, I think it is less clear how to 'segment' the line for coloring. Think of it - the true position of x is infinitely small so what part of the line should be colored for specific x? This way or another, they still charge too much ;) – EBH Aug 07 '17 at 22:29
4

The only way I know of to do this is with a little trick using surf:

% Create some sample data:
x = cumsum(rand(1,20));  % X data
y = cumsum(rand(1,20));  % Y data
z = 1:20;                % "Color" data

% Plot data:
surf([x(:) x(:)], [y(:) y(:)], [z(:) z(:)], ...  % Reshape and replicate data
     'FaceColor', 'none', ...    % Don't bother filling faces with color
     'EdgeColor', 'interp', ...  % Use interpolated color for edges
     'LineWidth', 2);            % Make a thicker line
view(2);   % Default 2-D view
colorbar;  % Add a colorbar

And the plot:

enter image description here

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • This does not seem to work and it is giving me an error. For reference; x, y and z are same size in my problem (I basically want to plot temperature against distance and color it with for example specific heat and they are all 1000, 1 double). – thephysicsguy Aug 07 '17 at 22:13
  • @thephysicsguy: I updated my answer. The issue was that I was using row vectors of data, but you appear to have column vectors of data. I generalized the code to handle either. – gnovice Aug 08 '17 at 03:26
3

To manipulate the color of the line continuously, you'll want to use surface.

While at first look, this function looks most useful for plotting 3d surfaces, it provides more flexibility for line coloring than the basic plot function. We can use the edges of the mesh to plot our line, and take advantage of the vertex colors, C, to render interpolated color along the edge.

You can check out the full list of rendering properties, but the ones you are most likely to want are

  1. 'FaceColor', 'none', do not draw the faces
  2. 'EdgeColor', 'interp', interpolate between vertices

Here's an example adapted from MATLAB Answers post

x = 0:.05:2*pi;
y = sin(x);
z = zeros(size(x)); % We don't need a z-coordinate since we are plotting a 2d function
C = cos(x);  % This is the color, vary with x in this case.
surface([x;x],[y;y],[z;z],[C;C],...
        'FaceColor','none',...
        'EdgeColor','interp');

Image produced by example

Cecilia
  • 4,512
  • 3
  • 32
  • 75
0
  • Generate a colormap, e.g. jet(10). The example would generate a 10*3 matrix.
  • Use interp1 to interpolate between the values in the RGB space using your data by setting the first color as the lowest value and the last color as the highest value. This would generate a n*3 matrix where n is the number of data points.
  • Use scatter with the optional parameter c to plot the points with the interpolated colors.
  • activate colorbar to show the colorbar.
MosGeo
  • 1,012
  • 9
  • 9
  • You don't need `jet` and `interp1` for that, you can use the `z` as is. See my answer here. – EBH Aug 07 '17 at 22:20