figure
x=0:0.001:20;
y1=sin(x);
y2=cos(x);
y3=2*sin(x);
y4=2*cos(x);
subplot(221);plot(x,y1);title('sin');xlabel('x');ylabel('y');
subplot(222);plot(x,y2);title('cos');xlabel('x');ylabel('y');
subplot(223);plot(x,y3);title('2sin');xlabel('x');ylabel('y');
subplot(224);plot(x,y4);title('2cos');xlabel('x');ylabel('y');
基于上述几个问题,初步思路为:
FontWeight
调整字体粗细,可选的属性有:FontWeight | 含义 |
---|---|
normal | 正常 |
bold | 加粗 |
''
代替set(gca,'position',[mag mag1 big big])
改变图的大小set(gca,'XTick',x0,'XTicklabel',x1);
,不想显示的用''
代替,set(gca,'XLim',[min, max]);
改变坐标范围。关于图片位置及大小情况,首先要了解matlab的绘图机制。先来看一个简单例子:
figure
set(gcf,'position',[100 0 800 600]);
subplot(221)
set(gca,'position',[0.05 0.55 0.4 0.4],'xtick',[],'ytick',[],'box','on');
字体的常用属性有以下几个:
属性 | 含义 | 取值 |
---|---|---|
FontName | 字体名称 | Arial、Times New Roman,etc. |
FontSize | 字体大小 | 10 number,etc. |
FontWeight | 字体粗细 | normal(正常)、bold(加粗) |
FontAngle | 字体倾斜 | italic(意大利斜体)、normal(正常) |
set(gca,'FontName','Times New Roman','FontSize',14,'FontWeight','bold','FontAngle','italic' )%设置坐标轴刻度字体名称,大小,加粗 ,斜体;
title('tit','FontWeight','normal');
set(gca,'XLim',xl1,'XTick',xt1,'XTicklabel',xtl1);
XLim
指的是刻度范围,xl1
是一个一行二列的矩阵;XTick
指的是标刻度的位置,xt1
一般是数值型的;XTicklabel
指的是在标刻度的文字内容,xtl1
一般是char型的;xt1
和xtl1
需要长度一致,在某个刻度处若不想显示内容,可以用''
占位。
function draw2()
figure
x=0:0.001:20;
y1=sin(x);
y2=cos(x);
y3=2*sin(x);
y4=2*cos(x);
wid=800;
hei=600;
bili=wid/hei;
wid1=0.43;
lefx=1-wid1*2;
lefy=lefx*bili;
hei1=(1-lefy)/2;
magx=lefx*8/14; %margin x
intx=lefx*3/14; %interval x
magy=lefy*8/14; %margin y
inty=lefy*3/14; %interval y
magx1=magx+wid1+intx;
magy1=magy+hei1+inty;
xt=0:5:10;xtl1=sprintfc('%g',xt);xtl2={'','',''};
yt1=-1:1;ytl1=sprintfc('%g',yt1);ytl3={'','',''};
yt2=-2:2:2;ytl2=sprintfc('%g',yt2);
set(gcf,'position',[0 0 wid hei])
subplot(221)
set(gca,'position',[magx magy1 wid1 hei1],'xtick',[],'ytick',[],'box','on')
plot(x,y1);
set(gca,'XLim',[0, 11]);
set(gca,'YLim',[-1.5, 1.5]);
title('sin','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'YTick',yt1,'YTicklabel',ytl1);
set(gca,'XTick',xt,'XTicklabel',xtl2);
ylabel('y');
subplot(222)
set(gca,'position',[magx1 magy1 wid1 hei1],'xtick',[],'ytick',[],'box','on')
plot(x,y2);
set(gca,'XLim',[0, 11]);
set(gca,'YLim',[-1.5, 1.5]);
title('cos','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'YTick',yt1,'YTicklabel',ytl3);
set(gca,'XTick',xt,'XTicklabel',xtl2);
subplot(223)
plot(x,y3);
set(gca,'XLim',[0, 11]);
set(gca,'YLim',[-3, 3]);
set(gca,'position',[magx magy wid1 hei1],'xtick',[],'ytick',[],'box','on')
title('2sin','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'YTick',yt2,'YTicklabel',ytl2);
set(gca,'XTick',xt,'XTicklabel',xtl1);
xlabel('x');
ylabel('y');
subplot(224)
plot(x,y4);
set(gca,'XLim',[0, 11]);
set(gca,'YLim',[-3, 3]);
set(gca,'position',[magx1 magy wid1 hei1],'xtick',[],'ytick',[],'box','on')
title('2sin','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'YTick',yt2,'YTicklabel',ytl3);
set(gca,'XTick',xt,'XTicklabel',xtl1);
xlabel('x');
end
ps:看起来好像也没有很好看,笔者随便找的例子体现不出效果,其他的数据也许会好看一点。
关于四子图的情况,笔者编写了两种分图函数,一种分图的思想为:子图比例与大图比例保持相同;另一种分图的思想为:横向边缘与竖向边缘宽度相同。读者可根据自己的需求选取并结合自己的绘图内容加以扩充使用。两种函数如下:
function draw4()
figure
wid=1000;
hei=600;
big=0.43;
lef=1-big*2;
mag=lef*8/14; %margin
int=lef*3/14; %interval
mag1=mag+big+int;
set(gcf,'position',[0 0 wid hei])
subplot(221)
title('GPS-IF','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'position',[mag mag1 big big],'xtick',[],'ytick',[],'box','on')
subplot(222)
set(gca,'position',[mag1 mag1 big big],'xtick',[],'ytick',[],'box','on')
subplot(223)
set(gca,'position',[mag mag big big],'xtick',[],'ytick',[],'box','on')
subplot(224)
title('GPS-IF','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'position',[mag1 mag big big],'xtick',[],'ytick',[],'box','on')
end
function draw3()
figure
wid=1000;
hei=600;
bili=wid/hei;
wid1=0.43;
lefx=1-wid1*2;
lefy=lefx*bili;
hei1=(1-lefy)/2;
magx=lefx*8/14; %margin x
intx=lefx*3/14; %interval x
magy=lefy*8/14; %margin y
inty=lefy*3/14; %interval y
magx1=magx+wid1+intx;
magy1=magy+hei1+inty;
set(gcf,'position',[0 0 wid hei])
subplot(221)
title('GPS-IF','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'position',[magx magy1 wid1 hei1],'xtick',[],'ytick',[],'box','on')
subplot(222)
set(gca,'position',[magx1 magy1 wid1 hei1],'xtick',[],'ytick',[],'box','on')
subplot(223)
set(gca,'position',[magx magy wid1 hei1],'xtick',[],'ytick',[],'box','on')
subplot(224)
title('GPS-IF','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'position',[magx1 magy wid1 hei1],'xtick',[],'ytick',[],'box','on')
end
function draw3()
figure
a=2;b=2;
wid=1000;hei=600;
pmag=50; %the size of margin pix
marx(1:b+1,1)=3;marx(1)=8;
mary(1:a+1,1)=3;mary(1)=8;
sumx=sum(marx);sumy=sum(mary);
marx=marx./sumx;mary=mary./sumy;
lefy=pmag/hei/mary(1);
lefx=pmag/wid/marx(1);
wid1=(1-lefx)/b;
hei1=(1-lefy)/a;
magx=lefx*8/14; %margin x
intx=lefx*3/14; %interval x
magy=lefy*8/14; %margin y
inty=lefy*3/14; %interval y
magx1=magx+wid1+intx;
magy1=magy+hei1+inty;
xt=0:0.5:1;xtl1=sprintfc('%g',xt);xtl2={'','',''};
yt1=0:0.5:1;ytl1=sprintfc('%g',yt1);ytl3={'','',''};
yt2=-2:2:2;ytl2=sprintfc('%g',yt2);
set(gcf,'position',[0 0 wid hei])
subplot(221)
set(gca,'position',[magx magy1 wid1 hei1],'xtick',[],'ytick',[],'box','on')
title('TiT1','FontSize',10,'FontName','Arial','FontWeight','normal');
set(gca,'Ylim',[0 1],'YTick',yt1,'YTicklabel',ytl1);
ylabel('Ylab');
subplot(222)
set(gca,'position',[magx1 magy1 wid1 hei1],'xtick',[],'ytick',[],'box','on')
subplot(223)
set(gca,'position',[magx magy wid1 hei1],'xtick',[],'ytick',[],'box','on')
subplot(224)
set(gca,'position',[magx1 magy wid1 hei1],'xtick',[],'ytick',[],'box','on')
title('GPS-IF','FontSize',10,'FontName','Arial','FontWeight','normal');
end
此函数可绘制任意多个子,依据以下标准绘制
% main draw
function draw5(wid,hei,a,b)
figure
pmag=50; %the size of margin pix
marx(1:b+1,1)=3;marx(1)=8;
mary(1:a+1,1)=3;mary(1)=8;
sumx=sum(marx);sumy=sum(mary);
marx=marx./sumx;mary=mary./sumy;
lefy=pmag/hei/mary(1);
lefx=pmag/wid/marx(1);
marx=marx.*lefx;
mary=mary.*lefy;
wid1=(1-lefx)/b;
hei1=(1-lefy)/a;
xt=0:0.5:1;xtl1=sprintfc('%g',xt);xtl2=kcell(size(xtl1));xl=[0 1];
yt1=0:0.5:1;ytl1=sprintfc('%g',yt1);ytl2=kcell(size(ytl1));yl=[0 1];
set(gcf,'position',[0 0 wid hei])
for i=1:a
magy=(a-i)*hei1+sum(mary(1:end-i));
for j=1:b
magx=(j-1)*wid1+sum(marx(1:j));
n=(i-1)*b+j;
subplot(a,b,n);
set(gca,'position',[magx magy wid1 hei1],'box','on');
% plot here!
set(gca,'XLim',xl,'XTick',xt,'XTicklabel',xtl2);
set(gca,'YLim',yl,'YTick',yt1,'YTicklabel',ytl2);
if j==1
ylabel('Ylab');
set(gca,'YTick',yt1,'YTicklabel',ytl1);
end
if i==a
xlabel('Xlab');
set(gca,'XTick',xt,'XTicklabel',xtl1);
end
end
end
end
% get a null cell which is a*b dims
function data=kcell(m)
a=m(1);b=m(2);
data=cell(a,b);
for i=1:a
for j=1:b
data(i,j)=cellstr(num2str(data{i,j}));
end
end
end
注意: