Offsetting the text coordinates on a plot

Hi I just wanted to know how i could offset the text on the plot so each point is readable, as of right now each the text for the points are overlapping
tmat = [0 t1 t2 t3 t4 t5 t6 t7 t8]/3600
plot(tmat,hmat,'-',tmat,SpERm,'-', 'Marker','square')
for p = 1:numel(tmat)
text(tmat(p)+p,hmat(p)+p,['(',num2str(tmat(p)),',',num2str(hmat(p)),')'])
end

1 comentario

Jonas
Jonas el 8 de Dic. de 2022
probably it is the easiest to rotate your text in such a way, that it is written vertically. For this, add the Name-Value Pair 'Rotation',90 or Rotation,-90

Iniciar sesión para comentar.

Respuestas (1)

Making up some data:
t1 = 5.9848;
t2 = 7.623;
t3 = 9.234;
t4 = 10.982;
t5 = 13.223;
t6 = 14.765;
t7 = 16.023;
t8 = 18.122;
tmat = [0 t1 t2 t3 t4 t5 t6 t7 t8];
hmat = 69088.7639*ones(1,numel(tmat));
SpERm = zeros(1,numel(tmat));
Here's one thing you can try, based on @Jonas's suggestion:
figure
plot(tmat,hmat,'-',tmat,SpERm,'-', 'Marker','square')
for p = 1:numel(tmat)
if mod(p,2)
alignment = 'bottom';
rotation = +45;
else
alignment = 'top';
rotation = -45;
end
text(tmat(p),hmat(p), ...
['(',num2str(tmat(p)),',',num2str(hmat(p)),')'], ...
'VerticalAlignment',alignment, ...
'Rotation',rotation)
end
xlim([-1 25])
ylim([-1.5e4 1e5]);
Here's another thing you can try:
% this code will apply a vertical offset to each text, as follows:
% text 1: offset = +0
% text 2: offset = -0
% text 3: offset = +abs_offset
% text 4: offset = -abs_offset
% text 5: offset = +2*abs_offset
% text 6: offset = -2*abs_offset
% text 7: offset = +0
% text 8: offset = -0
% text 9: offset = +abs_offset
% text 10: offset = -abs_offset
% etc.
%
% that is, alternating positive and negative offsets of magnitude
% increasing by "abs_offset" each pair, until "n_offset_levels" is reached,
% then restarting at offset=0.
figure
plot(tmat,hmat,'-',tmat,SpERm,'-', 'Marker','square')
yl = [-1.5e4 1e5];
abs_offset = 0.045*diff(yl); % adjust the offset level spacing
n_offset_levels = 3; % and number of levels (on one side, + or -)
n = numel(tmat);
offsets = abs_offset*mod(repelem(0:ceil(n/2),1,2),n_offset_levels);
offsets(2:2:end) = -offsets(2:2:end);
for p = 1:n
if mod(p,2)
alignment = 'bottom';
else
alignment = 'top';
end
text(tmat(p),hmat(p)+offsets(p), ...
['(',num2str(tmat(p)),',',num2str(hmat(p)),')'], ...
'VerticalAlignment',alignment)
end
xlim([-1 25])
ylim(yl);
You can also try to shorten the text strings, e.g., using sprintf, to have more space to place them without overlapping.

Categorías

Más información sobre Large Files and Big Data en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 8 de Dic. de 2022

Respondida:

el 8 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by