注释在子图中的定位

Positioning of annotations in subplots

我必须生成不同传感器数据的图表。这些应该分别打印在子图中。生成绘图后,应该用一些矩形对其进行注释以突出不同的特征。之后,这些矩形应该用文本注释,用简短的词描述属于class。

令人惊讶的是,相同的代码在另一个脚本中运行,其中我不使用子图,而只使用一个频谱图。 我已经用annotation(gcf, ...)替换了annotation(h, ...),应该是一样的。添加 tempplot 而不是 h,这对我来说很有意义,

Sensorinput = rand(100,6);
Naminginput = {'a' 'b' 'c' 'd' 'e' 'f'}
sampledExperiment = [10, 10, 0;
                 30,10, 1; 
                 50,10, 0; 
                 70,10, 1; 
                 ]

experimentnaming = {0, 'classA';
                1, 'classB'
                }

samplerate = 10;                    

h = figure;
for index = 1:1:length(Sensorinput(1,:))
  tempplot = subplot(length(Sensorinput(1,:)),1,index);  
  plot(Sensorinput(:,index));
  title(Naminginput(index));
  pos = get (tempplot, 'position')
  step = pos(3)/length(Sensorinput(:,index));


  for index=1:1:length(sampledExperiment)

    annotation(h, "rectangle",[pos(1)+step*sampledExperiment(index,1),pos(2),step*sampledExperiment(index,2),pos(4)]);
    y  = ylim;
    for namingindex=1:1:length(experimentnaming(:,1))  
      if experimentnaming{namingindex,1}==sampledExperiment(index,3)
          text(sampledExperiment(index,1)/samplerate,y(2),experimentnaming(namingindex,2),'rotation',90);
      end
    end

  end

end

我希望盒子的高度与底层子图的高度相同。此外,我希望将文本(显然打印为堆栈)作为框的注释。 但这是我目前得到的结果:wrong result. Also, the boxes do not zoom correctly, when I resize the window. wrong_small_window。 这主要是什么在起作用:working with single figure。绘制具有子图全高的矩形并添加文本。这是使用相同的代码创建的,但没有子图。

希望你能帮忙?

我只是设法通过使用直接矩形而不是注释矩形来获得我想要的东西。使用这种类型,我可以使用数据 space 而不是数字 space 并且一切正常。

Sensorinput = rand(100,6);
Naminginput = {'a' 'b' 'c' 'd' 'e' 'f'};
sampledExperiment = [10, 10, 0;
                     30, 10, 1;
                     50, 10, 0;
                     70, 10, 1];
experimentnaming = {0, 'classA';
                    1, 'classB'};
samplerate = 10;

h = figure;
for index = 1:1:length(Sensorinput(1,:))
    tempplot = subplot(length(Sensorinput(1,:)),1,index);
    plot(Sensorinput(:,index));
    title(Naminginput(index));
    pos = get (tempplot, 'position')
    step = pos(3)/length(Sensorinput(:,index));

    for index=1:1:length(sampledExperiment)        
        rectangle('Position',[sampledExperiment(index,1),0,sampledExperiment(index,2),1]);
        y  = ylim;
        for namingindex=1:1:length(experimentnaming(:,1))
            if experimentnaming{namingindex,1}==sampledExperiment(index,3)                    
                text(sampledExperiment(index,1)+0.5*sampledExperiment(index,2), y(2),experimentnaming(namingindex,2),'rotation',90);
            end
        end            
    end        
end

我在绘制文本时还混淆了数据 space 和图形 space。

感谢您通过工作示例进行说明。这是使用 area 函数的另一种更简单/更美观的方法。

Sensor = struct ('input', num2cell (rand (6, 100), 2), ...
                 'name',  {'a'; 'b'; 'c'; 'd'; 'e'; 'f'});
Experiment = struct ('samplestart', { 10     ,  30     ,  50     ,  70}, ...
                     'samplewidth', { 10     ,  10     ,  10     ,  10}, ...
                     'classindex',  { 0      ,  1      ,  0      ,  1 });

ClassNames     = {'classA', 'classB'};
ClassColours   = {[1.0, 0.8, 0.8], [0.8, 0.8, 1.0]};

NumPlots = length (Sensor);
for n = 1 : NumPlots
  subplot(NumPlots, 1, n); hold on;
  for m = Experiment
    Hndl = area ([m.samplestart, m.samplestart + m.samplewidth], [1, 1]);
    set (Hndl, 'facecolor', ClassColours{m.classindex + 1});
    Hndl = text (m.samplestart+2, 1.2, ClassNames{m.classindex+1});
  end
  plot ([1:100], Sensor(n).input, 'k', 'linewidth', 2);
  text (-7.5, -0.25, Sensor(n).name, 'fontsize', 16, 'fontweight', 'bold');
end