Filter löschen
Filter löschen

Offsetting the text coordinates on a plot

22 Ansichten (letzte 30 Tage)
Nicholas DeGarmo
Nicholas DeGarmo am 8 Dez. 2022
Beantwortet: Voss am 8 Dez. 2022
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 Kommentar
Jonas
Jonas am 8 Dez. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Voss
Voss am 8 Dez. 2022
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.

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by