Matlab - 如何在不同的y轴上绘图

时间:2017-04-21 10:38:07

标签: matlab plot double axis

我想在同一图表上绘制电动机的功率和扭矩。 我使用一个循环绘制三角星配置,还有一个双齿轮系统,所以我想绘制每个配置的2系列数据(功率和扭矩)。 我尝试了这个,但我注意到在第二个循环中它绘制在第二个轴,而不是在第一个轴。我使用matlab 2015,所以我没有yyaxis function

clc; clear all;close all

tau=[8.823;1.604]; %in ordine decrescente
n_gamme=length(tau);
data=[22 22;575 1200;1500 6000;365 175];%P,Ginocchio,rpm max,C
names={'Star';'Delta'};

nN=zeros(2);nmax=zeros(2);M_N=zeros(2); %inizializzo

for i=1:n_gamme %contatore gamme
%     j=1; se non ho stella-triangolo
for j=1:2 %contatore stella-triangolo
    P=data(1); %potenza max
    nN(i,j)=data(2,j)./tau(i); %rpm, ginocchio 
    nmax(i,j)=data(3,j)/tau(i); %giri max
    M_N(i,j)=data(4,j)*tau(i); %coppia max
end    
end

PL=['r','r-';'g','g-'];

%     j=1; %se non ho stella-triangolo
for j=1:2 %contatore stella-triangolo
%         subplot(length(tau),1,j);
%         figure

    x1(j,:) = linspace(0,nN(1,j)); %numero di giri da 0 al ginocchio 1
    x2(j,:) = linspace(nN(1,j),nmax(1,j)); %da ginocchio 1 a max 1
    x3(j,:) = linspace(0,nN(2,j)); %da 0 a ginocchio 2
    x4(j,:) = linspace(nN(2,j),nmax(2,j)); %da ginocchio 2 a max 2

    C_1(1,j)=M_N(1,j); %coppia costante 1 marcia
    C_2(j,:)=30*P*1000./(pi*x2(j,:)); %coppia 1 marcia
    C_3(2,j)=M_N(2,j); %coppia costante 2 marcia
    C_4(j,:)=30*P*1000./(pi*x4(j,:)); %coppia 2 marcia

    ax1 = gca;
    plot(ax1,[0,nN(1,j)],[C_1(1,j) C_1(1,j)],PL(1,j),'LineWidth',2/j^2) %I coppia costante
    hold on
    plot(ax1,x2(j,:),C_2(j,:),PL(2,j),'LineWidth',2/j^2) %coppia I
    plot(ax1,[max(x2(j,:)),nN(2,j)],[C_3(2,j) C_3(2,j)],PL(1,j),'LineWidth',2/j^2) %II coppia costante
    plot(ax1,x4(j,:),C_4(j,:),PL(2,j),'LineWidth',2/j^2) %coppia II
    ylim([0 3500])
    ylabel(ax1,'Torque [Nm]'); %# Add a label to the left y axis
    set(ax1,'Box','off');%# Turn off the box surrounding the whole axes

   axesPosition = get(gca,'Position'); 
   ax2 = axes('Position',axesPosition,...
   'YLim',[0 30],...            %#   and a different scale
   'XAxisLocation','top',...
   'YAxisLocation','right',...
   'Color','none',...
   'XColor','b','YColor','b',...
   'Box','off');                %#   ... and no surrounding box

    hold on
    m=P./nN; %pendenza potenza
    P_1(j,:)=m(1,j).*x1(j,:); %potenza 1 marcia
    P_2(1,j)=P; %Potenza costante 1 marcia
    P_3(j,:)=m(2,j).*x3(j,:); %Potenza 2 marcia
    P_4(2,j)=P; %Potenza costante 2 marcia

    plot(ax2,x1(j,:),P_1(j,:),'b--') 
    plot(ax2,[max(x1(j,:)),nmax(1,j)],[P P],'b--')      
    plot(ax2,x3(j,:),P_3(j,:),'b--') 
    plot(ax2,[max(x3(j,:)),nmax(2,j)],[P P],'b--') 
    ylabel(ax2,'Power [kW]');

    linkaxes([ax1 ax2],'x'); %lega tra di loro gli assi,
    grid on
    grid minor
end   

我如何以相同的比例绘图

2 个答案:

答案 0 :(得分:1)

在R2016a之前,你应该使用plotyy来表示你想要的那种情节。

答案 1 :(得分:1)

原因是您使用ax1 = gcagca是当前轴,在完成第一次迭代后,当前轴是第二个,因为它是您绘制的最后一个轴。

要解决此问题,请在循环之前添加轴创建ax1 = axes,并将ax1 = gca替换为axes(ax1),以便ax1成为当前轴。

注意:另请注意,您要创建ax2 两次。这意味着实际上有两个相同的轴具有完全相同的位置和属性。更好的做法是在for循环之前移动所有轴,并且循环仅用于绘制它们。我会在循环之前写下以下内容:

ax1 = axes;
hold on;
ylim([0 3500])
ylabel(ax1,'Torque [Nm]'); %# Add a label to the left y axis
set(ax1,'Box','off');%# Turn off the box surrounding the whole axes

axesPosition = get(ax1,'Position'); 
ax2 = axes('Position',axesPosition,...
   'YLim',[0 30],...            %#   and a different scale
   'XAxisLocation','top',...
   'YAxisLocation','right',...
   'Color','none',...
   'XColor','b','YColor','b',...
   'Box','off');                %#   ... and no surrounding box

hold on;
ylabel(ax2,'Power [kW]');
linkaxes([ax1 ax2],'x'); %lega tra di loro gli assi,
xlim([0,3800]);

grid on
grid minor

现在循环就是这样:

for j=1:2 %contatore stella-triangolo
%         subplot(length(tau),1,j);
%         figure

    x1(j,:) = linspace(0,nN(1,j)); %numero di giri da 0 al ginocchio 1
    x2(j,:) = linspace(nN(1,j),nmax(1,j)); %da ginocchio 1 a max 1
    x3(j,:) = linspace(0,nN(2,j)); %da 0 a ginocchio 2
    x4(j,:) = linspace(nN(2,j),nmax(2,j)); %da ginocchio 2 a max 2

    C_1(1,j)=M_N(1,j); %coppia costante 1 marcia
    C_2(j,:)=30*P*1000./(pi*x2(j,:)); %coppia 1 marcia
    C_3(2,j)=M_N(2,j); %coppia costante 2 marcia
    C_4(j,:)=30*P*1000./(pi*x4(j,:)); %coppia 2 marcia

    plot(ax1,[0,nN(1,j)],[C_1(1,j) C_1(1,j)],PL(1,j),'LineWidth',2/j^2) %I coppia costante
    plot(ax1,x2(j,:),C_2(j,:),PL(2,j),'LineWidth',2/j^2) %coppia I
    plot(ax1,[max(x2(j,:)),nN(2,j)],[C_3(2,j) C_3(2,j)],PL(1,j),'LineWidth',2/j^2) %II coppia costante
    plot(ax1,x4(j,:),C_4(j,:),PL(2,j),'LineWidth',2/j^2) %coppia II

    m=P./nN; %pendenza potenza
    P_1(j,:)=m(1,j).*x1(j,:); %potenza 1 marcia
    P_2(1,j)=P; %Potenza costante 1 marcia
    P_3(j,:)=m(2,j).*x3(j,:); %Potenza 2 marcia
    P_4(2,j)=P; %Potenza costante 2 marcia

    plot(ax2,x1(j,:),P_1(j,:),'b--') 
    plot(ax2,[max(x1(j,:)),nmax(1,j)],[P P],'b--')      
    plot(ax2,x3(j,:),P_3(j,:),'b--') 
    plot(ax2,[max(x3(j,:)),nmax(2,j)],[P P],'b--') 

end