matlab中双坐标轴的画法
例一:
[AX, H1, H2]=plotyy(n,l,n,s):figure(gcf);
set(H1,'Marker','*');
set(H2,'Marker','o');
legend([H1,H2],'XXXX','YYYY');
例二:
clc
clear all
close all
runoff=[10700 11400 15800 22900 43100 40700 50500 46000 41800
35000];
sed=[0.105 0.094 0.156 1.264 0.363 0.429 0.731 0.682 0.654 0.290];
m=1:10;
[ax,h1,h2]=plotyy(m,runoff,m,sed); %h-- line handle
set(get(ax(1),'Ylabel'),'string','Runoff (m^3/s))','color','r')
%y1
set(get(ax(2),'Ylabel'),'string','Sediment concentration
(kg/m^3)','color','k') %y2
xlabel('Month')
set(h1,'linestyle','-','color','r'); set(h2,'linestyle','- -','color','k');
legend([h1 h2],'runoff','sediment concentration') %标注两条线
legend('boxoff')
% box off
set(ax(:),'Ycolor','k') %设定两个Y轴的颜色为黑色
set(ax(1),'ytick',[0:10000:100000]); %设置y轴间隔
set(ax(2),'ytick',[0:0.1:1.5])
set(ax,'xlim',[1 12]) % 设置x轴范围
hold on
scatter(ax(1),4,22900,'r*')
axes(ax(2));
hold on
scatter(4,1.264,'ro')
坐标轴刻度设置
X坐标轴刻度数据点位置
set(gca, ’XTick’, [0 1 2])
X坐标轴刻度处显示的字符
set(gca,'XTickLabel',{'a','b','c'})
设置坐标轴刻度字体名称,大小
set(gca,'FontName','Times New Roman','FontSize',14)
'FontWeight’,’bold’ 加粗 ‘FontAngle’,’italic’ 斜体
对字体的设置也可以用在title, xlabel, ylabel等中
双坐标,多曲线的画法:
plotyy(x,[y1;y2],x,[y3;y4])
例如:
figure
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
y3 = sin(x);
[AX,H1,H2] = plotyy(x,[y1;100*y3],x,[y3;y2],'plot');
************************************
对原图双坐标的某一坐标加曲线:
x=linspace(0,2*pi,40);
[ax,h1,h2]=plotyy(x,sin(x)+cos(x),x,exp(x));
set(h1,'linestyle','-')
set(h2,'linestyle','-')
set(h1,'marker','o')
set(h2,'marker','+')
hold on
x=linspace(0,2*pi,40);
hh=line(x,cos(x));
set(hh,'linestyle','-')
set(hh,'marker','s')
hold on
hhf=line(x,sin(x));
set(hhf,'color','r')
set(hhf,'linestyle','-')
set(hhf,'marker','*')
legend([h1,h2,hh,hhf],'sin(x)+cos(x)','exp(x)','cos(x)','sin(x)',0);
********************************
my codes:
clear all
clc;
%**************
I = 0:0.005:4;
V1 = zeros(1,length(I));
Irr = 1000;
Temp = 25;
for i = 1:length(I)
V1(i) =
PV_model_current_test_noDiode(I(i),Irr,Temp);
end
P1=V1.*I;
%**************
I = 0:0.005:4;
V = zeros(1,length(I));
Irr = 300;
Temp = 25;
for i = 1:length(I)
V2(i) =
PV_model_current_test_noDiode(I(i),Irr,Temp);
end
P2=V2.*I;
%**************
figure(1);
[AX,H1,H2] = plotyy(V1,I,V1,P1);
set(H1,'LineStyle','-','LineWidth',4);
set(H1,'color','b');
set(H2,'LineStyle','-.','LineWidth',4);
set(H2,'color','b');
set(AX(1),'xlim',[0 45],'ylim',[0
5],'FontSize',40,'LineWidth',6,'Xcolor','k','Ycolor','k','FontName','Times
New Roman');
set(AX(2),'xlim',[0 45],'ylim',[0
65],'FontSize',40,'LineWidth',6,'Xcolor','k','Ycolor','k','FontName','Times
New Roman');
set(get(AX(1),'Ylabel'),'string','I
(A)','FontSize',40,'FontName','Times New Roman');
set(get(AX(2),'Ylabel'),'string','P
(W)','FontSize',40,'FontName','Times New Roman');
xlabel('V (V)');
set(AX(1),'ytick',[0:1:5]);
set(AX(2),'ytick',[0:13:65]);
hold on;
hhf1=plot(AX(1),V2,I);
set(hhf1,'color','k')
set(hhf1,'linestyle','-.','LineWidth',6)
hold(AX(2));
hhf2=plot(AX(2),V2,P2);
set(hhf2,'color','k')
set(hhf2,'linestyle',':','LineWidth',6)
hold on;
hhf3=plot(AX(1),V2+V1,I);
set(hhf3,'color','r')
set(hhf3,'linestyle','--','LineWidth',6)
hold on;
hhf4=plot(AX(2),V2+V1,P2+P1);
set(hhf4,'color','r')
set(hhf4,'linestyle',':','LineWidth',6)
grid off
Note: 可以先对AX1 和AX2分别画一条曲线,然后再用hold on;
plot来对两坐标添加更多的曲线。
本文详细介绍了如何在MATLAB中创建双坐标轴图形,包括实例代码和坐标轴设置。通过`plotyy`函数,可以同时展示不同尺度的数据,如在同一个图表上绘制Runoff和Sediment concentration。此外,还讲解了如何调整坐标轴刻度、颜色、字体样式,以及如何在已有双坐标轴图形上添加更多曲线。

4281

被折叠的 条评论
为什么被折叠?



