【问题标题】:How to add marker/symbol to colorbar如何将标记/符号添加到颜色条
【发布时间】:2016-09-20 12:15:34
【问题描述】:

我想在我的 MATLAB colorbar 中添加一个标记/特殊刻度线。例如,假设我有一个从-23 的颜色条刻度,我的临界值是-1.8,我如何通过符号/标记添加值的标记?

【问题讨论】:

    标签: matlab matlab-figure symbols colorbar


    【解决方案1】:

    执行此操作的一种方法是获取colorbar 位置,计算您想要标记的位置,然后在其中放置annotation object(如箭头):

    % Plot sample data, displaying color bar and getting its limits:
    
    data = peaks();
    imagesc(data);
    hBar = colorbar();
    cLimits = caxis();
    hold on;
    
    % Select and plot a point of interest:
    
    point = [31 15];
    value = data(point(1), point(2));
    plot(point(2), point(1), 'r+');
    
    % Compute location of color bar pointer and make annotation:
    
    barPos = get(hBar, 'Position');
    xArrow = barPos(1)+barPos(3)/2+[0.05 0];
    yArrow = barPos(2)+barPos(4)*(value-cLimits(1))/diff(cLimits)+[0 0];
    hArrow = annotation('textarrow', xArrow, yArrow, ...
                        'String', num2str(value, '%.2f'), 'Color', 'r');
    

    如果调整图形大小,注释对象的位置可能会相对于彩条发生偏移。避免这种情况的一种方法是使用以下代码调整轴和颜色条的大小调整行为:

    axesPos = get(gca, 'Position');
    set(hBar, 'Location', 'manual');
    set(gca, 'Position', axesPos);
    

    这应该允许注释对象保持固定在颜色条上的正确位置。

    【讨论】:

      【解决方案2】:

      这是另一种选择 - 只需添加/更改特定值的刻度标签:

      MarkTxt = '<-Mark';
      imagesc(rand(10)-2) % something to plot
      colormap('jet')
      CB = colorbar;
      
      % in case you DON'T want the value to appear:
      value = -1.8;
      t = find(CB.Ticks==value);
      if ~isempty(t)
          CB.TickLabels{t} = MarkTxt;
      else
          [CB.Ticks,ord] = sort([CB.Ticks value],'ascend');
          t = CB.Ticks==value;
          CB.TickLabels{t} = MarkTxt;
      end
      
      %% OR - in case you want the value to appear:
      value = -1.24;
      t = find(CB.Ticks==value);
      if ~isempty(t)
          CB.TickLabels{t} = [CB.TickLabels{t} MarkTxt];
      else
          [CB.Ticks,ord] = sort([CB.Ticks value],'ascend');
          t = CB.Ticks==value;
          CB.TickLabels{t} = [CB.TickLabels{t} MarkTxt];
      end
      

      没有值:

      具有值:

      【讨论】:

        【解决方案3】:

        您只需在绘图区域外添加text 并将其用作标记:

        A = rand(15);
        f=figure;
        imagesc(A);
        text(7,7,'X','Color','red') % Put a red X in the middle
        text(17.5,size(A,1)*(1-A(7,7)),'<marker'); % Set a marker on the colour bar for the middle point
        colorbar
        

        结果:

        我还没有找到一种方法在 颜色条上绘制标记,因为颜色条不是我可以在 uistack 中使用的绘图子项之一某种原因。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-23
          • 1970-01-01
          • 2015-08-16
          • 1970-01-01
          • 2017-07-15
          • 2016-06-22
          • 2020-11-13
          • 2014-01-12
          相关资源
          最近更新 更多