Changing subplot x limit

24 次查看(过去 30 天)
Emil Doyle
Emil Doyle 2021-1-27
I am trying to create 2 subplots in a figure, theta_1 vs. t and theta_2 vs. t. The range of t in both plots is 10. There are the same amount of data points in both plots. When I run the code however, the second subplot's range of t is increased
This is my code:
figure(1)
grid on
P1 = subplot(2,1,1)
plot(t,v(1,:),'linewidth',2)
xlabel('t','fontSize',14);
ylabel('\theta_1','fontSize',14);
P2 = subplot(2,1,2)
plot(t,v(2,:),'r','linewidth',2)
h=gca;
get(h,'fontSize')
set(h,'fontSize',14)
xlabel('t','fontSize',14);
ylabel('\theta_2','fontSize',14);
xlim(P2, [0 10]);%trying to specify xlim
fh = figure(1);
set(fh, 'color', 'white');
I even tried to specify the xlim for the second subplot but it made no difference. What am I doing wrong?
  3 个评论
dpb
dpb 2021-1-27
h=gca;
get(h,'fontSize')
set(h,'fontSize',14)
xlabel('t','fontSize',14);
ylabel('\theta_2','fontSize',14);
xlim(P2, [0 10]);%trying to specify xlim
Don't juse gca at all here; use the handles to the subplots exclusively.
It doesn't make any sense from just looking at the code that the range for the x axis is any different presuming the two t vectors are really the same t at the time the plot was created.
But, without the data it's not possible to prove, but cleaning it up and redoing would be the first step--
figure
P1 = subplot(2,1,1);
plot(t,v(1,:),'linewidth',2)
xlabel('t','fontSize',14);
ylabel('\theta_1','fontSize',14);
P2 = subplot(2,1,2);
plot(t,v(2,:),'r','linewidth',2)
xlabel('t','fontSize',14);
ylabel('\theta_2','fontSize',14);
Amrtanshu Raj
Amrtanshu Raj 2021-2-1
Hi,
can you share the data?

请先登录,再进行评论。

回答(0 个)

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by