1.2 豊富な可視化機能：MATLAB基本関数

# 目次へのリンク
[MATLABによる画像処理・コンピュータービジョン入門目次](https://qiita.com/tohruk/items/e5f7939b622bcf2e3c4c)

# 概要


MATLABのさまざまな可視化機能を紹介します。




対応ファイル：[I1_02_visualization.m](https://github.com/mathworks/MATLAB-IPCV-Eval-Kit-JP/blob/master/demo_files/I1_02_visualization.m)


# パスを通す


デモフォルダにパスを通します。


```matlab:code
addpath('.\I1_02_grademo')
```
# 2次元ラインプロット
```matlab:code
type line2d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])
x=-3:0.1:3;
y1=sin(x);
y2=sin(x.^2);
y3=cos(x.^2);
plot(x,y1,x,y2,'--',x,y3,':')
title('sin(x), sin(x^2), cos(x^2)')
```
```matlab:code
line2d
```
![figure_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/57933e97-b908-cba9-7510-52cb1b320048.png)


# 2次元ラインプロット(マーカーつき)
```matlab:code
h=findobj(gca,'type','line');
set(h(1),'marker','o')
set(h(2),'marker','*')
set(h(3),'marker','d')
grid on
```

![figure_1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/645486ec-cd19-37bd-6ec0-9c5558dc9c7a.png)

# 極座標プロット (polar)
```matlab:code
type polarplot.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

theta=0:2*pi/100:2*pi;
r=theta/(2*pi);
polar(theta,r)
```
```matlab:code
polarplot
```

![figure_2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/04290394-3f0d-0eb2-075d-01251a43543d.png)


# コンパスプロット
```matlab:code
type compassplot.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

Z = eig(randn(20,20));
compass(Z)
```
```matlab:code
compassplot
```

![figure_3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/5cadae2a-1f96-cabe-deea-56175150f499.png)


# 領域の塗りつぶし (fill)
```matlab:code
type fill2d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

x=-5:0.05:5;
p=[1 0 -20 10];
y=polyval(p,x);
r=sort(roots(p));
x1=r(1):0.1:r(2);
y1=polyval(p,x1);
plot(x,y)
hold on
fill(x1,y1,'r')
grid
```
```matlab:code
fill2d
```

![figure_4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/6b30c648-3896-1b15-561f-bc047513bb62.png)


# 2次元離散プロット (stem)
```matlab:code
type stem2d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

alpha=0.02; beta=0.5;
t=0:2:200;
y=exp(-alpha*t).*sin(beta*t);
stem(t,y)
title('y=exp^{(-\alphat)}\cdotsin(\betat)')
```
```matlab:code
stem2d
```

![figure_5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/d5dd17ee-bc8d-34dc-f2cc-cf62383df581.png)


# 階段状プロット (stairs)
```matlab:code
type stair2d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

alpha=0.02; beta=0.5;
t=0:2:200;
y=exp(-alpha*t).*sin(beta*t);
stairs(t,y)
title('y=exp^{(-\alphat)}\cdotsin(\betat)')
```
```matlab:code
stair2d
```


![figure_6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/7e94bcbe-d92c-b40f-b6f7-b1f8639e77b9.png)

# 3次元ラインプロット
```matlab:code
type line3d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

t=0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
grid
xlabel('x')
ylabel('y')
zlabel('z')
```
```matlab:code
line3d
```

![figure_7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/531f1f0e-9e7e-801b-e15b-ff2003c5185c.png)


# メッシュプロット (mesh)
```matlab:code
type meshdemo.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[x,y,z]=peaks(50);
mesh(x,y,z)
title('メッシュプロット','fontname','ＭＳ ゴシック')
```
```matlab:code
meshdemo
```


![figure_8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/77d479fb-3991-0012-1c9e-ccbe89033323.png)

# 表面プロット (surf)
```matlab:code
type surfdemo.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[x,y,z]=peaks(50);
surf(x,y,z)
title('表面プロット','fontname','ＭＳ ゴシック')
```
```matlab:code
surfdemo
```


![figure_9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/b11dca41-a814-629c-8f37-75affc0369a4.png)

# 3次元断面プロット (slice)
```matlab:code
type slicedemo.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[x,y,z] = meshgrid(-2:.1:2, -2:.1:2, -2:.1:2);
v = x .* exp(-x.^2 - y.^2 - z.^2);

[xi,yi]=meshgrid(0:.1:4);
zi=xi.^2+yi.^2;

slice(x,y,z,v,[.8 ],[],[ -.5])
hold on
slice(x,y,z,v,xi-2,yi-2,zi/8-2);
hold off
```
```matlab:code
slicedemo
```


![figure_10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/a3931c5f-09c7-22c3-c91c-555d2c67c79d.png)

# 3次元オブジェクト (surf)
```matlab:code
type obj3d1.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[x1,y1,z1]=sphere(30);
[x2,y2,z2]=cylinder(1:-0.05:0,20);
x2=x2-1;
y2=y2-1;
z2=z2-1;
surf(x1,y1,z1)
hold on
surf(x2,y2,z2)
light('position',[1 -1 1]);
shading interp
lighting phong
colormap(copper)
axis equal
title('3次元オブジェクト','fontname','ＭＳ ゴシック')
```
```matlab:code
obj3d1
```


![figure_11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/3c683be8-75ee-6f4a-7426-5dc0f433ffcf.png)

# 2次元等高線図 (contour)
```matlab:code
type cont2d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[X,Y]=meshgrid(-2:.2:2,-2:.2:2);
Z = X.*exp(-X.^2-Y.^2);
[c,h]=contour(X,Y,Z);
clabel(c,h)
title('z=xe^{(-x^2-y^2)}')
```
```matlab:code
cont2d
```


![figure_12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/964d18a0-277b-3822-3a45-abe869f62a64.png)

# 塗りつぶし2次元等高線図
```matlab:code
type cont2df.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[X,Y,Z]=peaks;
[c,h]=contourf(X,Y,Z,[-7 -5.5 -1 0 0.9 2.6 4.7 8]);
clabel(c,h)
colorbar
```
```matlab:code
cont2df
```

![figure_13.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/0d742eec-b77b-3d5c-ca55-a122c947def9.png)


# 3次元等高線図 (contour3)
```matlab:code
type cont3d.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[X,Y,Z]=peaks;
contour3(X,Y,Z,20)
colorbar
```
```matlab:code
cont3d
```


![figure_14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/4960e876-9772-9810-4072-384f57394e36.png)

# 円グラフ (pie)
```matlab:code
type piedemo.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

x=[1 3 0.5 2.5 2];
explode=[0 0 0 1 0];
subplot(1,2,1),pie(x,explode)
subplot(1,2,2),pie3(x,explode);
colormap(cool)
```
```matlab:code
piedemo
```


![figure_15.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/67a77482-c7c0-987b-75e0-2621092f1c9c.png)

# 棒グラフ
```matlab:code
type bargraph.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

Y=rand(5);
subplot(2,2,1),bar(Y,'stacked')
subplot(2,2,2),barh(Y(:,1)) 

X=rand(5);
subplot(2,3,4),bar3(X), set(gca,'ylim',[0 6])
subplot(2,3,5),bar3(X,'grouped')
subplot(2,3,6),bar3(X,'stacked')
colormap(summer)
```
```matlab:code
bargraph
```


![figure_16.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/6f78deb6-db91-fee4-af8e-993181a3c49e.png)

# ヒストグラム (hist)
```matlab:code
type histplot.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

x=randn(500,1);
hist(x,20)
title('ヒストグラム','fontname','ＭＳ ゴシック')

histfit(x,20)
title('ヒストグラム','fontname','ＭＳ ゴシック')
```
```matlab:code
histplot
```


![figure_17.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/edf8ae87-9609-65c8-c03f-676b2a223755.png)

# 画像表示 (imshow)
```matlab:code
type implot.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

imshow(imread('ngc6543a.jpg'))
```
```matlab:code
implot
```


![figure_18.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/a675b0a8-21fa-1322-568a-e7a0d59c8964.png)

# 表面プロットへの貼り付け(warp)
```matlab:code
type warpdemo.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

load clown
[x,y,z]=cylinder;

subplot(2,2,1)
image(X)
axis image

subplot(2,2,3)
h=mesh(x,y,z);
set(h,'edgecolor',[0 0 0])

subplot(122)
warp(x,y,z,flipud(X),map)
axis square
```
```matlab:code
warpdemo
```

![figure_19.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/02642920-3df8-54c1-a618-f73eddb07a33.png)


# Delaunayの三角メッシュ
```matlab:code
type tridemo.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.0098 0.3529 0.5469 0.5469])
load seamount
tr=delaunay(x,y);
trisurf(tr,x,y,z)
axis([210.85 211.7 -48.45 -47.95 -4300 -500])
map=pink(64);
colormap(map(10:end,:))

f2=figure(2); clf reset
set(f2,'units','normalized','position',[0.4385 0.0182 0.5469 0.5469])
plot(x,y,'k.','markersize',10)
hold on
trimesh(tr,x,y,z)
hidden off
grid
map=pink(64);
colormap(map(10:end,:))
```
```matlab:code
tridemo
```

![figure_20.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/6d665462-8562-31d0-9add-cb8067802680.png)



![figure_21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/4c332473-de81-5973-f3ac-49540e93fc4b.png)

# メッシュ、コンター
```matlab:code
type meshcontour.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.0098 0.3529 0.5469 0.5469])
load data3d1
h=surf(x(:,:,30),y(:,:,30),130*ones(21,31)-130,v(:,:,30)*30-125);
%set(h,'edgecolor','none')
shading interp
hold on
h=mesh(x(:,:,30),y(:,:,30),v(:,:,30)*30-125);
set(h,'facecolor','none')
[c,h]=contour3(x(:,:,30),y(:,:,30),v(:,:,30)*30-125,10,'k');
set(h,'linewidth',1)
axis equal
map=jet(64);
colormap(map(10:end,:))

f2=figure(2); clf reset
set(f2,'units','normalized','position',[0.4385 0.0182 0.5469 0.5469])
[px,py]=gradient(v(:,:,30));
contour(x(:,:,30),y(:,:,30),v(:,:,30),20)
hold on
h=quiver(x(:,:,30),y(:,:,30),px,py);
set(h,'color','k','linewidth',1)
axis equal
axis([0 60 0 40])
map=jet(64);
colormap(map(10:end,:))
```
```matlab:code
meshcontour
```

![figure_22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/e09aa53c-101e-2d48-3591-06ba77315a47.png)
![figure_23.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/fca2d85b-21e0-ca8d-d25a-9734aae20a87.png)


# 流速の等値面
```matlab:code
type flowiso2.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[x y z v] = flow;
p = patch(isosurface(x, y, z, v, 0));
isonormals(x,y,z,v,p);
set(p, 'facecolor', 'r', 'edgecolor', 'n');
daspect([1 1 1]);
view(3);
camlight 
lighting p
%isolims(x,y,z,v)
camproj p; 
    
delete(p)

for i = -11:2
  p = patch(isosurface(x, y, z, v, i, 'v'));
  isonormals(x,y,z,v,p);
  set(p, 'facec', 'f', 'cdata', i, 'edgec', 'n');
end

set(gcf, 'color', [.3 .3 .3])
set(gca, 'color', 'k')
set(gca, 'xcolor', 'w')
set(gca, 'ycolor', 'w')
set(gca, 'zcolor', 'w')
box on
% lighting g
lighting phong
colorbar;

axis tight

p = findobj('type', 'patch');
caxis(caxis)
```
```matlab:code
flowiso2;
```


![figure_24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/67edde37-7532-aab3-05d6-df72c6c224bd.png)

# スライス平面上に等高線
```matlab:code
type cslice.m
```
```text:output
f1=figure(1); clf reset
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

[x y z v] = flow;
h=contourslice(x,y,z,v,[1:9],[],[0], linspace(-8,2,10));
axis([0 10 -3 3 -3 3]); daspect([1 1 1])
camva(32); camproj perspective;
campos([-3 -15 5])
set(gcf, 'Color', [.3 .3 .3], 'renderer', 'zbuffer')
set(gca, 'Color', 'black' , 'XColor', 'white', ...
         'YColor', 'white' , 'ZColor', 'white')
box on
```
```matlab:code
cslice
```

![figure_25.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/f687a25e-c7b1-bbca-5892-0e27aba26a2e.png)


# 頭部の断面図
```matlab:code
type headiso_h.m
```
```text:output
f1=figure(1); clf('reset')
set(f1,'units','normalized','position',[0.3652 0.3008 0.6016 0.6016])

load headsmall
[x y z data2] = subvolume(data, [nan 30    nan 30   nan 45]);
p = patch(isosurface(x,y,z,data2, 30), 'FaceColor', 'r', 'EdgeColor', 'n');
isonormals(x,y,z,data2,p)
p2 = patch(isocaps(x,y,z,data2, 30), 'FaceColor', 'i', 'EdgeColor', 'n');

[x y z data2] = subvolume(data, [30 nan    nan nan   nan nan]);
p = patch(isosurface(x,y,z,data2, 30), 'FaceColor', 'r', 'EdgeColor', 'n');
isonormals(x,y,z,data2, p)
p2 = patch(isocaps(x,y,z,data2, 30), 'FaceColor', 'i', 'EdgeColor', 'n');

view(-130,30);axis vis3d tight; daspect([1 1 1]); h = camlight('left'); 
lighting phong
colormap(gray(100))

set(gcf, 'color', [.3 .3 .3])
set(gca, 'color', 'k')
set(gca, 'xcolor', 'w')
set(gca, 'ycolor', 'w')
set(gca, 'zcolor', 'w')
```
```matlab:code
headiso_h
```


![figure_26.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/14336/3ec82e19-b178-c3ff-8331-8b5911fdd137.png)

# 終了
```matlab:code
% パスを削除
rmpath('.\I1_02_grademo')
```
# *参考*

   -  [MATLAB Plot Gallery](https://jp.mathworks.com/products/matlab/plot-gallery.html) 

# *謝辞*


本記事は @eigs さんの[livescript2markdown](https://github.com/minoue-xx/livescript2markdown)を使わせていただいてます。




# 目次へのリンク
[MATLABによる画像処理・コンピュータービジョン入門目次](https://qiita.com/tohruk/items/e5f7939b622bcf2e3c4c)