您的位置:首页 > 编程语言 > MATLAB

五、Matlab 之 绘图操作(上)

2017-12-08 16:46 316 查看
1、plot – 最常见的绘图指令

plot(x, y) – 两个参数,对应x 和 y

plot(y) – 只有一个参数,则x 按照1、2、……n的顺序

2、hold on/off

用来决定本次图形是否在下一次仍然保持.

3、样式选择

格式为 plot ( x, y, ‘str’);



更多详情戳这里:::

eg: plot(1:2:20, ‘or–’)

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


4、titile 和 label

xlabel()

ylabel()

title()

zlabel()

x = 0:0.1:2*pi; y1 = sin(x); y2 = exp(x);
plot(x, y1, '--*', x, y2, ':0');
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}');


注意这里用到了LaTex语法,,,, \pi 表示了 π,,,,

再看一段,,,

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


feature 调整?

Font、Font Size、Line width、 Axis limit、Tick position、Tick label

figure –> axes –> line、test……

改变 axes 的外围坐标值,,,

set(handle, gca's preporsity, [××])
set(gca, 'XLim', [0, 2*pi]);
set(gca, 'XLim', [-1.2, 1.2]);

当然了,我们还可以这样写:
xlim([0, 2*pi]);
ylim([-1.2, 1.2]);


Axes 的 font 和 tick,,,

set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);%%每个小格的间隔是pi/2
set(gca, 'XTickLabel', 0:90:360);%%但是每个显示的是0 90 。。。。


如果想要显示·和π的形式对应一样的,需要先把axes的类型转化为symbol,,,然后再写;;

set(gca, 'FontName', 'symbol');
set(gca, 'XTickLabel1', {'0', 'p/2', 'p', '3p/2', '2p'})


下面设置 Line的属性

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


还有一个就是打的点的设置了,,,又叫Maker的设置,,,

x = rand(20, 1);
set(gca, 'FontSize', 18);
plot(x, '-md', 'LineWidth', 2, 'MakerEdgeColor', 'k', ..... 'MakerFaceColor', 'g', 'MakerSize', 10);
xlim([[1, 20]);


上述代码就是代表线的 facecolor,,,为green,,,,然后边缘的Edgecolor为black,,,

如果想画多个图像在不同的坐标系呢?

figure, plot(x, y1);

figure, plot(x, y2);

注意: gca 、 gcf是只能指到现在的gca和gcf的,,好像C语言的指针一样,特别容易搞错,要特别注意。。

5、gcf 和 gca的区别。。。

gcf - graphic 的 feature,,

gca - graphic 的 Axes

6、figure的一些应用

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




许多图像画在一个figure上面,,,

每一个figure叫做 subplot

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






如何储存呢???

两种格式, Bitmap 和 Vector,,,,

向量的不会失真,,相反那个会失真。



一个作业记录

一个作业代码记录

x = [1:0.02:2];
y1 = x.*x; y2 = sin(2*pi*x);
h1 = plot(x,y1,'-k');
hold on;
h2 = plot(x,y2,'or','MarkerFaceColor','c');
xlabel('Times(ms)');
ylabel('f(t)');
title('Mini Assiginment #1');
legend('t^2', 'sin(2\pi t)','Location','NorthWest'); %注意最后一个属性,666
set(gca, 'Fontsize', 15); % gca就是外面的那个框的句柄
% set(gca, 'XTick', 1:0.2:2);
% set(gca, 'XTickLabel', 1:0.2:2);
set(gca, 'YTick', -1:0.5:4); %设置间隔
% set(gca, 'YTickLabel', -1:0.5:2);
set(h1, 'LineWidth', 2.5);
set(h2, 'Color', 'c');


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: