如何在Matlab中使用第三个变量创建颜色渐变?

时间:2017-08-07 21:30:53

标签: matlab matlab-figure

如何在Matlab中创建颜色渐变,以便绘制y = y(x)的2D线图,并使用另一个依赖于x的变量对其进行着色,使得z = z(x)。我也可以使用散点图或点图。

我还希望有一个色彩图传奇类型的东西,显示颜色渐变及其实际的z表示。这种东西在VisIt和ParaView等可视化工具中很常见,但我还是无法在Matlab中弄清楚。

4 个答案:

答案 0 :(得分:3)

我知道这样做的唯一方法是使用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

情节:

enter image description here

答案 1 :(得分:3)

如果散点图很好,可以使用4th input to scatter

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

其中z是颜色。

enter image description here

答案 2 :(得分:2)

要连续操纵线条的颜色,您需要使用surface

初看起来,这个函数看起来对绘制三维曲面非常有用,它为线条着色提供了比基本plot函数更多的灵活性。我们可以使用网格的边来绘制我们的线,并利用顶点颜色C来沿边缘渲染插值颜色。

您可以查看full list of rendering properties,但您最想要的是

  1. 'FaceColor','none',不要画脸
  2. 'EdgeColor','interp',在顶点之间插值
  3. 以下是改编自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

答案 3 :(得分:0)

  • 生成色彩图,例如jet(10)。该示例将生成10 * 3矩阵。
  • 使用interp1使用数据在RGB空间中的值之间进行插值,方法是将第一种颜色设置为最低值,将最后一种颜色设置为最高值。这将生成一个n * 3矩阵,其中n是数据点的数量。
  • scatter与可选参数c一起使用,以使用插值颜色绘制点。
  • 激活colorbar以显示彩条。
相关问题