【问题标题】:Positioning of annotations in subplots在子图中定位注释
【发布时间】:2017-12-06 10:53:03
【问题描述】:

我必须生成不同传感器数据的图。这些应该打印在每个子图中。生成绘图后,应使用一些矩形对其进行注释以突出显示不同的特征。之后,这些矩形应该用一个文本进行注释,用一个简短的词来描述所属类。

令人惊讶的是,相同的代码正在另一个脚本中工作,我不使用子图,而只使用一个谱图。 我已经用注释(gcf,...)替换了注释(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。此外,当我调整窗口大小时,这些框不会正确缩放。 wrong_small_window。 这主要是什么工作:working with single figure。用子图的全高绘制矩形并添加文本。这是使用相同的代码创建的,但没有子图。

希望你能帮忙?

【问题讨论】:

  • 仍然不确定你在问什么,也许你可以注释你的图像/放一张手绘图/正确的例子?此外,这里的代码没有意义,因为它不是一个独立的示例。考虑编写一个可以复制/粘贴并直接在 matlab 中运行的最小工作示例。但是,作为一般建议,如果您想要微调手动控制,我建议直接使用axes 放置“子图”而不是subplot 命令;您可以使用line 在您想要的位置手动绘制框;
  • (cont.) 最后,如果您希望文本沿 x 轴移动而事实并非如此,那么您可能在 text 参数中指的是 namingindex 而不是 index,因为后者在你的内部 for 循环内不会改变。
  • 感谢您的 cmets。我为没有子图的程序添加了一些示例和图像的代码,它运行良好。希望现在更清楚了。

标签: octave matlab-figure subplot


【解决方案1】:

感谢您通过一个工作示例进行澄清。这是使用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

【讨论】:

  • 谢谢。我更新了另一个类,所以它就像你的例子一样工作。
【解决方案2】:

我只是通过使用直接矩形而不是注释矩形来获得我想要的。使用这种类型,我可以使用数据空间而不是图形空间,一切都很好。

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

在绘制文本时,我还将数据空间与图形空间混合在一起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多