matlab basic,Matlab 5 (Basic_Plotting)

plot(x,y) %描绘(x,y)点,点的个数点的密度是由x确定的,因为y可以是一个函数,是将x代进去算出的值

plot(y) %描绘(x,y)点,x=[1,2,3...n],n=length(y)

例:plot( cos(-2*pi : pi/20 : 2*pi) )

0975e794f09b

cos.jpg

0975e794f09b

Snip20180213_1.png

使用 hold on 指令,会将所有图像绘制到一个figure中。

例:

hold on

plot( cos(0 : pi/20 : 2*pi), 'o--g' )

plot( sin(0 : pi/20 : 2*pi), 'x:r' )

hold off

0975e794f09b

holdon.jpg

一条指令画多个图像

图例

例:

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'); %图例

0975e794f09b

legend.jpg

title()

xlabel()

ylabel()

zlabel()

例:

x = 0 : 0.1 : 2*pi;

y1 = sin(x);

y2 = exp(-x);

plot(x,y1,'--*', x,y2,':o');

legend('sin(t)', 'e^{-x}');

title("Function Plots of sin(t) and e^{-x}");

xlabel('t = 0 to 2\pi');

ylabel('values of sin(t) and e^{-x}');

0975e794f09b

title and label.jpg

\int_{0}^{2}    定积分符号 上下界

text()

annotation()

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

0975e794f09b

text.jpg

练习:

t = linspace(1,2);

y = t .^ 2;

g = sin(2*pi*t);

plot(t,y,'-k', t,g,'o:r');

legend('t^2', 'sin(2\pi t)');

title("Mini Assignment #1");

xlabel('Time (ms)');

ylabel('f(t)');

0975e794f09b

exercise.jpg

Figure Adjustment(调整)

0975e794f09b

Snip20180213_5.png

Figure 's properties:

0975e794f09b

Figure Object.png

Axes 's properties:

0975e794f09b

Axes Object.png

Line 's properties:

0975e794f09b

Line Object.png

Text 's properties

0975e794f09b

Text Obejct.png

方案:

先找handle of object。

再获取或者修改object的属性。

0975e794f09b

策略.png

操作步骤:

1. 获取handle的方法:

0975e794f09b

get handle.png

2.1 查看某个object有哪些属性?用get(handle)方法,会在terminal打印所有properties:

0975e794f09b

get(handle),查看所有properties.png

2.2 设置坐标轴的范围:

0975e794f09b

set(handle, property, value).png

2.3 设置坐标轴字体大小、设置坐标轴的标记的内容:

0975e794f09b

set(handle, property, value).png

代码:

x = linspace(0,3);

y = x .^ 2 .* sin(x);

line_sin = plot(x,y);

line_2 = line([3*pi/4,3*pi/4], [0, (3*pi/4) ^ 2 * sin(3*pi/4)]);

str = '$$ \int_{0}^{3\pi/4} x^2\sin(x) dx $$';

anno_text = text(0.18, 3, str, 'Interpreter', 'latex');

annotation('arrow', 'X', [0.33, 0.55], 'Y', [0.6, 0.35]); %整个版面:X:[0,1] Y:[1,0]

% 调整图像属性:

% get(line_sin); %查看Line有哪些属性

set(line_sin, 'LineStyle','-', 'LineWidth',3, 'Color',[0,0.5,0.7]) %修改Line的属性

% get(line_2);

% set(line_2, 'LineStyle', '--');

set(line_2, 'Color', [0,0,0]);

% get(anno_text);

set(anno_text, 'FontSize', 25);

% get(gca); %查看坐标轴的所有属性

set(gca, 'FontSize', 15);

set(gca, 'XLim', [0,pi]);

% XTick 和 XTickLabel 是对应的:

set(gca, 'XTick', 0:pi/4:2*pi);

% set(gca, 'XTickLabel', 0:45:180);

set(gca, 'TickLabelInterpreter', 'TeX'); %改成字符型

set(gca, 'XTickLabel', {'0', '\pi/4', '\pi/2', '3\pi/4', '\pi'});

set(gca, 'YLim', [0,4.5]);

% delete(line_sin); %删除某个图像

0975e794f09b

after_Adjustment.jpg

例子:修改 Line Object 的 Marker

x = rand(20,1);

line1 = plot(x, 'd-m');

% get(gca);

set(gca, 'XLim',[1,20], 'FontSize',15);

% get(line1);

set(line1, 'LineWidth',2, 'MarkerFaceColor','g', 'MarkerEdgeColor','k', 'MarkerSize',10);

0975e794f09b

marker.jpg

Multiple Figure

figure 命令,用来创建figure window.

例:

x = -10 : 0.1 : 10;

y1 = x.^2 - 8;

y2 = exp(x);

figure, plot(x,y1);

figure, plot(x,y2);

0975e794f09b

Multiple_Figure.png

Be Careful using gcf、gca handle.

gcf、gca handle 指的是最后画的这个figure,前面的就找不到了。

Figure Position and Size :

0975e794f09b

Position.png

一个 Figure Window,画许多不同的 small figure,使用 subplot(m,n,1)

0975e794f09b

subplot.png

m: num of rows

n: num of cols

x、y轴的比例:

axis normal: 自动比例

axis square: 方的

axis equal: data units are the same(最正确的比例)

axis equal tight: 最正确的比例切掉空余地方。

0975e794f09b

axis_equal.png

打开/关闭 网格

打开/关闭 y轴的upper bound 和 x轴的upper bound(上边框和右边框)

打开/关闭 x、y轴

0975e794f09b

on/off.png

保存:

0975e794f09b

saveas.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值