필터 지우기
필터 지우기

Plotting a single curve with two x and y axes

조회 수: 5 (최근 30일)
Ammad Yousuf
Ammad Yousuf 2024년 3월 29일
편집: Drew 2024년 3월 29일
Hi,
I want to plot a signal on voltage scale on the left and as resistance on the right y axis. I want two x-axes one on bottom and one on top. They are directly proportional but the scale is different. I have tried it with following script:
figure('Position',[100 100 1000 800]);
ti= tiledlayout(1,1);
ax1= axes(ti);
ax2= axes(ti);
plot(ax1,disc_poistion,Vc_set(:,1),'lineWidth',1.5)
xlabel('Angular Position [Deg]')
ylabel('Voltage [V]')
plot(ax2,ball_position, Rc_set(:,1),'lineWidth',1.5)
ax2.XAxisLocation= 'top';
ax2.YAxisLocation= 'Right';
xlabel('Angular Position 2 [Deg]')
ylabe('Resistance [\Omega]')
ax=gca;
ax.FontSize= 20;
grid on
However, Im facing problems with the above code. It does plot the curve with two Y and X axes the formating like setting axes limits, changing font size and adding labels is done only on the later scale i.e. ax2. All the formating for the prior scale is reset. Attached is the plot generated from the above script.
with the other method of using yyaxis left and yyaxis right i get two overlapped curves with some offset, which I dont want. I have also gone through the following information page which I didnt find helpful for my case:
'https://uk.mathworks.com/help/matlab/creating_plots/graph-with-multiple-x-axes-and-y-axes.html?searchHighlight=two%20x-axes'
Any help with this would be highly appreciated.
Thanks,
Ammad.

답변 (1개)

Drew
Drew 2024년 3월 29일
편집: Drew 2024년 3월 29일
The documentation shows how to do what you want. Follow this example and you should be all set:
openExample('graphics/MultipleXandYaxesExample')
% Here is the example code without all the explanation ...
% I recommend to open the example to see the explanation.
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1,y1,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,x2,y2,'-k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
As for the issues in your code:
  • It looks like the reason that "ax.FontSize= 20;" did not change the fontsize of the first axes is because it should be "ax1", not "ax", that is: "ax1.FontSize= 20;" instead of "ax.FontSize= 20;".
  • The code you provided has syntax errors (for example, "ylabe" instead of "ylabel") and does not produce the plot you shared (for example, the right-side ylabel of "Contact Resistance" vs "Resistance"), so there is some mismatch somewhere. You also did not provide your data, or dummy data of similar scale.
I hope that helps, and please remember to accept the answer.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by