台大郭彦甫笔记05 初阶绘图

一、基本绘图

画图:①生成数字点 ②连线

plot( )

只生成最后一个

plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold on/off

两条线都生成

hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off
Plot Style

• plot(x,y,'str')
str中的符号

img

legend( ) 标图标

legend('L1',)

x=0:0.5:4*pi;
y=sin(x); h=cos(x); w=1./(1+exp(-x));
g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
legend('sin(x)','cos(x)','Sigmoid','Gauss function');
title( ) and ?label( )

标记图片名称和x,y,z轴

x = 0:0.1:2*pi; y1 = sin(x); y2 = exp(-x);
plot(x, y1, '--*', x, y2, ':o');
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}')
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');

img

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 $$';   % '$$...$$' 意味着 latex, '\int'意味着积分符号,'{0}^{2}'意味着在(02)区间内积分
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); %X,Y的区间

img

二、图像对象属性

图形对象

img

修改图形的对象

步骤:
① 识别一个对象的句柄
② 获取或修改handle

① Identifying the Handle of An Object
h = plot(x,y);

img

②Fetching or Modifying Properties

get( )    %获取属性

set( )    %修改属性
x = linspace(0, 2*pi, 1000);
y = sin(x); h = plot(x,y);
get(h)

得到一个sin(x)图像

img

set(gca, 'XLim', [0, 2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
修改x,y limit

img

set(gca, 'FontSize', 25);     %25是字号

set(gca, 'XTick', 0:pi/2:2*pi);     %坐标轴刻度间距
set(gca, 'XTickLabel', 0:90:360);      %坐标轴刻度标记
 
set(gca, 'FontName', 'symbol');
set(gca, 'XTickLabel', {'0', 'p/2', 'p', '3p/2', '2p'});    %'p' means 'pi'

img

修改线形
h = plot(x,y); 
set(h, 'LineStyle','-.', ...
'LineWidth', 7.0, ...
'Color', 'g');

或者

plot(x,y, '-.g',...
'LineWidth', 7.0);
删除图像 delete(h)
x=rand(20,1); set(gca, 'FontSize', 18);
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k',...
'MarkerFaceColor', 'g', 'MarkerSize', 10);
xlim([1, 20]);

% face color 
% edge color 边界颜色

img

多个图像
x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);
一个figuer有几个plots
subplot(m, n,1);
t = 0:0.1:2*pi; x = 3*cos(t); y = sin(t);
subplot(2, 2, 1); plot(x, y); axis normal
subplot(2, 2, 2); plot(x, y); axis square
subplot(2, 2, 3); plot(x, y); axis equal
subplot(2, 2, 4); plot(x, y); axis equal tight
将图形保存到文件

1 使用saveas(fig,filename)命令可以将图形对象保存到文件中,其中fig为图形句柄,filname为文件名

saveas(gcf, 'myfigure.png')
saveas(gcf,'<filename>','<formattype>');
%(版权声明:本文为CSDN博主「ncepu_Chen」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ncepu_Chen/article/details/103097452)

使用saveas()函数将图像保存成位图时,会发生失真.要精确控制生成图片的质量,可以使用print()函数
2 需要高度解析

print