MATLAB學習筆記四:Basic plotting ,Graphical object properties

1. 基礎繪圖


1.1 根據Data畫圖

1.1.1 plot()

  • plot(x, y)
  • plot(y) % 其中x = [1...n], n = length(y)
  • EX:
plot(cos(o:pi/20:2*pi));

在這裏插入圖片描述

1.1.2 hold on/off

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

在這裏插入圖片描述

1.2 繪圖形式

  • plot(x, y, 'str')'str'見下表:
數據標識 線型 顏色
. - k
* - - b
X -. c

請見官方文檔

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

在這裏插入圖片描述

1.2.2 title()label()

  • title()
  • xlabel()
  • ylabel()
  • zlabel()
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}');

1.2.3 text()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]); % 

1.2.4 EX

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

在這裏插入圖片描述


2. 圖形對象參數


2.1 Graphical Object

2.2 Figure Properties

2.3 Modifying Properties of An Object

2.3.1 Identifying the Handle of An Object

在這裏插入圖片描述

2.3.2 Fetching or Modifying Properties

  • fetch properties 取出屬性
x = linspace(0, 2*pi, 1000);
y = sin(x);
h = plot(x,y);
get(h)

get(gca)
  • modify properties 修改屬性
% Setting Axes Limits

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

% 同樣的效果:

% xlim([0, 2*pi]);
% ylim([-1.2, 1.2]);

% Setting Font and Tick of Axes

set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', 0:90:360);

% Setting Axes Symbol

set(gca, 'FontName', 'latex');  % 不再支持symbol,而是支持latex;
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});

% Setting Line style and width

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

% 上面的代碼也可替換成這樣寫

plot(x,y, '-.g', ...
    'LineWidth', 7.0);

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

2.3.3 EX

  • 將上一個練習中的圖變清晰:
    在這裏插入圖片描述
t = 1:0.01:2;
f = t.^2;
g = sin(2*pi*t);
h = plot(t, f);
plot(t,f,'-k','LineWidth',4);
hold on;
plot(t, g, 'om', 'MarkerFaceColor', 'k');
title('Mini Assignment #1');
xlabel('Time(ms)');
ylabel('f(t)');
legend('t^2','sin(2\pit)');
set(gca, 'FontSize', 18);

2.4 Multiple Figures

% 畫多個圖
% 注意gcf,gca只能指到當前的figure

x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);  % 利用figure
figure, plot(x,y2);
  • Figure Position and Size
figure('Position', [left, bottom, width, height]);

在這裏插入圖片描述

  • Several Plots in One Figure
% several small plots 'in a figure'

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
  • Control of Grid, Box, and Axis
    在這裏插入圖片描述

  • Saving Figures into Files

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

在這裏插入圖片描述

  • 如果需要高分辨率的圖,使用print,見print
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章