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)
采纳的回答
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 个评论
ok thank you. How would I incorporate that into my sprintf line code?
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 = '$----------'
.
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
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?
.
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?
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.
.
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.
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.
Works in isolation but I still can't get this to work in the code.
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’.)
.
Thanks again. This is the cell array in question:
V_i = {'u','v','w'};
Still won't work for me!
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.)
.
Very nice!
Thank you!
Is it still not working correctly in your application?
Works fine now, thankfully.
Great!
What was the problem? (Just curious.)
It was those dollar signs either side of the repmat function, but I'm not entirely sure what they achieve to be honest.
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')

.
That is super useful information and explains why I couldn't itallicise any legends using sprintf. Thanks again!
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!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
