Avoid overlapping xline labels

조회 수: 21 (최근 30일)
Daniel Rowe
Daniel Rowe 2022년 2월 20일
댓글: Star Strider 2022년 2월 23일
Hi,
I'm trying to avoid overlapping xline labels. My code below attempts to identify the mean x,y and z components of the attached dataset 'corr'. Clearly I can play with the horizontal alignment command, but as I have multiple datasets with varying mean 'corr' values, I would prefer a more robust means of avoiding overlap, e.g. a slight vertical offset between the mean values (the 'LabelVerticalAlignment' command only offers limited options: top, middle and bottom - I would prefer all the labels to be positioned near the top of the graph, i.e. above the histogram, and avoiding any overlap). How would I achieve this?
Thanks
Here is my code.
% Plot correlation histogram
h = figure(2);
corr_bar = mean(corr(:,1:3));
for i = 1:3
H = histogram(corr(:,i:3),'FaceAlpha',0.4,'edgecolor','none');
H_max(i) = max(H.Values);
x = xline(corr_bar(i),':',sprintf('%c',V_i{i}),'LabelHorizontalAlignment',pos_h{i},'LineWidth',.5,'Color','b','HandleVisibility','off','interpreter','latex'); %sprintf('%c = %0.2f',V_i{i},round(corr_bar(i)*100)/100')
x.LabelHorizontalAlignment = 'center';
hold on
end
xlim([65 100])
ylim([0 max(H_max)*1.1])
xlabel('Correlation (\%)','interpreter','latex')
ylabel('Frequency (-)','interpreter','latex')
xline(70,'-.','Threshold','LineWidth',1,'Color','b','interpreter','latex')
legend('\it{u}','\it{v}','\it{w}','Location','southeast')
set(gca,'FontSize',12)
set(legend,'FontSize',12)

채택된 답변

Star Strider
Star Strider 2022년 2월 20일
The labels are always at the top of the xline so one option is just to pad the end of the label with spaces —
figure
xl1 = xline(0.55, '-', 'Label 1');
xl2 = xline(0.56, '--', ['Label 2' ' ']);
xlim([0 1])
There may be more elegant ways to accomplish this, however they do not appear to be in the xline properties. (This is obviously a text object, so there should be (x,y) position coordinates that can be changed, however it is not obvious to me how to find them in the properties, even using findobj.)
.
  댓글 수: 20
Daniel Rowe
Daniel Rowe 2022년 2월 23일
That is super useful information and explains why I couldn't itallicise any legends using sprintf. Thanks again!
Star Strider
Star Strider 2022년 2월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by