Offsetting the text coordinates on a plot
23 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1224112/image.jpeg)
1 Commento
Jonas
il 8 Dic 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
Risposte (1)
Voss
il 8 Dic 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));
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 Commenti
Vedere anche
Categorie
Scopri di più su Labels and Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!