matlab学习:基础绘图

1.基础绘图

和python类似

1.1 绘图:plot()

  • 如果绘制多条曲线,需要使用hold on-hold off ,不然后面的曲线会覆盖掉前面的。
hold on
plot(cos(0:pi/20:2*pi),'o-k'); 
plot(sin(0:pi/20:2*pi),'X--b');
hold off%两个一起画;不使用hold on的话,默认会覆盖掉前面那张图片

在这里插入图片描述

1.2 样式

  • plot样式
    plot(x,y,‘str’)
    在这里插入图片描述

  • 图例:legend()

    • title()和xlabel()ylabel()zlabel()
x=0:0.5:4*pi;
y=sin(x);
h=cos(x);
w=1./(1+exp(-x));
xlabel('t=0 tp 2\pi');
ylabel('values of sin(t) and e^{-x}');
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,w,'c^-');%样式
title('values of sin(t) and e^{-x}');%标题
legend('sin(x)','cos(x)','sigmoid','Gauss function');%图例

在这里插入图片描述

  • text() and annotation()
    数学公式使用latex
x = linspace(0,3);
y = x.^2.*sin(x); 
plot(x,y); 
line([2,2],[0,2^2*sin(2)]);
str = '$$ \int_{0}^{2} x^2\sin(x) dx $$';%表示积分函数
text(0.25,2.5,str,'Interpreter','latex'); %积分函数的位置
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); %箭头位置

在这里插入图片描述

  • 小练习:
    在这里插入图片描述
t=1:0.01:2;
f=t.*t;
g=sin(2*pi*t);
plot(t,f,'k',t,g,'ro');
xlabel('Time (ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)','location','northwest');%northwest指定图例在左上角

在这里插入图片描述

2. 图像调整(Figure Adjustment

  • 一张图片由很多对象组成;
  • 一个对象又有很多属性。
    在这里插入图片描述
    在这里插入图片描述

2.1 修改图片对象的句柄(handle)

在这里插入图片描述

  • 函数
    |函数|解释 |
    |–|--|
    |gca|返回当前图像的坐标轴axes |
    |gcf|返回图像的figure对象|
    |allchild|找到具体对象的所有孩子|
    |ancestor|找到图片对象的祖先|
    |delete|删除具体对象|
    |findall|找出所有的图片对象|

2.2获得或者修改图片属性

  • 获得属性/对象:get()
  • 修改属性:set()
  • 两者都需要通过获取handle来进行实现。
%2020.09.19
x = linspace(0, 2*pi, 1000); 
y = sin(x); 
h = plot(x,y);
get(h) %获取h的信息
get(gca)%获取当前图像的axis对象的所有信息

在这里插入图片描述

%set(gca, 'XLim', [0, 2*pi]);%修改Xlim的取值
%set(gca, 'YLim', [-1.2, 1.2]);
xlim([0, 2*pi]);%修改Xlim的取值
ylim([-1.2, 1.2]);

在这里插入图片描述

2.3 设置字体和坐标轴的间距(tick)

set(gca, 'FontSize', 25);%设置字体大小
set(gca, 'XTick', 0:pi/2:2*pi);%Tick, YTick, ZTick - 刻度值; 由递增值组成的向量
set(gca, 'XTickLabel', 0:90:360);

2.4 曲线的格式

  • 线宽和曲线样式
%set(h, 'LineStyle', '-.',... 
%    'LineWidth', 7.0, 'Color', 'g');%设置line的格式
plot(x,y, '-.g',... 'LineWidth', 7.0)
%delete(h);%删掉曲线h

在这里插入图片描述

  • 图标(maker)的face和edge
x=rand(20,1); %rand(3,4) 返回一个 3×4 的矩阵。
set(gca, 'FontSize', 18);
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k',...
    'MarkerFaceColor', 'g', 'MarkerSize', 10);
xlim([1, 20]);

在这里插入图片描述

2.5 小练习

在这里插入图片描述

t=1:0.01:2;
f=t.*t;
g=sin(2*pi*t);
plot(t,f,'k',t,g,'ro','LineWidth', 2,'MarkerEdgeColor', 'r',...
    'MarkerFaceColor', 'g');
xlabel('Time (ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)','location','northwest');
set(gca,'FontSize',20,'FontWeight','bold','TitleFontWeight','bold','XTick', -1:1:4);

在这里插入图片描述

3. 组合图片

  • 当存在复合图像时,gcf指的是现在的图片,即figure2.
x = -10:0.1:10; 
y1 = x.^2 - 8;
y2 = exp(x); 
figure, plot(x,y1); 
figure, plot(x,y2);

在这里插入图片描述

3.1 设置图像的位置和大小

在这里插入图片描述

3.2 一个figure中显示几个plots

在这里插入图片描述
在这里插入图片描述

t = 0:0.1:2*pi; 
x = 3*cos(t);
y = sin(t); 
subplot(2, 2, 1); 
plot(x, y);
axis normal %x和y轴自动调整
subplot(2, 2, 2); 
plot(x, y); 
axis square %x和y轴的总长度一样
subplot(2, 2, 3); 
plot(x, y); 
axis equal %x和y轴的间隔一样
subplot(2, 2, 4); 
plot(x, y); 
axis equal tight %Set the axis limits to the range of the data
axis off
axis on

在这里插入图片描述

4. 把figures保存成文件

在这里插入图片描述

  • 如果想要保存成高解析度的文件,建议使用‘print’。
%Saving Figures into Files
saveas(gcf,'practice','pdf');

猜你喜欢

转载自blog.csdn.net/chairon/article/details/108689407