MATLAB教程_05Basic_Plotting_台大郭彦甫课程笔记

Matlab lesson

一、Plot from “Data”

• Strategies:

  1. Generate the numeric values of a function over a
    specific range
  2. Display the data “points” in a graphical way

1.Format
在这里插入图片描述
•plot(x,y) plots each vector pairs (x,y)
• plot(y) plots each vector pairs (x,y), where x=[1…n], n=length(y)
在这里插入图片描述

2.Exmple

plot(cos(0:pi/20:2*pi));

在这里插入图片描述

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

同时绘制两个图形在这里插入图片描述这个图片并未显示cos,这是因为matlab自动关闭前一幅图。如果希望同时绘制两个图象在一副图上需要用到一下指令
hold on/off

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

在这里插入图片描述

3.plot style:plot(x,y,‘str’)

在这里插入图片描述

 hold on
plot(cos(0:pi/20:2*pi),'+-g');
plot(sin(0:pi/20:2*pi),'s:y');
hold off
``

在这里插入图片描述

4.标注图标
在这里插入图片描述

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()标注图标

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

在这里插入图片描述

5.添加标题与标签
在这里插入图片描述

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}');

在xlable 与ylable中 \pi 不是除以pi,而是显示字符pi
其中 e^{-x} 同样是为了显示字符

在这里插入图片描述

6.Text with mathematical expression using 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公式
text(0.25,2.5,str,'Interpreter','latex') ;  //固定用法
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); //画箭头

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

7.练习
在这里插入图片描述

t=linspace(1,2);
f=t.^2;
g=sin(2*pi*t);
plot(t,f,'-k',t,g,'or');
xlabel('Time (ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t.^(2)','sin(2\pit)');

8.Figure Adjustment
在这里插入图片描述
在这里插入图片描述

  1. Modifying Properties of An Object
    Strategy:
    1. Identify the “handle” of an object
    2. Fetch or modify the object’s properties

for exmple:change the limits of the x-axis:
在这里插入图片描述
Identifying the Handle of An Object
在这里插入图片描述
Fetching(取回) or Modifying Properties

• To fetch properties:get()
• To modify properties:set()

x = linspace(0, 2*pi, 1000);
y = sin(x); h = plot(x,y); 
get(gac)

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

Setting Axes Limits

set(gca, 
'XLim', [0, 2*pi]);
set(gca, 
'YLim', [-1.2, 1.2]);

在这里插入图片描述
加入

set(gca, 'FontSize', 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'});//在symbol中pi用p表示

在这里插入图片描述
这是因为现有Matlab不支持symbol字体。
将代码改为

x = linspace(0, 2*pi, 1000);
y = sin(x); h = plot(x,y);
set(gca, 'XLim', [0, 2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});

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

set(h, 'LineStyle', '-.','LineWidth', 7.0, 'Color', 'g');

在这里插入图片描述

delete(h);//删去h

• Face and edge colors of the markder

在这里插入图片描述


x=rand(20,1);
 set(gca, 'FontSize', 18);
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k','MarkerFaceColor', 'g', 'MarkerSize', 10);
xlim([1, 20]);

在这里插入图片描述

练习
在这里插入图片描述

t=linspace(1,2);
f=t.^2;
g=sin(2*pi*t);
hold on;
f1=plot(t,f,'-k');
f2=plot(t,g,'or');
set(f1,'linewidth',4);
set(f2,'LineWidth', 2,'MarkerFaceColor', 'b' ,'MarkerSize', 10);
xlabel('Time (ms)');
ylabel('f(t) and g(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pit)','location','northwest');
hold off;

在这里插入图片描述

9.Multiple Figures

x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);

在这里插入图片描述

10.Figure Position and Size

figure('Position', [left, bottom, width, height]);

在这里插入图片描述

x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
subplot(1,2,1);plot(x,y1);
subplot(1,2,2);plot(x,y2);

在这里插入图片描述

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

在这里插入图片描述
通过不同的坐标设定,使得同一图形产生不同形状

在这里插入图片描述
grid(格线),box(盒子),axis(坐标),都是不同的部分,可以分别打开和关闭,如

grid on

在这里插入图片描述

grid off

在这里插入图片描述
11.Saving Figures into Files

saveas(gcf,'<filename>','<formattype>');

在这里插入图片描述
在这里插入图片描述
可以将figure存储为jpg文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值