축(axis) 속성
MATLAB은 그래프가 화면에서 가능한 한 많은 공간을 이용할 수 있도록 직사각형 모양의 좌표계를 만듬
1
axis 명령어 옵션
의미
axis auto 기본 축 한계로 되돌아감
axis equal x, y, z 축의 unit이 동등한 크기를 갖도록 설정
axis square 축 박스를 정사각형으로 설정
axis image axis equal + axis tight
t = 0:0.1:2*pi;
x = 2 * cos(t);
y = 2 * sin(t);
plot(x, y);
axis auto axis equal
axis square
축(axis) 속성
아래와 같이 타원형 그래프를 그려서, axis equal 과 axis square가 어떻게 다른지 확인해 봅시다.
2
t = 0:0.1:2*pi;
x = 2 * cos(t);
y = 4 * sin(t);
plot(x, y);
축(axis) 속성
아래와 같이 타원형 그래프를 그려서, axis equal 과 axis square가 어떻게 다른지 확인해 봅시다.
3
t = 0:0.1:2*pi;
x = 2 * cos(t);
y = 4 * sin(t);
plot(x, y);
axis equal
axis square
축 눈금 표시
축의 속성 ‘xtick’, ‘xticklabel’ 변경
4
x = 0:0.1:2*pi;
xlabel = linspace(0, 2*pi, 5);
w = '0 pi|0.5 pi|1 pi|1.5 pi|2 pi';
plot( x, sin(x) );
set(gca, 'xtick', xlabel, 'xticklabel', w);
Handle Graphics Object
한 그래프는 여러 그래픽 오브젝트들의 조합으로 만들어짐
• Handle이란? – MATLAB이 그래픽 오브젝트에 부여하는 고유의 식별 아이디
• Handle은 왜 필요한가? – 그래프 figure가 여러 개 생성되어 있을 때, 첫 번째 figure의 속성을 바꾸고 싶다면? – 첫 번째 figure의 handle로 접근이 가능
5
Figure Object
Axes Object
Line Object
Text Object
Handle Graphics Object
set 함수 • 매개변수로 지정한 그래픽 오브젝트의 속성을 변경
• set( handle, propertyname, propertyvalue, propertyname, propertyvalue, …)
6
>> set( 1, ‘color’ , ‘w’)
Figure의
Handle
특성 이름
(propertyname)
특성 값
(propertyvalue)
propertynames, propertyvalues
7
>> set(1) Alphamap
CloseRequestFcn: string -or- function handle -or- cell array
Color
Colormap
CurrentAxes
CurrentCharacter
CurrentObject
CurrentPoint
DockControls: [ {on} | off ]
FileName
IntegerHandle: [ {on} | off ]
InvertHardcopy: [ {on} | off ]
KeyPressFcn: string -or- function handle -or- cell array
KeyReleaseFcn: string -or- function handle -or- cell array
MenuBar: [ none | {figure} ]
Name
NextPlot: [ new | {add} | replace | replacechildren ]
NumberTitle: [ {on} | off ]
PaperUnits: [ {inches} | centimeters | normalized | points ]
PaperOrientation: [ {portrait} | landscape | rotated ]
PaperPosition
PaperPositionMode: [ auto | {manual} ]
PaperSize
PaperType: [ {usletter} | uslegal | A0 | A1 | A2 | A3 | A4 | A5 | B0 | B1 | B2 | B3 | B4 | B5 | arch-A | arch-B | arch-C | arch-D | arch-E | A | B | C | D | E | tabloid | <custom> ]
Pointer: [ crosshair | fullcrosshair | {arrow} | ibeam | watch | topl | topr | botl | botr | left | top | right | bottom | circle | cross | fleur | custom | hand ]
PointerShapeCData
PointerShapeHotSpot
Position
Renderer: [ {painters} | zbuffer | OpenGL | None ]
RendererMode: [ {auto} | manual ]
Resize: [ {on} | off ]
ResizeFcn: string -or- function handle -or- cell array
SelectionType: [ normal | open | alt | extend ]
ToolBar: [ none | {auto} | figure ]
Units: [ inches | centimeters | normalized | points | {pixels} | characters ]
WindowButtonDownFcn: string -or- function handle -or- cell array
WindowButtonMotionFcn: string -or- function handle -or- cell array
WindowButtonUpFcn: string -or- function handle -or- cell array
WindowKeyPressFcn: string -or- function handle -or- cell array
WindowKeyReleaseFcn: string -or- function handle -or- cell array
WindowScrollWheelFcn: string -or- function handle -or- cell array
WindowStyle: [ {normal} | modal | docked ]
그래픽 오브젝트 속성 변경해보기
8
>> close all
>> peaks
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2)
>> set( 1, ‘visible’, ‘off’ )
>> set( 1, ‘visible’, ‘on’ )
>> set( 1, ‘pos’, [230 320 360 350] )
>> set( 1, ‘color’, ‘w’ )
>> set( 1, ‘color’, ‘g’, ‘pos’, [100 320 100 350] )
…
…
특정 Handle을 가리키는 변수
gcf (get current figure ) • 현재 활성화 되어있는 figure window의 handle값을 갖고 있는 변수
gca (get current axes ) • 현재 활성화 되어 있는 figure window의 axes object의 handle 값을
갖고 있는 변수
gco (get current object) • 현재 활성화 되어 있는 figure window에서 마우스로 클릭한 그래프
요소의 handle 값을 갖고 있는 변수
9
특정 Handle을 가리키는 변수
10
>> t = -2:0.01:2;
>> y1 = t .^ 2;
>> y2 = t .^ 3;
>> plot( t, y1, t, y2 )
파란색 선 마우스로 선택
>> set( gco, ‘linestyle’, ‘:’ )
좌우 Y축 범위 다르게 plot
plotyy( X1, Y1, X2, Y2 )
11
x = 0:0.1:1;
plotyy(x, sin(x), x2, 10*cos(x));
여러 그래프를 하나의 그림에 나타내기
subplot(m, n, p) 또는 subplot(mnp) • 그림을 m x n 영역로 나누고, p번째 영역에 plot
12
subplot(2, 2, 1);
x = 0:0.1:2*pi;
plot( x, sin(x) );
subplot(2, 2, 2);
x = 0:0.1:10;
plot( x, sin(round(x)) );
subplot(2, 2, 3);
x = 0.01:0.01:2*pi;
plot( x, sinc(x) );
subplot(2, 2, 4);
x = 0:0.1:2*pi;
plot( x, sin(x) );
hold on;
plot( x, cos(2*x) );
1 2
3 4
여러 그래프를 하나의 그림에 나타내기
13
subplot(2, 2, [1 3]);
x = 0:0.1:2*pi;
plot( x, sin(x) );
subplot(2, 2, 2);
x = 0:0.1:10;
plot( x, sin(round(x)) );
subplot(2, 2, 4);
x = 0:0.1:2*pi;
plot( x, cos(2*x) );
Quiz
이전 슬라이드의 그래프와 다르게, 첫 번째 그래프가 가로로 넓게 펼쳐진 형태의 figure를 만들어 봅시다.
14
축 방향 거꾸로 만들기
set(gca, ‘xdir’ 또는 ’ydir’ 또는 ’zdir’, ‘rev’)
x 축 또는 y 축이 일반적인 방향의 역방향으로 생성되도록 드로잉 • x축의 값 증가 방향: “왼쪽오른쪽” 에서 “오른쪽왼쪽”
• y축의 값 증가 방향: “아래쪽위쪽” 에서 “위쪽아래쪽”
15
t = 0:0.1:2*pi;
subplot(1, 2, 1);
plot( t, sinc(t) );
subplot(1, 2, 2)
plot( t, sinc(t) );
set( gca, 'xdir', 'rev' );
grid on/off 외의 속성들
xgrid, ygrid, zgrid : 각각의 좌표축에만 grid 설정
gridlinestyle : grid 라인의 스타일 변경
16
t = 0:0.001:1;
y = exp(-20*t) .* sin(200*t);
plot(t, y);
set(gca, 'xgrid', 'on', 'gridlinestyle', '-.');
곡선과 눈금 모양 설정
plot 함수에서 선의 굵기, marker의 크기 및 색상, 폰트의 크기 등을 설정 가능
17
plot 함수 옵션 기본값
LineWidtn 0.5
MarkerSize 6
MarkerEdgeColor auto
MarkerFaceColor none
FontSize 10
FontAngle normal
>> plot(x,sin(x), '^r--',
'LineWidth', 2,
'MarkerEdgeColor', 'g',
'MarkerFaceColor', 'b')
ez* 그래프 드로잉 함수들
18
ez* 함수
19
ezplot (2D plotter)
20
>> ezplot( ‘exp( x^2 ) / x’ )
>> ezplot( 'sin(x) + sin(y) = sin(x*y)' )
ezplot (2D plotter)
21
>> ezplot('sinc(x)', [-1, 10])
>> ezplot('x^2+y^2 = 3^2', [-pi 0.7*pi])
ezplot (2D plotter)
22
>> ezplot('cos(t)', 'cos(t)*sin(t)')
>> ezplot('cos(t)', 'cos(t)*sin(t)', [0, 3])
ezplot3 (3D plotter)
23
>> ezplot3( 'cos(t)','t*sin(t)','sqrt(t)‘ )
ezplot3 (3D plotter)
24
>> ezplot3( 'cos(t)','t*sin(t)','sqrt(t)‘, [0, 10] )
ezplot3 (3D plotter)
25
>> ezplot3( 'cos(t)','t*sin(t)','sqrt(t)‘, [0, 10], ‘animate’ )
ezcontour
26
>> ezcontour('sin(3*y-x^2+1) + cos(2*y^2-2*x)', [-2 2 -1 1])
Quiz
27
ezcontourf
28
>> ezcontour('sin(3*y-x^2+1) + cos(2*y^2-2*x)', [-2 2 -1 1])
ezcontourf
29
>> ezcontour('sin(3*y-x^2+1) + cos(2*y^2-2*x)', [-2 2 -1 1])
ezsurf
30
>> ezsurf('sin(3*y-x^2+1) + cos(2*y^2-2*x)', [-2 2 -1 1])
ezsurf
31
>> ezsurf('sin(3*y-x^2+1) + cos(2*y^2-2*x)', [-2 2 -1 1])
ezsurf
32
>> ezsurf( 'x', 'x+y', 'sin(x)+cos(2*y)‘ )
ezsurfc
33
>> ezsurfc('x.*exp(-x.^2 - y.^2)')
Quiz
34
Quiz Sol.
35
>> ezsurf('cos(x)*cos(y)', 'sin(x)', 'cos(x)*sin(y)', [0, 2*pi])
ezmesh, ezmeshc
36
ezpolar (polar coordinate plotter)
37
>> ezpolar( 'sin(2*t)*cos(3*t)', [0 pi] )
2D 그래프 드로잉 함수들
38
bar 그래프
39
subplot(3,1,1);
bar( rand(10,5), 'stacked‘ );
subplot(3,1,2);
bar( 0:.25:1, rand(5), 1);
subplot(3,1,3);
bar( rand(2,3), .75, 'grouped‘ );
>> x = [55 68 76 80 95];
>> y = [6 12 18 11 4];
>> bar(x, y);
>> barh(x, y);
bar 그래프
수능 등급 별 학생수
40
수능등급 1반 2반 3반
1등급 5 7 4
2등급 12 11 15
3등급 19 20 17
4등급 7 6 5
5등급 2 1 3
S = [5 7 4; 12 11 15; 19 20 17; 7 6 5; 2 1 3];
bar(S);
legend( ‘1반’, ‘2반’, ‘3반’ );
xlabel( ‘등급’ );
ylabel( ‘학생수’ );
bar3 그래프
수능 등급 별 학생수
41
수능등급 1반 2반 3반
1등급 5 7 4
2등급 12 11 15
3등급 19 20 17
4등급 7 6 5
5등급 2 1 3
S = [5 7 4; 12 11 15; 19 20 17; 7 6 5; 2 1 3];
bar3(S)
legend('1반', '2반', '3반');
xlabel('반');
ylabel('학생수');
ylabel('등급');
zlabel('학생수');
hist 그래프
r = hist( Y ) • 입력 데이터 Y의 범위를 균일하게 10등분
하여 해당 범위 내에 속하는 원소들을 count
r = hist( Y, k ) • 입력 데이터 Y의 범위를 균일하게 k등분 하여
해당 범위 내에 속하는 원소들을 count
42
>> hist( randn(1000,1) )
>> hist( randn(1000, 50) )
area 그래프
43
S = [5 7 4; 12 11 15; 19 20 17; 7 6 5; 2 1 3];
area(S);
legend( ‘1반’, ‘2반’, ‘3반’ );
xlabel( ‘반’ );
ylabel( ‘학생수’ );
pie 그래프
44
x = [1 2 3 4 5];
y = [16 38 56 18 6];
pie(x, y);
legend('grade 1', 'grade 2', 'grade 3', 'grade 4', 'grade 5', -1);
pie3 그래프
h = pie3( X ) • 배열 X를 구성하는 각각의 원소들을 하나의 조각으로 표현
h = pie3( X, explode ) • explode의 nonzero에 있는 조각을 pie chart 중심에서 이탈시켜 표현
45
>> x = [1 3 0.5 2.5 2];
>> explode = [0 1 0 0 1];
>> pie3( x, explode );
계단 모양 그래프
이산 데이터 표시 등에 사용될 수 있는 그래프
46
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> stairs( y );
stem 그래프
이산 신호 임펄스 응답 나타낼 때 이용
47
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> stem( y );
errorbar 그래프
데이터의 신뢰도 표시에 사용되는 그래프 • 오차 데이터 지정해야 함
48
>> x = 0:0.5:2*pi;
>> y = sin(x);
>> lowlimit = 0.1*ones(size(x));
>> highlimit = 0.3*ones(size(x));
>> errorbar(x, y, lowlimit, highlimit);
Quiz
49
Quiz Sol.
50
x = -2:0.01:2;
n = 100;
f = zeros(1, length(x));
for k=1:2:n
f = f + sin(pi*k*x)/k;
end
plot(x,f);
Plot Editing
51
Plot Editing
그래프 회전, 텍스트 레이블, 화살표, 도형, …
52