Offsetting the text coordinates on a plot

조회 수: 5 (최근 30일)
Nicholas DeGarmo
Nicholas DeGarmo 2022년 12월 8일
답변: Voss 2022년 12월 8일
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
Jonas
Jonas 2022년 12월 8일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2022년 12월 8일
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.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by