Set XTick of yyaxis to have integer ticks only

I have a plot with double y axes using yyaxis. I want to set XTick to have only integer values, e.g., 1, 2, 3, 4, 5, 6, instead of 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6.
With normal plot, I can just use:
ax = gca;
ax.XTick = unique(round(ax.XTick));
to achieve this goal.
But this seems not the case for yyaxis plot.
Please help!
---
Update: here is a MWE to illustrate my problem:
figure('Position', get(0, 'Screensize'));
max_iter = 100;
A_history = nan(1,max_iter); B_history = nan(1,max_iter);
for iter = 1 : max_iter
yyaxis left;
A_history(iter) = randn(1);
plot(A_history(1:iter), '-ob', 'LineWidth', 0.9, 'MarkerSize', 4.5, 'MarkerFaceColor', 'b');
ylabel('first yaxis');
yyaxis right;
B_history(iter) = randn(1);
plot(B_history(1:iter), '-or', 'LineWidth', 0.6, 'MarkerSize', 3, 'MarkerFaceColor', 'r');
ylabel('second yaxis');
% ax = gca;
% ax.XTick = unique(round(ax.XTick));
pause(1);
end
The above code will create xticks of 0, 0.1, 0.2, 0.3, 0.4, ..., 1 for the 1st iteration; xticks of 1, 1.1, 1.2, 1.3, 1.4, ..., 2 for the second iteration, for example.
What I want is to get rid of all the non-integer values (e.g., 0.1, ... ,0.9, 1.1, ... ,1.9) in the xticks, just leave integer values (e.g., 0,1 for the 1st iteration; 1,2 for the second iteration).
I have tried to add the commented 2 lines of code. But they don't give me the expected result, unfortunately. What's worse is that the xticks would become [0,1] all the time...
Hope this makes the problem description clearer.

3 Kommentare

It should work.
yyaxis right
plot(rand(1,20)*4, rand(1,20), '*')
yyaxis left
plot(rand(1,20)*6, rand(1,20), '*')
ax = gca;
ax.XTick = unique(round(ax.XTick));
hmhuang
hmhuang am 15 Nov. 2021
Bearbeitet: hmhuang am 15 Nov. 2021
@Adam Danz My xticks are actually changing in each iteration, not fixed as your example shown. Can you please have a look at my updated post? Thanks!
Adam Danz
Adam Danz am 15 Nov. 2021
Then you probably just need to set xlim.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 15 Nov. 2021
Bearbeitet: dpb am 15 Nov. 2021

0 Stimmen

yyaxis right
plot(rand(1,20)*4, rand(1,20), '*')
yyaxis left
plot(rand(1,20)*6, rand(1,20), '*')
hAx=gca; % retrieve handle to current axes
hAx.YAxis(2).TickLabelFormat='%d'; % set RH y axis format string to integer
NB: the subscript on the YAxis handle array -- the specialized axes object created by yyaxis is a super axes container that has both LH and RH axes objects inside. The first array element is LH, the second the RH axis.

5 Kommentare

hmhuang
hmhuang am 15 Nov. 2021
Bearbeitet: hmhuang am 15 Nov. 2021
@dpb Sorry...This is not what I want....Please see the updated post.
dpb
dpb am 15 Nov. 2021
Bearbeitet: dpb am 15 Nov. 2021
...
xlm=xlim;
xticks(xlm(1):xlm(2))
end
hmhuang
hmhuang am 15 Nov. 2021
Bearbeitet: hmhuang am 15 Nov. 2021
@dpb Thanks! But this will cause overlapped xticks in the end when we have a large max_iter, say 1000.
Adam Danz
Adam Danz am 15 Nov. 2021
Bearbeitet: Adam Danz am 15 Nov. 2021
In the example from your question,
% To show the full plot from the start of the animation
ax = gca();
ax.XLim = [1,max_iter];
ax.XTick = unique(round(linspace(1,max_iter, 10))); % for up to 10 ticks
or
% To animate the x axis limits
ax = gca();
ax.XLim = [0,iter];
ax.XTick = unique(round(linspace(1,max_iter, 10))); % for up to 10 ticks
...
xtk=xticks;
xticks(xtk(xtk==fix(xtk)))
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 15 Nov. 2021

Kommentiert:

dpb
am 15 Nov. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by