【问题标题】:2-D line gradient color in MatlabMatlab中的二维线渐变颜色
【发布时间】:2017-02-11 10:43:17
【问题描述】:

是否可以在 Matlab 中为二维线添加渐变色,尤其是当您的数据点数量较少(少于 10 个?)时,结果会类似于下图中的一个?

【问题讨论】:

    标签: matlab graph colors gradient matlab-figure


    【解决方案1】:

    如果您有 MATLAB R2014b 或更新版本,这并不难。

    n = 100;
    x = linspace(-10,10,n); y = x.^2;
    p = plot(x,y,'r', 'LineWidth',5);
    
    % modified jet-colormap
    cd = [uint8(jet(n)*255) uint8(ones(n,1))].';
    
    drawnow
    set(p.Edge, 'ColorBinding','interpolated', 'ColorData',cd)
    

    结果:

    摘自 Undocumented Features - Color-coded 2D line plots with color data in third dimension。原作者是thewaywewalk。归属细节可以在contributor page 上找到。该源在CC BY-SA 3.0 下获得许可,可以在Documentation archive 中找到。参考主题 ID:2383 和示例 ID:7849。

    【讨论】:

    • 感谢您的解决方案!
    【解决方案2】:

    这是一种可能的方法:使用从所需颜色图中获取的不同颜色显式绘制线条的每一段。

    x = 1:10; % x data. Assumed to be increasing
    y = x.^2; % y data
    N = 100; % number of colors. Assumed to be greater than size of x
    cmap = parula(N); % colormap, with N colors
    linewidth = 1.5; % desired linewidth
    xi = x(1)+linspace(0,1,N+1)*x(end); % interpolated x values
    yi = interp1(x,y,xi); % interpolated y values
    hold on
    for n = 1:N
        plot(xi([n n+1]), yi([n n+1]), 'color', cmap(n,:), 'linewidth', linewidth);
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-07
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2016-05-31
      相关资源
      最近更新 更多