matlab绘图总结

前言:

  • 我就记录一下自己用到的画图指示吧,就不讲全面性和完整性了
  • 首先要大力感激我的好友坤哥,多亏他给我了我巨大的启迪。关于这个问题,我有时间还要单独的跟他聊一聊是怎么学习的,效率这么高,基础这么扎实。
  • 最后要说一下matlab的图形化作图功能实在是太强大了,后来我甚至发现可以,直接插个图上面去,然后生成作图的代码。

目录:

  • 最初的版本
  • 中间的版本
  • 最终版本
  • 其他

最初的版本

clear
figure
rectangle('position',[20,20,10,10],'edgecolor','k','facecolor','k');  %
%hold on
rectangle('position',[40,25,3,3],'edgecolor','k','facecolor','r');  %红色矩形
%hold on
rectangle('position',[38.5,23.5,6,6],'curvature',[1,1],'edgecolor','k');  %黑色的边界线
%hold on
rectangle('position',[10,40,4,4],'curvature',[1,1],'edgecolor','k','facecolor','k');  %黑色圿
%hold on
rectangle('position',[42,10,8,8],'curvature',[1,1],'edgecolor','k','facecolor','k');  %黑色圿
%hold on
rectangle('position',[9.5,9.5,1,1],'curvature',[1,1],'edgecolor','g','facecolor','g');%绿色的小炿
%hold on
rectangle('position',[9.5,19.5,1,1],'curvature',[1,1],'edgecolor','g','facecolor','g');%绿色的小炿
%hold on
rectangle('position',[9.5,29.5,1,1],'curvature',[1,1],'edgecolor','g','facecolor','g');%绿色的小炿
%hold on
rectangle('position',[29.5,9.5,1,1],'curvature',[1,1],'edgecolor','g','facecolor','g');%绿色的小炿
%hold on
rectangle('position',[29.5,34.5,1,1],'curvature',[1,1],'edgecolor','g','facecolor','g');%绿色的小炿
%hold on
rectangle('position',[39.5,39.5,1,1],'curvature',[1,1],'edgecolor','g','facecolor','g');%绿色的小炿
grid on;

中间的版本
数据坐标的控制是写死的

function createfigure
%CREATEFIGURE
% 创建 figure
figure1 = figure;
% 创建 axes
axes1 = axes('Parent',figure1);


%准备数据
input = importfile('input.txt');
place=input{1:1,{'VarName1','VarName2'}}
Num_of_obstacle=input{3:3,{'VarName1'}}

 %画障碍物
obstacle=zeros(1,5);
for i=1:Num_of_obstacle
       trash_temp=input{3+i:3+i,{'VarName1','VarName2','VarName3','VarName4','VarName5'}};
       obstacle(:,:,i)= trash_temp;
    if  obstacle(1,5,i)==0
        obstacle(1,1,i)=obstacle(1,1,i)-obstacle(1,3,i)/2.0;
        obstacle(1,2,i)=obstacle(1,2,i)-obstacle(1,4,i)/2.0;
        rectangle('Parent',axes1,'Position',obstacle(1,1:4,i),'FaceColor','k');
    else  obstacle(1,5,i)==1
         obstacle(1,1,i)=obstacle(1,1,i)-obstacle(1,3,i)/2.0;
         obstacle(1,2,i)=obstacle(1,2,i)-obstacle(1,4,i)/2.0;
         obstacle(1,3,i)=obstacle(1,3,i);
         obstacle(1,4,i)=obstacle(1,4,i);
         rectangle('Parent',axes1,'Position',obstacle(1,1:4,i),'FaceColor',[0 0 0],...
         'Curvature',[1 1]);
    end
end

%画垃圾
trash=zeros(1,4);
Number_of_trash=input{3+Num_of_obstacle+1:3+Num_of_obstacle+1,{'VarName1'}};
for i=1:Number_of_trash
    trash_temp=input{3+Num_of_obstacle+1+i:3+Num_of_obstacle+1+i,{'VarName1','VarName2','VarName3','VarName4'}}
    trash(:,:,i)= trash_temp;
    rectangle('Parent',axes1,'Position',[trash(1,1,i) trash(1,2,i) 1 1 ],'EdgeColor',[0 1 0],...
    'FaceColor',[0 1 0],...
    'Curvature',[1 1]);
end


% 画robot 和 sensor    
robot=input{2:2,{'VarName1','VarName2','VarName3','VarName4'}}
% 画sensor  
radius_of_sensor=input{3+Num_of_obstacle+Number_of_trash+3:3+Num_of_obstacle+Number_of_trash+3,{'VarName1'}};
rectangle('Parent',axes1,'Position',[robot(1)-radius_of_sensor(1) robot(2)-radius_of_sensor(1) radius_of_sensor*2 radius_of_sensor*2],'Curvature',[1 1]);
% 画robot
robot(1)=robot(1)-robot(3)/2.0;
robot(2)=robot(2)-robot(4)/2.0;
rectangle('Parent',axes1,'Position',robot,'FaceColor','r');


time_step=input{3+Num_of_obstacle+Number_of_trash+4:3+Num_of_obstacle+Number_of_trash+4,{'VarName1'}};
weight=input{3+Num_of_obstacle+Number_of_trash+5:3+Num_of_obstacle+Number_of_trash+5,{'VarName1'}};
hold on

%画垃圾站
garbage=input{3+Num_of_obstacle+Number_of_trash+2:3+Num_of_obstacle+Number_of_trash+2,{'VarName1','VarName2'}};
scatter(garbage(1),garbage(2),'k','x');

% 取消以下行的注释以保留坐标区的 X 范围
% xlim(axes1,[0 50]);
% 取消以下行的注释以保留坐标区的 Y 范围
% ylim(axes1,[0 50]);
grid(axes1,'on');
% 设置其余坐标区属性 
axis([0 50 0 50]);
set(axes1,'XTick',...
    [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50],...
    'XTickLabel',...
    {'','','','','','','','','','','10','','','','','','','','','','20','','','','','','','','','','30','','','','','','','','','','40','','','','','','','','','','50'},...
    'YTick',...
    [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50],...
    'YTickLabel',...
    {'','','','','','','','','','','10','','','','','','','','','','20','','','','','','','','','','30','','','','','','','','','','40','','','','','','','','','','50'});

最终版本
加入了坐标轴控制
和打点

% 创建 figure
figure1 = figure;
% 创建 axes
axes1 = axes('Parent',figure1);

%准备数据
Input = importfile('Input.txt');
place=Input{1:1,{'VarName1','VarName2'}};
Num_of_obstacle=Input{3:3,{'VarName1'}};

%画障碍物
obstacle=zeros(1,5);
for i=1:Num_of_obstacle
    trash_temp=Input{3+i:3+i,{'VarName1','VarName2','VarName3','VarName4','VarName5'}};
    obstacle(:,:,i)= trash_temp;
    if  obstacle(1,5,i)==0
        obstacle(1,1,i)=obstacle(1,1,i)-obstacle(1,3,i)/2.0;
        obstacle(1,2,i)=obstacle(1,2,i)-obstacle(1,4,i)/2.0;
        rectangle('Parent',axes1,'Position',obstacle(1,1:4,i),'FaceColor','k');
    else obstacle(1,5,i)==1
        obstacle(1,1,i)=obstacle(1,1,i)-obstacle(1,3,i)/2.0;
        obstacle(1,2,i)=obstacle(1,2,i)-obstacle(1,4,i)/2.0;
        obstacle(1,3,i)=obstacle(1,3,i);
        obstacle(1,4,i)=obstacle(1,4,i);
        rectangle('Parent',axes1,'Position',obstacle(1,1:4,i),'FaceColor',[0 0 0],...
            'Curvature',[1 1]);
        hold on
    end
end

%画垃圾
trash=zeros(1,4);
Number_of_trash=Input{3+Num_of_obstacle+1:3+Num_of_obstacle+1,{'VarName1'}};
for i=1:Number_of_trash
    trash_temp=Input{3+Num_of_obstacle+1+i:3+Num_of_obstacle+1+i,{'VarName1','VarName2','VarName3','VarName4'}}
    trash(:,:,i)= trash_temp;
    rectangle('Parent',axes1,'Position',[trash(1,1,i)-0.5 trash(1,2,i)-0.5 1 1 ],'EdgeColor',[0 1 0],...
        'FaceColor',[0 1 0],...
        'Curvature',[1 1]);
end

% 画Robot 和 sensor
Robot=Input{2:2,{'VarName1','VarName2','VarName3','VarName4'}};
% 画sensor
radius_of_sensor=Input{3+Num_of_obstacle+Number_of_trash+3:3+Num_of_obstacle+Number_of_trash+3,{'VarName1'}};
sensor_visual = rectangle('Parent',axes1,'Position',[Robot(1)-radius_of_sensor(1) Robot(2)-radius_of_sensor(1) radius_of_sensor*2 radius_of_sensor*2],'Curvature',[1 1]);
% 画Robot
Robot_visual = rectangle('Parent',axes1,'Position',[Robot(1)-Robot(3)/2.0 Robot(2)-Robot(4)/2.0 Robot(3) Robot(4)],'FaceColor','r');
% 时间限制
time_step=Input{3+Num_of_obstacle+Number_of_trash+4:3+Num_of_obstacle+Number_of_trash+4,{'VarName1'}};
% 机器人负重上限
weight=Input{3+Num_of_obstacle+Number_of_trash+5:3+Num_of_obstacle+Number_of_trash+5,{'VarName1'}};

%画垃圾站
garbage=Input{3+Num_of_obstacle+Number_of_trash+2:3+Num_of_obstacle+Number_of_trash+2,{'VarName1','VarName2'}};
scatter(garbage(1),garbage(2),'k','x');

grid(axes1,'on');
x_margin=place(1);
y_margin=place(2);
axis([0 x_margin 0 x_margin]);
x_XTick=1:x_margin;
x_XTickLabel={};
for i=1:x_margin
    if mod(i,10)== 0
        t=floor(i/10)*10;
        x_XTickLabel{1,i}=num2str(t);
    else
        x_XTickLabel{1,i}='';
    end
end
y_YTick=1:y_margin;
y_YTickLabel={};
for i=1:y_margin
    if mod(i,10)== 0
        t=floor(i/10)*10;
        y_YTickLabel{1,i}=num2str(t);
    else
        y_YTickLabel{1,i}='';
    end
end
set(axes1,'XTick',x_XTick,'XTickLabel',x_XTickLabel,'YTick',y_YTick,'YTickLabel',y_YTickLabel)
hold on

其他:

matlab画图总结:
1.plot
可以同时画多个图 plot(x,y1,x,y2);
可以加网格  grid on
可以加坐标轴信息 
图的名称 
可以在图上加上记号或者说是标签 text(1.5,0.3,'cos(x)') 
可以用鼠标加上标签 gtext('sin(x)') 
 x=0:pi/20:2*pi;
 y1=sin(x);
 y2=cos(x);
 plot(x,y1,x,y2);
 grid on
 xlabel('变量 X')
 ylabel('变量 Y1 & Y2')
 title('正弦余弦波形')     %添加图像标题
 text(1.5,0.3,'cos(x)')    %将cosx这个注解加到坐标中的某个位置
 gtext('sin(x)')      % 用鼠标的光标定位,将sinx这个注解放在你鼠标点击的地方

2.设置坐标轴范围
 axis([0 50 0 50]);

3.设置坐标轴的刻度间隔
clear
figure;
axes;
axis([0 100 0 1000])
set(gca,'XTick',0:5:100);
set(gca,'YTick',0:20:1000);


4.figure 和plot的区别
figure 一般是建一个画布,然后画多张图
plot则是画一张图,来一个画布
clear;
y1 = [1,5,7,9,2,5,1,3,9,5];
y2 = [8,5,6,9,4,6,9,2,3,6];
y3 = [2,3,6,4,7,9,2,8,3,9];
y4 = [3,6,8,9,2,7,1,5,9,2];
x2 = [1,3,5,7,9,11,13,15,17,19];
x3 = [1,2,3,4,5,6,7,8,9,10];
x4 = [1,2,3,4,5,6,8,9,10,13];
figure(1);
plot(y1);
figure(2);
plot(x2,y2);
figure(3);
plot(x3,y3,'r.-');
% figure(4);
% plot(x4,y4,'r.-',xxx,yy,'g*-');
% xlabel('x坐标');
% ylabel('y坐标');
% axis([0 20 0 10]);
% 
figure;
subplot(2,2,1);stem(y1,':r*');title('stem');
subplot(2,2,2);stem(x2,y2,'-gx');title('stem');
subplot(2,2,3);semilogx(x3,y3);title('semilogx');text(1,5,'说明');
% subplot(2,2,4);semilogy(x4,y4);title('semilogy');


5.将多张图画在同一个画布上,同一区域
经测试发现如果不加12 那么就会默认生成不同的画布自动编号为123,如果都写1那么就会共存显示在一个画布上面,如果没有figuer默认就是覆盖式的写在同一张画布上面
clear;
y1 = [1,5,7,9,2,5,1,3,9,5];
y2 = [8,5,6,9,4,6,9,2,3,6];
y3 = [2,3,6,4,7,9,2,8,3,9];
y4 = [3,6,8,9,2,7,1,5,9,2];
x2 = [1,3,5,7,9,11,13,15,17,19];
x3 = [1,2,3,4,5,6,7,8,9,10];
x4 = [1,2,3,4,5,6,8,9,10,13];
figure(1);
plot(y1);
hold on
figure(1);
plot(x2,y2);
hold on
figure(1);
plot(x3,y3,'r.-');
hold on


6.在一个画布上,创建独立的画布
figure;
subplot(2,2,1);stem(y1,':r*');title('stem');
subplot(2,2,2);stem(x2,y2,'-gx');title('stem');
subplot(2,2,3);semilogx(x3,y3);title('semilogx');text(1,5,'说明');


7.去掉整个坐标轴
axis off 


8.只去掉最底下的那条线,但是不去掉刻度和网格
box off;

9.惊喜惊喜!
最厉害的画图方法就是在图上设置好之后,然后点击另存为代码!!!!!!
xiang


10.我觉得这个读写的方法或许更好一点
http://blog.sciencenet.cn/blog-111625-859181.html

给坐标轴一个特写

x_margin=50;
y_margin=50;
axis([0 x_margin 0 x_margin]);
x_XTick=1:x_margin;
x_XTickLabel={};
for i=1:x_margin
    if mod(i,10)== 0
        x_XTickLabel{1,10}='10';
    else 
        x_XTickLabel{1,i}='';
    end
end

y_YTick=1:y_margin;
y_YTickLabel={};
for i=1:y_margin
    if mod(i,10)== 0
        y_YTickLabel{1,10}='10';
    else 
        y_YTickLabel{1,i}='';
    end
end


set(axes1,'XTick',x_XTick,'XTickLabel',x_XTickLabel'YTick',y_YTick,'YTickLabel',y_YTickLabel)




其他相关的乱七八糟的:

matlab画图总结:
1.plot
可以同时画多个图 plot(x,y1,x,y2);
可以加网格  grid on
可以加坐标轴信息 
图的名称 
可以在图上加上记号或者说是标签 text(1.5,0.3,'cos(x)') 
可以用鼠标加上标签 gtext('sin(x)') 
 x=0:pi/20:2*pi;
 y1=sin(x);
 y2=cos(x);
 plot(x,y1,x,y2);
 grid on
 xlabel('变量 X')
 ylabel('变量 Y1 & Y2')
 title('正弦余弦波形')     %添加图像标题
 text(1.5,0.3,'cos(x)')    %将cosx这个注解加到坐标中的某个位置
 gtext('sin(x)')      % 用鼠标的光标定位,将sinx这个注解放在你鼠标点击的地方

2.设置坐标轴范围
 axis([0 50 0 50]);

3.设置坐标轴的刻度间隔
clear
figure;
axes;
axis([0 100 0 1000])
set(gca,'XTick',0:5:100);
set(gca,'YTick',0:20:1000);


4.figure 和plot的区别
figure 一般是建一个画布,然后画多张图
plot则是画一张图,来一个画布
clear;
y1 = [1,5,7,9,2,5,1,3,9,5];
y2 = [8,5,6,9,4,6,9,2,3,6];
y3 = [2,3,6,4,7,9,2,8,3,9];
y4 = [3,6,8,9,2,7,1,5,9,2];
x2 = [1,3,5,7,9,11,13,15,17,19];
x3 = [1,2,3,4,5,6,7,8,9,10];
x4 = [1,2,3,4,5,6,8,9,10,13];
figure(1);
plot(y1);
figure(2);
plot(x2,y2);
figure(3);
plot(x3,y3,'r.-');
% figure(4);
% plot(x4,y4,'r.-',xxx,yy,'g*-');
% xlabel('x坐标');
% ylabel('y坐标');
% axis([0 20 0 10]);
% 
figure;
subplot(2,2,1);stem(y1,':r*');title('stem');
subplot(2,2,2);stem(x2,y2,'-gx');title('stem');
subplot(2,2,3);semilogx(x3,y3);title('semilogx');text(1,5,'说明');
% subplot(2,2,4);semilogy(x4,y4);title('semilogy');


5.将多张图画在同一个画布上,同一区域
经测试发现如果不加12 那么就会默认生成不同的画布自动编号为123,如果都写1那么就会只显示最后一行,
clear;
y1 = [1,5,7,9,2,5,1,3,9,5];
y2 = [8,5,6,9,4,6,9,2,3,6];
y3 = [2,3,6,4,7,9,2,8,3,9];
y4 = [3,6,8,9,2,7,1,5,9,2];
x2 = [1,3,5,7,9,11,13,15,17,19];
x3 = [1,2,3,4,5,6,7,8,9,10];
x4 = [1,2,3,4,5,6,8,9,10,13];
figure(1);
plot(y1);
hold on
figure(1);
plot(x2,y2);
hold on
figure(1);
plot(x3,y3,'r.-');
hold on


6.在一个画布上,创建独立的画布
figure;
subplot(2,2,1);stem(y1,':r*');title('stem');
subplot(2,2,2);stem(x2,y2,'-gx');title('stem');
subplot(2,2,3);semilogx(x3,y3);title('semilogx');text(1,5,'说明');


7.去掉整个坐标轴
axis off 


8.只去掉最底下的那条线,但是不去掉刻度和网格
box off;

9.惊喜惊喜!
最厉害的画图方法就是在图上设置好之后,然后点击另存为代码!!!!!!
xiang


10.我觉得这个读写的方法或许更好一点
http://blog.sciencenet.cn/blog-111625-859181.html
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值