Axis ticks alternating between appearing on either side of the plot.
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Johannes
am 27 Jun. 2021
Beantwortet: DGM
am 27 Jun. 2021
Hi everyone,
I have a question regarding ticks. I want to have the x-ticks where the normal eigenfrequencies are located. However, at times some eigenfrequencies are close to one another. An easy fix for me would be to alternate with the ticks, having some of them show on the left and some on the right. Is there a way to do that?
Thanks in advance and best regards
1 Kommentar
DGM
am 27 Jun. 2021
I don't know that doing so would accomplish anything that's not still ambiguous. Even if you were to offset the ticks, in cases like the ones near 99, it's still hard to tell which one is which. Color-coding the labels would help, but as far as I know, tick labels can't be individually colored. Using annotations might be an option though.
Akzeptierte Antwort
Scott MacKenzie
am 27 Jun. 2021
I think this achieves what you are after (although I concur with @DGM that it may not fully resolve the issue you note):
% test data
y = rand(8,10);
y = [repmat(y(:,1),1,3), y];
n = size(y,1); % number of signals
x = 0:size(y,2)-1;
tiledlayout(2,1);
nexttile;
plot(x, y');
yticks(sort(y(:,1)));
nexttile;
yLeft = y(1:2:n,:); % odd signals with left axis ticks
yRight = y(2:2:n,:); % even signals with right axis ticks
yyaxis left;
plot(x,yLeft', '-');
yticks(sort(yLeft(:,1)));
hold on;
yyaxis right;
plot(x,yRight', '-');
yticks(sort(yRight(:,1)));
Weitere Antworten (1)
DGM
am 27 Jun. 2021
For what it's worth,
nsmp = 20; % test data parameters
nsig = 8;
w = 0.9; % label spreading weight
% make random test data
x = 0:nsmp-1;
y = rand(nsig,nsmp-5) + rand(nsig,1)*5;
y = [repmat(y(:,1),1,5), y];
hg = plot(x,y);
% blindly spread out labels to try to keep them from colliding
% this is a weighted average of given spacing and uniform spacing
[yv idx] = sort(y(:,1),'ascend');
yl = linspace(min(y(:,1)),max(y(:,1)),numel(y(:,1))).'
ya = w*yv + (1-w)*yl;
ya(idx) = ya;
for p = 1:nsig
[xx yy] = datc2figc([-0.5 0],[ya(p) y(p,1)]); % see included file
ha = annotation('textarrow',xx,yy,'string',num2str(y(p,1),4));
ha.Color = get(hg(p),'color'); % make color match
ha.HeadStyle = 'none';
end
set(gca,'ytick',[],'yticklabel',[]) % get rid of regular y ticks and labels
I admit that trying to deal with annotations is a great way to lose your hair. The included file (or something which accomplishes the same) is almost universally necessary in order to use annotations for any typical purpose.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!