Avoid overlapping xline labels
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
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
2022 年 2 月 20 日
ok thank you. How would I incorporate that into my sprintf line code?
Star Strider
2022 年 2 月 20 日
I would so something like this —
sprintf(['%c' repmat(' ',1,10)],V_i{i})
That adds spaces at the end.
To demonstrate the effect, change the space to something else, for example a hyphen —
i = 1;
V_i{i} = '$';
sprintf(['%c' repmat('-',1,10)],V_i{i})
ans = '$----------'
.
Daniel Rowe
2022 年 2 月 20 日
the dashed line option works nicely to demonstrate, but my preference is for blank spaces after the mean value. I integrated this into the loop to get the graduated height differential but doesn't seem to work:
sprintf(['%c' repmat(' ',1,i*2)],V_i{i})
i.e. no spaces appear after printing the value
Star Strider
2022 年 2 月 20 日
It appears to work for me here —
figure
xl1 = xline(0.55, '-', 'Label 1');
xl2 = xline(0.56, '--', sprintf(['%s' repmat(' ',1,20)],'Label 2'));
xlim([0 1])

I cannot determine the reason it fails to work in your code. Perhaps you just need more spaces?
.
Daniel Rowe
2022 年 2 月 21 日
OK so the code works fine without the latex interpreter., which seems strange. Any idea why this might be, or how I might get around this?
Star Strider
2022 年 2 月 21 日
LaTeX requires a different way of specifying spaces.
They must be ‘escaped’, that is written as ‘\ ’, as illustrated here, both in the repmat call and the spaces in the ‘Label 2’ character vector —
figure
xl1 = xline(0.55, '-', 'Label 1');
xl2 = xline(0.56, '--', sprintf(['$%s' repmat('\\ ',1,20), '$'],'Label\ \ 2'), 'Interpreter','latex');
xlim([0 1])

Each space needs to be written as a separate ‘escaped’ space, '\ \ \ ', for example, specifying 3 consecutive spaces.
That should work.
.
Daniel Rowe
2022 年 2 月 21 日
Unfortunately, this does not work either. Instead, I receive a warning message:
Warning: Escaped character '\ ' is not valid. See 'doc sprintf' for supported special characters.
Star Strider
2022 年 2 月 21 日
It works in my code, and throws no errors.
I have no idea what your code currently looks like, so I can’t offer any suggestions. Note that in the repmat call in my code, the backslants are themselves ‘escaped’ (as '\\ ') since that’s necessary to use them in sprintf.
Daniel Rowe
2022 年 2 月 23 日
Works in isolation but I still can't get this to work in the code.
Star Strider
2022 年 2 月 23 日
They have to be ‘escaped’ in the sprintf format string. They work without being ‘escaped’ in argument strings.
Using my approach in your posted code xline call, it appears to work —
i = 1;
corr_bar(i) = 0.5;
V_i{i} = 'Z';
pos_h{i} = 'right';
figure
xlim([0.4 0.6])
x = xline(corr_bar(i),':',sprintf(['$$%c' repmat('\\ ',1,30) '$$'],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')

The problem may be with whatever ‘V_1{i}’ is, since I could make it work with ASCII alphabet letters, although other symbols such as ‘#’ failed and threw an error. That could be due to those characters being unacceptable to the LaTeX interpreter. If so, I have no idea if a work-around exists, other than their being represented in the Interpreter symbols (that then would also have to have ‘escaped’ backslants). (I tried this with both ‘%c’ and ‘%s’.)
.
Daniel Rowe
2022 年 2 月 23 日
Thanks again. This is the cell array in question:
V_i = {'u','v','w'};
Still won't work for me!
Star Strider
2022 年 2 月 23 日
As always, my pleasure!
This seems to work —
corr_bar = 0.04:0.01:0.06;
V_i = {'u','v','w'};
figure
xlim([0.02 0.08])
for i = 1:3
pos_h{i} = 'right';
x = xline(corr_bar(i),':',sprintf(['$%c' repmat('\\ ',1,i*10) '$'],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.FontSize = 16;
end

I also made the offset a function of ‘i’ so it varies automatically. (I added the 'FontSize' to make it easier to see.)
.
Daniel Rowe
2022 年 2 月 23 日
Very nice!
Star Strider
2022 年 2 月 23 日
Thank you!
Is it still not working correctly in your application?
Daniel Rowe
2022 年 2 月 23 日
Works fine now, thankfully.
Star Strider
2022 年 2 月 23 日
Great!
What was the problem? (Just curious.)
Daniel Rowe
2022 年 2 月 23 日
It was those dollar signs either side of the repmat function, but I'm not entirely sure what they achieve to be honest.
Star Strider
2022 年 2 月 23 日
The dollar signs tell the LaTeX interpreter to interpret everything between them. Without their being present, it won’t process anything and instead just ignore it and throw a Warning.
figure
text(0.2, 0.1, 'Text^2', 'Interpreter','latex')

Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
Text^2
String scalar or character vector must have valid interpreter syntax:
Text^2
figure
text(0.4, 0.1, '$Text^2$', 'Interpreter','latex')

.
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
2022 年 2 月 23 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
