Offsetting the text coordinates on a plot
22 次查看(过去 30 天)
显示 更早的评论
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
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
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));
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Labels and Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!