Matlab繪圖基礎——axis設置坐標軸取值范圍


簡介

peaks;

axis tight %Set the axis limits to equal the range of the data

axis square

axis 'auto x' %x軸坐標上下限自動調整

axis off %Plot a surface without displaying the axes lines and background.

set(gca,'Visible','off'); %消除坐標軸,顯示范圍的大小沒有改變,同上句

tmp = gca;

tmp.XAxis.Visible = 'off'; % 隱藏X軸的

% 上面一句也可以隱藏Ticks,同時Box的上端也隱藏了,這就使得圖像看上去不太好看

tmp.XTick= []; % 這一句可以只隱藏Ticks

%更多特性可參考Matlab幫助文檔,查找"Axes Properties"

%例一:同時設置subplot的多幅圖像的axis

% Create a figure with two subplots.set the axis limits for the subplots to the same values.

x1 = linspace(0,10,100);y1 = sin(x1);

ax1 = subplot(2,1,1);plot(ax1,x1,y1)

%

x2 = linspace(0,5,100);y2 = sin(x2);

ax2 = subplot(2,1,2);plot(ax2,x2,y2);

%

axis([ax1 ax2],[0 10 -1 1])

%例二:在原圖上繼續作圖,而不改變原坐標系的區間

x = linspace(0,10);y = sin(x);plot(x,y)

y2 = 2*sin(x);hold on

axis manual %關鍵步驟,凍結axis 可以對比不加該語句的結果

plot(x,y2);hold off

%例三:改變坐標系的方向(指向)

C = eye(10); pcolor(C);

colormap summer

% Reverse the coordinate system so that the y values increase from top to bottom.

axis ij; % i行,第j

% 上下兩條語句等價

set(gca,'Ydir','reverse');

% y軸默認是指向上的

實際應用:結合axis,axes,colorbar等工具,制作一個數字圖像灰度統計圖。

% 數據生成及展示(真實情況可以用數字圖像代替)

set(groot,'defaultAxesLineStyleOrder','remove','defaultAxesColorOrder','remove');

%每次使用記得清除上次設置的參數,否則設置的參數會被保留下來

x=1:255;y=rand(1,255);y=y';%y是行向量還是列向量都無所謂

n=length(x);

stem(x,y, 'Marker', 'none');

title('未設置坐標軸的區間','fontsize',14);

 

% 坐標軸區間的自動設置(適用於直方圖的顯示)

% Get x/y limits of axes using axis

hist_axes = gca;

limits = axis(hist_axes);

if n ~= 1 %當只有一個值時設置x坐標軸

limits(1) = min(x);

else

limits(1) = 0;

end

limits(2) = max(x);

var = sqrt(y'*y/length(y));

limits(4) = 2*var; % 只改變了y軸顯示的高度

axis(hist_axes,limits);

title('設置了的坐標軸區間','fontsize',14);

 

% 改變圖像的位置,寬和高,隱藏X軸的標注

% In GUIDE, default axes units are characters. In order for axes repositiong

% to behave properly, units need to be normalized.

hist_axes_units_old = get(hist_axes,'units');

set(hist_axes,'Units','Normalized');

 

% 隱藏X軸的標注

% hist_axes.XAxis.Visible = 'off';

% 上面一句也可以隱藏Ticks,同時Box的上端也隱藏了,這就使得圖像看上去不太好看

% 下面這一句可以只隱藏Ticks;

hist_axes.XTick= [];

 

% Get axis position and make room for others.

pos = get(hist_axes,'pos');

set(hist_axes,'pos',[pos(1) 0.15 pos(3) 0.75])

set(hist_axes,'Units',hist_axes_units_old); % 坐標向上移動了,相應也調整了整個圖眾向比例

title('移動了的坐標原點','fontsize',14);

 

% 設置ColorBar

c = colorbar('position',[pos(1) 0.1 pos(3) 0.05], 'location','southoutside');

c.Ticks= 0:0.125:1;

c.TickLabels = ceil((0:0.125:1)*255); % 使用floor函數,Ticks與真實值似乎有偏差

c.Box = 'off'; % 取消顯示ColorBar的框框,這樣使ColorBar的上框線看起來不粗

c.TickDirection = 'both';

colormap gray;

% colorbar最左下角點的橫坐標、縱坐標、寬度、高度

title('一個灰度統計圖的繪制就完成了','fontsize',14);


免責聲明!

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



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