How to create color gradient using third variable in matlab?

How do you create a color gradient in Matlab so that you plot a 2D plot of y = y (x) and you colorize it with another variable, which also depends on x, so z = z (x). The difference or point plot is also good.

I would also like to have a colormap legend showing the color gradient and the actual z representation. This material is pretty common in visualization tools like VisIt and ParaView, but I still could not display it in Matlab.

+5
source share
4 answers

The only way I know 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

+3
source

If the scatter plot is fine, you can use the 4th input for 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

+3
source

To control the color of the line continuously, you will want to use surface .

At the first viewing, this function looks most useful for building three-dimensional surfaces; it provides more flexibility for coloring strings than the basic plot function. We can use the edges of the grid to build our line and use the colors of the C vertices to make an interpolated color along the edge.

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

  • 'FaceColor', 'none', do not draw faces
  • 'EdgeColor', 'interp', interpolate between vertices

Here is an example from MATLAB's Answer Message

 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'); 

An example image

+2
source
  • Creating a color map, for example. jet(10) . In the example, a 10 * 3 matrix will be created.
  • Use interp1 to interpolate between values ​​in the RGB space using your data, setting the first color as the lowest value and the last color as the highest value. This will create a matrix n * 3, where n is the number of data points.
  • Use scatter with optional c to plot points with interpolated colors.
  • activate colorbar to display the color bar.
0
source

Source: https://habr.com/ru/post/1270660/


All Articles