plotyy: problem with ticks

4 views (last 30 days)
Marco Sammito
Marco Sammito on 10 Dec 2016
Commented: Mischa Kim on 10 Dec 2016
Hi. I have this graph but I cannot figure out how to adjust the ticks. Can you help me out?
[ax,h1,h2] = plotyy(t,R(:,1),[t,t],[fval,tensione_vapore]);
minimum_left = min(R(:,1));
maximum_left = max(R(:,1));
minimum_right = min(fval);
maximum_right = max(fval);
set(ax(1),'YTick',[minimum_left - 0.1 * minimum_left : (maximum_left - minimum_left) / 10 : maximum_left + maximum_left * 0.1])
set(ax(2),'YTick',[minimum_right - 0.1 * maximum_left : (maximum_right - minimum_right) / 10 : maximum_right + maximum_right * 0.1])
Edit The last two ticks on the right and left axis should be located at the corners (right and left).
function RP1_senza_loop()
R0 = 20e-6;
P0 = 101325;
Pmin = 83720.4;
f = 1e6 / 40;
Pvap = 84550;
tspan = [0 500e-6]; %
options = odeset('RelTol',1e-7,'AbsTol',1e-7);
[t,R] = ode45(@(t,R) DE(t,R,R0,P0,Pmin,f,Pvap), tspan, [R0,0],options);
%
fval = zeros(length(t),1);
for ii = 1 : length(t)
fval(ii) = P(t(ii),P0,Pmin,f);
end
%
t = t * 1e6;
R = R * 1e6;
tensione_vapore = Pvap * ones(length(t),1);
[ax,h1,h2] = plotyy(t,R(:,1),[t,t],[fval,tensione_vapore]);
xlabel('Time (\mus)')
ylabel(ax(1),'R (\mum)')
ylabel(ax(2),'Pressure (Pa)')
minimum_left = min(R(:,1));
maximum_left = max(R(:,1));
minimum_right = min(fval);
maximum_right = max(fval);
set(ax(1),'YTick',[minimum_left - 0.1 * minimum_left : (maximum_left - minimum_left) / 10 : maximum_left + maximum_left * 0.1])
set(ax(2),'YTick',[minimum_right - 0.1 * maximum_left : (maximum_right - minimum_right) / 10 : maximum_right + maximum_right * 0.1])
end
%
function Rdot = DE(t,R,R0,P0,Pmin,f,Pvap)
S = 0.05984;
rho = 961.5;
mi = 0.297e-3; % kg / (m * s)
c = 1481;
%
Rdot = zeros(2,1);
Rdot(1) = R(2);
Rdot(2) = -1.5 * R(2) * R(2) / R(1) + 1 / (R(1) * rho) *...
(Pvap - P(t,P0,Pmin,f) + (P0 - Pvap + 2 * S / R0) *...
(R0 / R(1))^3 - 2 * S / R(1) - 4 * mi * R(2) / R(1) +...
R(1) / c * (-3 * R0^3 / R(1)^4 * R(2) * (P0 - Pvap + 2 * S /R0) -...
derivata(t,P0,Pmin,f)));
end
%
function fval = P(t,P0,Pmin,f)
if (t <= 40e-6)
fval = P0;
elseif (t > 40e-6) && (t <= 60e-6)
fval = (P0 + Pmin) / 2 + (P0 - Pmin) / 2 * cos(2 * pi * f * t);
elseif (t > 60e-6) && (t <= 100e-6)
fval = Pmin;
elseif (t > 100e-6) && (t <= 120e-6)
fval = (P0 + Pmin) / 2 + (P0 - Pmin) / 2 * cos(2 * pi * f * t);
else
fval = P0;
end
end
%
function dPdt = derivata(t,P0,Pmin,f)
if (t <= 40e-6)
dPdt = 0;
elseif (t > 40e-6) && (t <= 60e-6)
dPdt = (Pmin - P0) * pi * f * sin(2 * pi * f * t);
elseif (t > 60e-6) && (t <= 100e-6)
dPdt = 0;
elseif (t > 100e-6) && (t <= 120e-6)
dPdt = (Pmin - P0) * pi * f * sin(2 * pi * f * t);
else
dPdt = 0;
end
end
I tried with this, but Matlab uses as left axis limit 100. It is too much.
leftlimits = ylim(ax(1));
rightlimits = ylim(ax(2));
set(ax(1),'YTick',[leftlimits(1) : (leftlimits(2) - leftlimits(1)) / 10 : leftlimits(2)])
set(ax(2),'YTick',[rightlimits(1) : (rightlimits(2) - rightlimits(1)) / 10 : rightlimits(2)])
  1 Comment
Mischa Kim
Mischa Kim on 10 Dec 2016
Unless you tell us how exactly the ticks should be adjusted we cannot help. Also, please attach entire code so we can reproduce.

Sign in to comment.

Accepted Answer

Mischa Kim
Mischa Kim on 10 Dec 2016
Marco, still not quite sure about the end result. How about adding
set(ax(1),'YLim',[minimum_left, maximum_left])
set(ax(2),'YLim',[minimum_right, maximum_right])
after you set the tick locations?
  3 Comments
Image Analyst
Image Analyst on 10 Dec 2016
Your step size is not rounded to integers so the in between values are fractions. You need to round this too: (maximum_left - minimum_left) / 10
Mischa Kim
Mischa Kim on 10 Dec 2016
Use, instead
set(ax(1),'YTick',round([minimum_left - 0.1 * minimum_left : (maximum_left - minimum_left) / 10 : maximum_left + maximum_left * 0.1]))
set(ax(2),'YTick',round([minimum_right - 0.1 * maximum_left : (maximum_right - minimum_right) / 10 : maximum_right + maximum_right * 0.1]))

Sign in to comment.

More Answers (1)

dpb
dpb on 10 Dec 2016
set(ax,{'ylim'},{[minimum_left maximum_left];[minimum_right maximum_right]})
See
doc set % for details on syntax for multiple values at one go...
Or, of course, just use ylim on each axes handle or the new-fangled methods for a more verbose method.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!