Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸詳解
屬實(shí)是寫(xiě)工具函數(shù)寫(xiě)上癮了,又寫(xiě)了一個(gè)一行代碼將坐標(biāo)軸變?yōu)榧^坐標(biāo)軸的函數(shù),而且可以對(duì)其進(jìn)行隨意拖動(dòng)和縮放(拖動(dòng)需要先點(diǎn)擊那個(gè)像手掌的符號(hào)):

功能函數(shù)的引用非常簡(jiǎn)單,就只是在最后面加上一行:
arrowAxes()或者arrowAxes(ax)
即可,以下給出幾個(gè)例子:
demo1 基礎(chǔ)使用
就像上面說(shuō)的一樣,編寫(xiě)好代碼后在最后面引用一下工具函數(shù)即可:
% arrow axes demo1 % @author:slandarer t=-pi:.2:2*pi; stem(t,sin(t),'LineWidth',1.5); arrowAxes()

demo2 軸方向
可以調(diào)整坐標(biāo)區(qū)域的XAxisLocation屬性及YAxisLocation屬性來(lái)調(diào)整坐標(biāo)軸的方向和位置,總共有九種組合,篇幅問(wèn)題這里不一一列舉
這里只列舉常用的四種:
% arrow axes demo2 % @author:slandarer t=-pi:.2:2*pi; % 子圖1 ax1=subplot(2,2,1); plot(t,sin(t),'LineWidth',1.5); arrowAxes(ax1) % 子圖2 ax2=subplot(2,2,2); plot(t,sin(t),'LineWidth',1.5); ax2.XAxisLocation='top'; ax2.YAxisLocation='right'; arrowAxes(ax2) % 子圖3 ax3=subplot(2,2,3); plot(t,sin(t),'LineWidth',1.5); ax3.XAxisLocation='origin'; arrowAxes(ax3) % 子圖4 ax4=subplot(2,2,4); plot(t,sin(t),'LineWidth',1.5); ax4.XAxisLocation='origin'; ax4.YAxisLocation='origin'; arrowAxes(ax4)

demo3 軸的其他屬性
在引用工具函數(shù)前調(diào)整坐標(biāo)軸的粗細(xì)和顏色,引用工具函數(shù)時(shí),工具函數(shù)會(huì)自動(dòng)提取坐標(biāo)軸的屬性并賦予箭頭坐標(biāo)軸:
% arrow axes demo3 % @author:slandarer t=-pi:.2:2*pi; stem(t,sin(t),'LineWidth',1.5); % 修改坐標(biāo)軸屬性 ax=gca; ax.XColor=[1,0,0]; ax.LineWidth=2; ax.XAxisLocation='origin'; arrowAxes(ax)

后言
不管畫(huà)多少子圖,怎樣的軸方向和位置,每個(gè)子圖都能像如下這樣任意調(diào)整坐標(biāo)范圍和圖像縮放。


工具函數(shù)完整代碼
function arrowAxes(ax)
%
% @author: slandarer
% @公眾號(hào): slandarer隨筆
% @知乎 : hikari
% @CSDN : slandarer
%
% 期待您的關(guān)注!!!
help arrowAxes % 若不希望輸出[作者信息],請(qǐng)刪除這行
if nargin<1
ax=gca;
end
ax.Box='off';
ax.UserData.arrow{1}=[];
ax.UserData.arrow{2}=[];
ax.UserData.arrow{3}=[];
ax.UserData.arrow{4}=[];
pos=ax.Position;
xm=.02;
ym=.02;
% -------------------------------------------------------------------------
switch ax.XAxisLocation
case 'bottom'
ax.UserData.arrow{2}=annotation('arrow');
ax.UserData.arrow{2}.Color=ax.YColor;
ax.UserData.arrow{2}.Position=[pos(1),pos(2),0,pos(4)+ym];
case 'top'
ax.UserData.arrow{4}=annotation('arrow');
ax.UserData.arrow{4}.Color=ax.YColor;
ax.UserData.arrow{4}.Position=[pos(1),pos(2)+pos(4),0,-pos(4)-ym];
case 'origin'
ax.UserData.arrow{2}=annotation('arrow');
ax.UserData.arrow{2}.Color=ax.YColor;
ax.UserData.arrow{2}.Position=[pos(1),pos(2),0,pos(4)+ym];
ax.UserData.arrow{4}=annotation('arrow');
ax.UserData.arrow{4}.Color=ax.YColor;
ax.UserData.arrow{4}.Position=[pos(1),pos(2)+pos(4),0,-pos(4)-ym];
end
switch ax.YAxisLocation
case 'left'
ax.UserData.arrow{1}=annotation('arrow');
ax.UserData.arrow{1}.Color=ax.XColor;
ax.UserData.arrow{1}.Position=[pos(1),pos(2),pos(3)+xm,0];
case 'right'
ax.UserData.arrow{3}=annotation('arrow');
ax.UserData.arrow{3}.Color=ax.XColor;
ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2),-pos(3)-xm,0];
case 'origin'
ax.UserData.arrow{1}=annotation('arrow');
ax.UserData.arrow{1}.Color=ax.XColor;
ax.UserData.arrow{1}.Position=[pos(1),pos(2),pos(3)+xm,0];
ax.UserData.arrow{3}=annotation('arrow');
ax.UserData.arrow{3}.Color=ax.XColor;
ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2),-pos(3)-xm,0];
end
if strcmp(ax.XAxisLocation,'top')
if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2)+pos(4),pos(3)+xm,0];end
if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2)+pos(4),-pos(3)-xm,0];end
end
if strcmp(ax.YAxisLocation,'right')
if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1)+pos(3),pos(2),0,pos(4)+ym];end
if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1)+pos(3),pos(2)+pos(4),0,-pos(4)-ym];end
end
for i=1:4
if ~isempty(ax.UserData.arrow{i}),ax.UserData.arrow{i}.LineWidth=ax.LineWidth;end
end
reArrow()
% -------------------------------------------------------------------------
function reArrow(~,~)
if strcmp(ax.XAxisLocation,'origin')
pos=ax.Position;
ylim=ax.YLim;
sepy=(0-ylim(1))./(ylim(2)-ylim(1)).*pos(4);
switch true
case ylim(2)<=0
if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2)+pos(4),pos(3)+xm,0];end
if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2)+pos(4),-pos(3)-xm,0];end
case ylim(1)>=0
if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2),pos(3)+xm,0];end
if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2),-pos(3)-xm,0];end
case ylim(2)>0&ylim(1)<0
if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2)+sepy,pos(3)+xm,0];end
if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2)+sepy,-pos(3)-xm,0];end
end
end
if strcmp(ax.YAxisLocation,'origin')
pos=ax.Position;
xlim=ax.XLim;
sepx=(0-xlim(1))./(xlim(2)-xlim(1)).*pos(3);
switch true
case xlim(2)<=0
if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1)+pos(3),pos(2),0,pos(4)+ym];end
if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1)+pos(3),pos(2)+pos(4),0,-pos(4)-ym];end
case xlim(1)>=0
if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1),pos(2),0,pos(4)+ym];end
if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1),pos(2)+pos(4),0,-pos(4)-ym];end
case xlim(2)>0&xlim(1)<0
if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1)+sepx,pos(2),0,pos(4)+ym];end
if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1)+sepx,pos(2)+pos(4),0,-pos(4)-ym];end
end
end
end
set(ax.Parent,'WindowButtonMotionFcn',@reArrow); % 設(shè)置鼠標(biāo)按下回調(diào)
end到此這篇關(guān)于Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸詳解的文章就介紹到這了,更多相關(guān)Matlab箭頭坐標(biāo)軸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析,接上一篇內(nèi)容,本文著重分析源碼文件,需要的朋友可以參考下2014-09-09
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易文本編譯器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易文本編譯器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
C語(yǔ)言完整實(shí)現(xiàn)12種排序算法(小結(jié))
本文主要介紹了C語(yǔ)言完整實(shí)現(xiàn)12種排序算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
c++標(biāo)準(zhǔn)庫(kù)讀寫(xiě)ini文件的實(shí)現(xiàn)示例
本文介紹了一個(gè)完整的INI文件類(lèi)的實(shí)現(xiàn),包含讀取和寫(xiě)入操作,通過(guò)IniFile.h頭文件和IniFile.cpp實(shí)現(xiàn)文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
用位圖排序無(wú)重復(fù)數(shù)據(jù)集實(shí)例代碼(C++版)
本文講解如何用位圖排序無(wú)重復(fù)的數(shù)據(jù)集,我們使用C++實(shí)現(xiàn)一下這個(gè)方法2013-11-11
C++詳細(xì)講解函數(shù)調(diào)用與Struct和CLass的區(qū)別
主調(diào)函數(shù)使用被調(diào)函數(shù)的功能,稱(chēng)為函數(shù)調(diào)用。在C語(yǔ)言/C++中,只有在函數(shù)調(diào)用時(shí),函數(shù)體中定義的功能才會(huì)被執(zhí)行,下面讓我們?cè)敿?xì)來(lái)了解2022-05-05

