Matlab繪圖基礎——圖形修飾處理(入門)


引入——標題、色條、坐標軸、圖例等
例一:
set(groot, 'defaultAxesLineStyleOrder' , 'remove' , 'defaultAxesColorOrder' , 'remove' );
     %每次使用記得清除上次設置的參數,否則設置的參數會被保留下來
X = linspace(0,2*pi,30);
Y = [cos(X); exp(0.05*X)]';
 
m = stem(X,Y);               %m是圖像的句柄
title( '這里是標題' );          %標題
xlabel( 'x的坐標/m' );         %坐標軸
ylabel( 'y的坐標/t' , 'fontsize' ,14);
legend( '圖一' , '圖二' , 'Location' , 'southeast' , 'FontSize' ,19);   %方法一
legend({ '圖一' , '圖二' }, 'Position' ,[0.65,0.2,0.2,0.1], 'FontWeight' , 'bold' );    %方法二
legend( 'boxoff' );   %Remove the legend border.
     %鼠標可以直接移動legend,雙擊文字還可更改文字內容
text(0.2,-0.7, '這里可以填文字' , 'fontsize' ,10, 'FontWeight' , 'bold' );
axis(gca,[0 6 -1 1.5]);    %%設置只顯示X=-3:3,Y=-2:4,Z=-10:10范圍的圖像
axis  tight                    %以最大范圍(定義域的范圍)顯示此圖像      
     %axis auto              %將坐標軸設為自動返回缺省值  
grid  on ;         %顯示網格
set(gca, 'xtick' ,0:0.75:7);    %設置x軸的刻度區間為0.75,既每隔0.75標注一個點
grid  off ;         %消除網格            
m(1).Color =  'k' ;
m(1).LineStyle= '--' ;
m(2).Marker =  's' ;       %square
 
hbase=m.BaseLine;
hbase.BaseValue=0.7;
hbase.LineStyle= '-.' ;
set(gca, 'color' , 'y' );        %設置圖像的背景顏色為黃色
axis  off ;
hbase.Visible =  'off' ;
axis  on ;
例二:
peaks; m=gca;
axis(m,[-3 3 -4 4,-10,10]);    %%設置只顯示X=-3:3,Y=-2:4,Z=-10:10范圍的圖像
    % 只有上式可以更改了取值范圍,下式只能更改區間間隔
set(m, 'xtick' ,-3:0.75:2);      %設置x軸的左邊范圍及間隔
set(gca,  'XTick' , []);         % 隱藏x軸的標記
set(gca,  'YTick' , []);         % 隱藏y軸的標記
box  on                          %把三維圖形封閉的箱體里,箱子大小用axis定義
hcb = colorbar( 'southoutside' );   %橫向放置的colorbar
     %%colorbar;   %顯示橫放的色條(默認)
set(get(hcb, 'Xlabel' ), 'String' , '這是橫向放置的colorbar' , 'FontSize' ,12);
     %設置的是colorbar的Xlabel,然后加上文字 文字大小為12
     %在顯示的圖像中,用鼠標點擊Edit->Colormap選項可以手動更改顏色區間
set(hcb, 'Ticks' ,[-5,-2,1,4,7], 'TickLabels' ,{ 'Cold' , 'Cool' , 'Neutral' , 'Warm' , 'Hot' })
     %Specify Colorbar Ticks(標記) and Tick Labels
set(hcb, 'Direction' , 'reverse' );   %反轉colorbar的方向,連同文字一起反向
colormap  autumn                  %顏色映射表colormap,如果沒有定義C,則按Z(高度)進行顏色的映射
     %還有多種選項,具體參考matlab幫助——colormap
shading  interp                %%shading使圖像的網格曲面美觀
     %進行插值,可平滑小面描影並刪除網格線
     %還有其他參數shading flat;shading faceted;效果可以參考幫助
2、視點處理
%MATLAB提供了設置視點的函數view,其調用格式為:
view(-34,50);    %設置觀測者的位置,方位角za=34,仰角el=30
     %方位角從負y軸逆時針起算(向左為負,向右為正),
     %仰角從xoy面向上起算(向上為正);
view(3); %以默認視角查看該圖。(默認az=-37.5,el=30,單位:°)
應用舉例:從不同的角度查看多峰函數(peaks)
   subplot(2,2,1);mesh(peaks);
   view(-37.5,30);        %指定子圖1的視點
   title( 'azimuth=-37.5,elevation=30' )
   subplot(2,2,2);mesh(peaks);
   view(0,90);             %指定子圖2的視點
   title( 'azimuth=0,elevation=90' )
   subplot(2,2,3);mesh(peaks);
   view(90,0);             %指定子圖3的視點
   title( 'azimuth=90,elevation=0' )
   subplot(2,2,4);mesh(peaks);
   view(-7,-10);           %指定子圖4的視點
   title( 'azimuth=-7,elevation=-10' )


3、色彩處理

1.色圖

色圖(Color map)MATLAB系統引入的概念。在MATLAB中,每個圖形窗口只能有一個色圖。色圖是m×3 的數值矩陣,它的每一行是RGB三元組。色圖矩陣可以人為地生成,也可以調用MATLAB提供的函數來定義色圖矩陣。

我的解釋:colormap代表就是一個映射,它不是函數類型的映射,而是用一個三列向量來代表,三個分量分別代表R、G、B。

在數字圖像中,它可以將灰度圖映射為偽彩色圖像,如下圖:

 

A = imread( 'pout.tif' ) ; 
imshow(A);colorbar

 

colormap jet

 

colormap  gray    %返回到原圖

    所以可以把數字圖像的每個點的灰度看做普通三維圖像的Z軸,將Z軸高度映射為colormap中的顏色;
詳細可以參考: __傳送門__
 
《數字圖像處理Matlab》 —— 岡薩雷斯P147  的解釋:

 

    索引圖像(偽彩色圖像)有兩個分量,即整數矩陣X和彩色映射矩陣map.矩陣map是一個大小為m*3且范圍在[0,1]之間的浮點值構成的double類數組。map的長度m同它所定義的顏色數目相等。map的每一行都定義單身的紅、綠、藍三個分量。索引圖像將像素值“直接映射”到彩色值。每個像素的顏色有對應的整數矩陣X的值作為指向map的一個指針決定。

4、三維表面圖形的着色: 實際上就是在網格圖的每一個網格片上塗上顏色。
%surf函數用默認/缺省的着色方式對網格片着色。
%除此之外,還可以用shading命令來改變着色方式。
%%3種圖形着色方式的效果展示。
[x,y,z]=sphere(20);
subplot(2,2,1);surf(x,y,z);
axis  equal ;title( '原圖' )
subplot(2,2,2);surf(x,y,z);
shading  flat ; %each mesh line segment and face has a constant color determined by the color value 
     %at the endpoint of the segment or the corner of the face that___has the smallest index or indices(索引)___.
axis  equal ;title( 'flat' )
subplot(2,2,3);surf(x,y,z); 
shading  interp ;   %命令在網格片內采用顏色插值處理,得出的表面圖顯得最光滑
     % varies the color in each line segment and face by interpolating the colormap index or true color value across the line or face.
axis  equal ;title( 'interp' )
subplot(2,2,4);surf(x,y,z);
shading  faceted    %命令將每個網格片用其高度對應的顏色進行着色,但網格線仍保留着
     % flat shading with superimposed(疊加) black mesh lines. This is the default shading mode.
axis  equal ;title( 'faceted' )
colormap  jet ;
 
 
5、光照處理
%MATLAB提供了燈光設置的函數,其調用格式為:
%light('Color',選項1,'Style',選項2,'Position',選項3)
[x,y,z]=sphere(20);
subplot(2,2,1);
surf(x,y,z);axis  equal ; %原圖
 
subplot(2,2,2);
surf(x,y,z);axis  equal ;
light( 'Posi' ,[0,1,1]);
shading  interp ;hold  on ;
plot3(0,1,1, 'p' );text(0,1,1, ' light' );
hold  off ;
 
subplot(2,2,3);
surf(x,y,z);axis  equal ;
light( 'Posi' ,[1,0,1]);
shading  interp ;
hold  on ;
plot3(1,0,1, 'p' );text(1,0,1, ' light' );
hold  off
 
subplot(2,2,4);
surf(x,y,z);axis  equal ;
light( 'Posi' ,[1,0,1]);
light( 'Posi' ,[1,0,1]);   %重復兩次光照
shading  interp ;hold  on ;
plot3(1,0,1, 'p' );text(1,0,1, ' light' );
colormap  bone

 


 

 

6、圖形的裁剪處理

%繪制三維曲面圖,並進行插值着色處理,裁掉圖中x^2+y^2<=1部分
[x,y]=meshgrid(-5:0.1:5);
z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4);
surf(x,y,z);shading  interp ;

pause                  %程序暫停
     %為了展示裁剪效果,第一個曲面繪制完成后暫停,然后顯示裁剪后的曲面。
i=find(x.^2+y.^2<=1);
z1=z;z1(i)=NaN;
surf(x,y,z1);shading  interp ;

 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM