首页 > 代码库 > Matalb绘图总结2
Matalb绘图总结2
Matlab绘图总结2
Contents
如何在一个数据量很大的曲线上做标记
平滑一个数据量很少的曲线
绘制面积图
概述
上篇文章介绍了简单的绘图函数,地址见: http://blog.csdn.net/czyt1988/article/details/38637725
这篇将介绍一些论文绘图中经常碰到的问题,如在一个数量很大的曲线上做标记,平滑一个数据量很少的曲线,绘制面积图,以及如何改进绘图的美感
clc; clear all; close all; x = 0:0.01:5; y1 = 2*sin(x)+0.1; y2 = sin(x).*cos(x)+0.2; y3 = sin(x)./(x+0.1); y = [y1;y2;y3]; marker_style = {‘>‘;‘<‘;‘s‘;‘o‘;‘d‘;‘v‘;‘p‘;‘^‘;‘*‘;‘+‘;‘x‘;‘h‘}; line_style = {‘-‘;‘--‘;‘:‘;‘-.‘};
如何在一个数据量很大的曲线上做标记
如果有个曲线有5000个点,为了区分多条曲线,又想给他们做标记用以区分,点数少的时候好说 ,设置Marker就可以实现,但点数多的时候却不是那么好看了。
可以看看面这个例子:
没有标记图片如下所示:
text_legend = {}; text_legend{1,1} = ‘y1 = 2*sin(x)+0.1‘; text_legend{2,1} = ‘y2 = sin(x).*cos(x)+0.2‘; text_legend{3,1} = ‘y3 = sin(x)./(x+0.1)‘; figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = []; handle_plot(1) = plot(x,y1,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(2) = plot(x,y2,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(3) = plot(x,y3,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); grid on; legend(handle_plot,text_legend,0);
带标记图片如下所示:
figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = []; handle_plot(1) = plot(x,y1,‘-<‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(2) = plot(x,y2,‘-s‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(3) = plot(x,y3,‘-o‘,‘color‘,[rand rand rand],‘LineWidth‘,2); grid on; legend(handle_plot,text_legend,0);
标记完全看不清 为了可以稀疏的标记出来,可以绘制为需要绘制marker的曲线单独绘制一个仅有marker的标记线,这个标记线取点也在数据中获取,不过 取点间隔比原始数据更大,如:
index = 1:50:length(x);
通过上面这个代码获取需要抽取的索引,相当于每隔50个点抽取一个数据。
figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = [];handle_plot_Marker = []; line_color = [rand rand rand]; handle_plot(1) = plot(x,y1,‘-‘,‘color‘,line_color,‘LineWidth‘,2); handle_plot_Marker(1) = plot(x(index),y1(index),‘<‘,‘color‘,line_color); line_color = [rand rand rand]; handle_plot(2) = plot(x,y2,‘-‘,‘color‘,line_color,‘LineWidth‘,2); handle_plot_Marker(2) = plot(x(index),y2(index),‘s‘,‘color‘,line_color); line_color = [rand rand rand]; handle_plot(3) = plot(x,y3,‘-‘,‘color‘,line_color,‘LineWidth‘,2); handle_plot_Marker(3) = plot(x(index),y3(index),‘o‘,‘color‘,line_color); grid on; legend(handle_plot_Marker,text_legend,0);
使用这种方法,无论在多的点,也能清洗的标定记号。
平滑一个数据量很少的曲线
此问题和上个问题正好相反,如果数据量特别少,又想绘制一个光滑的曲线,就需要进行平滑插值。 还是使用上面经过抽取的数据:
x0 = x(index); y10 = y1(index); y20 = y2(index); y30 = y3(index); text_legend = {}; text_legend{1,1} = ‘y10‘; text_legend{2,1} = ‘y20‘; text_legend{3,1} = ‘y30‘; figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = []; handle_plot(1) = plot(x0,y10,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(2) = plot(x0,y20,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(3) = plot(x0,y30,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); grid on; legend(handle_plot,text_legend,0)
从上面图可以看出,由于点数比较少,有明显的不平滑出现,为了使其平滑,可以使用3次样条插值。 matlab插值函数有很多,其中3次样条插值(spline)在曲线平滑上的表现是比较理想的(别用线性插值,无任何效果)。
matlab中插值函数为interp1,样式为:yi=interp1(x,y,xi,‘spline‘),x,y为原来的值,xi为需要插值的x值,yi为插值后的结果
y1i=interp1(x0,y10,x,‘spline‘); y2i=interp1(x0,y20,x,‘spline‘); y3i=interp1(x0,y30,x,‘spline‘); text_legend = {}; text_legend{1,1} = ‘y1i‘; text_legend{2,1} = ‘y2i‘; text_legend{3,1} = ‘y3i‘; figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = []; handle_plot(1) = plot(x,y1i,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(2) = plot(x,y2i,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(3) = plot(x,y3i,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); grid on; legend(handle_plot,text_legend,0);
可以看到,插值后的结果和之前的对比非常接近
绘制面积图
有时候,绘制类似于功率的图时,需要给底部某区域填充颜色,这时就需要使用填充图。
matlab里绘制面积图使用fill函数,fill(x,y,color)函数前两个参数一一对应组成一个点序列,如: x=[x1,x2,x3……xn],y=[y1,y2,y3……yn],fill函数将会从(x1,y1)点开始(x2,y2),(x3,y3)……绘制一个区域, 到(xn,yn)然后再连接(x1,y1),在这个封闭区域里填充颜色
下面这个例子给曲线1填充颜色:
text_legend = {}; text_legend{1,1} = ‘y1 = 2*sin(x)+2.1‘; text_legend{2,1} = ‘y2 = sin(x).*cos(x)+2.2‘; text_legend{3,1} = ‘y3 = sin(x)./(x+0.1)+2‘; figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = []; handle_plot(1) = plot(x,y1,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); fill([x(1) x x(end)],[0 y1 0],[0.77,0.90,0.94]); handle_plot(2) = plot(x,y2,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); handle_plot(3) = plot(x,y3,‘-‘,‘color‘,[rand rand rand],‘LineWidth‘,2); grid on; legend(handle_plot,text_legend,0);
多个图需要填充时,最好把颜色设置成有透明度,这样看起来会漂亮很多, edgealpha属性和FaceAlpha属性可以用于设置填充的边界线和填充颜色的透明度
text_legend = {}; text_legend{1,1} = ‘y1 = 2*sin(x)+0.1‘; text_legend{2,1} = ‘y2 = sin(x).*cos(x)+0.2‘; text_legend{3,1} = ‘y3 = sin(x)./(x+0.1)‘; figure set(gcf,‘color‘,‘w‘); hold on; handle_plot = []; line_color = [rand rand rand]; handle_plot(1) = plot(x,y1,‘-‘,‘color‘,line_color); fill([x(1) x x(end)],[0 y1 0],line_color,‘edgealpha‘,0.4,‘FaceAlpha‘,0.4); line_color = [rand rand rand]; handle_plot(2) = plot(x,y2,‘-‘,‘color‘,line_color); fill([x(1) x x(end)],[0 y2 0],line_color,‘edgealpha‘,0.4,‘FaceAlpha‘,0.4); line_color = [rand rand rand]; handle_plot(3) = plot(x,y3,‘-‘,‘color‘,line_color); fill([x(1) x x(end)],[0 y3 0],line_color,‘edgealpha‘,0.4,‘FaceAlpha‘,0.4); grid on; legend(handle_plot,text_legend,0);
matlab还是能画出比较漂亮的图的…
ps:此blog依然由matlab的publish生成,源代码地址:
Matalb绘图总结2