Matalb绘图总结2
Matalb绘图总结2

Matalb绘图总结2
• 发表于 2年前
• 阅读 5
• 收藏 0
• 评论 0

# Matlab绘图总结2

## 概述

``````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 = {'-';'--';':';'-.'};``````

## 如何在一个数据量很大的曲线上做标记

``````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);``````

``index = 1:50:length(x);``

``````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)``````

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），在这个封闭区域里填充颜色

``````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);``````

``````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还是能画出比较漂亮的图的…

×