【问题标题】:Color some samples of MATLAB figure with different color用不同颜色为一些 MATLAB 图形样本着色
【发布时间】:2020-11-16 01:52:52
【问题描述】:

我在 MATLAB 中使用命令跟踪了一个长度为 N 的离散信号 x

stem(abs(x)); axis([0 N+6 0 4]);

结果图如下:

我的问题我只需要一些对应于例如索引 [7 10 11 12 15 18 48 50 52 60] 的值以用不同的颜色着色,比如说用红色。 我怎样才能在我的身材中做到这一点?

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

    通过hold on 和矩阵索引使用多个图

    您可以使用hold on 将绘图放置在绘图顶部。这确实需要进行调整,在这种情况下您需要一个向量 SampleIndices 指定样本编号/数据点索引。也可以使用矩阵索引获取关键点对应的幅度/数据点,Indicies

    %Vector relating to the sample/data point number%
    Sample = linspace(1,70,70);
    
    %Random test data%
    X = randi([0,2],1,70);
    stem(Sample,X);
    
    hold on
    %Key indices to change colour%
    Key_Indices = [7 10 11 12 15 18 48 50 52 60];
    
    %Matrix indexing to get values/amplitudes corresponding to key indices%
    X_Prime = X(Key_Indices);
    
    stem(Key_Indices,X_Prime,'r');
    
    axis([0 70 0 3]);
    hold off
    

    使用 MATLAB R2019b 运行

    【讨论】:

      【解决方案2】:

      这段代码只使圆圈变成红色,而不是茎

      plot with selected red circles

      %Vector relating to the sample/data point number
      Sample = linspace(1,70,70);
      
      %Random test data
      X = randi([0,2],1,70);
      stem(Sample,X);
      
      %Key indices to change color
      Key_Indices = [7 10 11 12 15 18 48 50 52 60];
      
      line(Sample(Key_Indices), X(Key_Indices), 'linestyle', 'none', 'marker', 'o', 'color', 'r')
      
      axis([0 70 0 3])
      grid on
      

      【讨论】:

        猜你喜欢
        • 2018-10-12
        • 1970-01-01
        • 2015-09-15
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多