Avoid overlapping xline labels

32 visualizaciones (últimos 30 días)
Daniel Rowe
Daniel Rowe el 20 de Feb. de 2022
Comentada: Star Strider el 23 de Feb. de 2022
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)

Respuesta aceptada

Star Strider
Star Strider el 20 de Feb. de 2022
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 comentarios
Daniel Rowe
Daniel Rowe el 23 de Feb. de 2022
That is super useful information and explains why I couldn't itallicise any legends using sprintf. Thanks again!
Star Strider
Star Strider el 23 de Feb. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by