Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸詳解
屬實(shí)是寫工具函數(shù)寫上癮了,又寫了一個(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ō)的一樣,編寫好代碼后在最后面引用一下工具函數(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)
后言
不管畫多少子圖,怎樣的軸方向和位置,每個(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)文章
基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11c/c++?Error:?redefinition?of?'xxx'的問(wèn)題及解決方法
兩個(gè)類/文件同時(shí)引用定義ReplyInfo的頭文件,會(huì)造成頭文件中定義重復(fù)定義,本文給大家分享c/c++?Error:?redefinition?of?‘xxx’?的問(wèn)題及解決方法,感興趣的朋友一起看看吧2023-08-08c語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)之字符串
這篇文章主要介紹了c語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)之字符串的相關(guān)資料,需要的朋友可以參考下2017-05-05C++使用遞歸和非遞歸算法實(shí)現(xiàn)的二叉樹葉子節(jié)點(diǎn)個(gè)數(shù)計(jì)算方法
這篇文章主要介紹了C++使用遞歸和非遞歸算法實(shí)現(xiàn)的二叉樹葉子節(jié)點(diǎn)個(gè)數(shù)計(jì)算方法,涉及C++二叉樹的定義、遍歷、統(tǒng)計(jì)相關(guān)操作技巧,需要的朋友可以參考下2017-05-05一文教你Qt如何操作SQLite數(shù)據(jù)庫(kù)
Sqlite 數(shù)據(jù)庫(kù)作為 Qt 項(xiàng)目開發(fā)中經(jīng)常使用的一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),可以說(shuō)是兼容性相對(duì)比較好的數(shù)據(jù)庫(kù)之一。本文為大家介紹了Qt操作SQLite數(shù)據(jù)庫(kù)的具體方法,希望對(duì)大家有所幫助2023-03-03使用VScode搭建ROS開發(fā)環(huán)境的教程詳解
這篇文章主要介紹了使用VScode搭建ROS開發(fā)環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08C++實(shí)現(xiàn)softmax函數(shù)的面試經(jīng)驗(yàn)
這篇文章主要為大家介紹了C++實(shí)現(xiàn)softmax函數(shù)的面試經(jīng)驗(yàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05