간단한 레이블을 만드는 서브 플로트를 제공하고 싶습니다. 불행히도 나는 추한 행동을하고 있습니다.matlab 그림의 서브 플로트 레이블
figure;
h1 = axes('OuterPosition', [0,0,.5 1]);
set(h1,'LooseInset',get(h1,'TightInset'));
h2 = axes('OuterPosition', [.5,0,.5 1]);
set(h2,'LooseInset',get(h2,'TightInset'));
axes(h1);
plot([0 1], [4 5]);
set_label1('A');
axes(h2);
plot([0 1], [4 5]);
set_label1('B');
내가 얻을 그림은 다음과 같습니다 : 여기
function h = set_label1(label)
tlh = get(gca, 'Title');
if strcmp(get(tlh, 'String'), '')
title(' ');
end
ylh = get(gca, 'YLabel');
if strcmp(get(ylh, 'String'), '')
ylabel(' ');
end
ylp = get(ylh, 'Position');
x = ylp(1);
tlp = get(tlh, 'Position');
y = tlp(2);
h = text('String', label, ...
'HorizontalAlignment', 'right',...
'VerticalAlignment', 'Baseline', ...
'FontUnits', 'pixels', ...
'FontSize', 16, ...
'FontWeight', 'bold', ...
'FontName', 'Arial', ...
'Position', [x y 0]);
end
는 간단한 테스트 실행 : 다음과 같은 기능을 고려하면 그림 크기를 조정하면
라벨은하지 않습니다 더 이상 올바른 위치에 있어야합니다. 괜찮 았어, 나는 그것을 기대했다. (그들이 속한 곳으로 되돌려 놓는 방법을 안다면 나에게 매우 행복해 할 것이라고 말한다.)
제가 직면 한 문제는 '데이터'단위로 라벨의 위치를 지정하고 싶지 않다는 것입니다. 대신 정규화 된 단위를 사용하고 싶습니다. 그래서 나는 수정 된 형태의 함수를 사용했다. 이제 우리는이를 사용하자 :
function h = set_label2(label)
tlh = get(gca, 'Title');
if strcmp(get(tlh, 'String'), '')
title(' ');
end
ylh = get(gca, 'YLabel');
if strcmp(get(ylh, 'String'), '')
ylabel(' ');
end
oldUnits = replace_prop(ylh, 'Units', 'normalized');
ylp = get(ylh, 'Position');
x = ylp(1);
set(ylh, 'Units', oldUnits);
oldUnits = replace_prop(tlh, 'Units', 'normalized');
tlp = get(tlh, 'Position');
y = tlp(2);
set(ylh, 'Units', oldUnits);
h = text('String', label, ...
'HorizontalAlignment', 'right',...
'VerticalAlignment', 'Baseline', ...
'FontUnits', 'pixels', ...
'FontSize', 16, ...
'FontWeight', 'bold', ...
'FontName', 'Arial', ...
'Units', 'normalized',...
'Position', [x y 0]);
end
function oldvalue = replace_prop(handle, propName, newvalue)
oldvalue = get(handle, propName);
set(handle, propName, newvalue);
end
을 동일한 테스트를 실행 :
figure;
h1 = axes('OuterPosition', [0,0,.5 1]);
set(h1,'LooseInset',get(h1,'TightInset'));
h2 = axes('OuterPosition', [.5,0,.5 1]);
set(h2,'LooseInset',get(h2,'TightInset'));
axes(h1);
plot([0 1], [4 5]);
set_label2('A');
axes(h2);
plot([0 1], [4 5]);
set_label2('B');
우리는 이전과 똑같은 사진을 얻을 수 있습니다. 유일한 문제는 우리가 나쁜 일을 크기를 조정할 때 발생하는 것입니다 :
레이블이 올바른 위치에 실제로. 그러나 내가 사용한 'LooseInset'
과 'TightInset'
속성은 축이 레이블이없는 것처럼 작동합니다. 이 문제가 수정 되었습니까? 내가하는 모든 일은 정규화 된 단위의 제목과 ylabel의 위치를 데이터 단위와 반대되게하는 것입니다. 그리고 이것은 이것을 엉망으로 만드는 것 같습니다.
정규화 된 단위로 가져와야하는 이유는 3D 플롯을 얻을 때 제목과 zlabel에 대해 레이블을 배치 할 수 있기 때문입니다. 모든
테스트 코드에서'LooseInset'을 변경하는 코드를 제거하더라도 여전히'set_label2'와 동일한 동작을합니다. – jmlopez
그런데'LooseInset'은 [문서화되지 않은] (http://undocumentedmatlab.com/blog/axes-looseinset-property/) 속성 – Amro