詳解Matlab如何繪制圓角半透明圖例
目前MATLAB的legend圖例是不支持圓角和半透明的,欸,不能咱就自己畫,就是把原始圖例隱藏后不斷追蹤其位置繪制半透明的圓角矩形嘛,這有任何難度嗎???完全沒有!!因此就有了這篇推送(目前不支持三維繪圖):
基本使用
繼續(xù)假設(shè)我們編寫了如下代碼:
t=0:0.35:3*pi; plot(t,sin(t),'Marker','d','LineWidth',2,'Color',[102,194,166]./255) hold on plot(t,cos(t./2),'Marker','o','LineWidth',2,'Color',[252,140,97]./255) plot(t,t,'Marker','^','LineWidth',2,'Color',[140,161,204]./255) lgd=legend('y=sin(t)','y=cos(t/2)','y=t'); lgd.Location='northwest'; lgd.FontSize=13; title(lgd,'Func','FontSize',14)
繪圖結(jié)果如下:
在代碼最后加上一行:
prettyLegend()
不過這樣只體現(xiàn)了圓角的性質(zhì),并沒有體現(xiàn)其半透明,要體現(xiàn)其半透明的性質(zhì)坐標區(qū)域最好不是白色的,哎我們直接和上一篇一結(jié)合不就好了(Matlab繪制酷炫坐標區(qū)域的方法詳解)
例如最后加上兩行(注意請將圖例修飾加在坐標區(qū)域修飾后面):
prettyAxes().dark2()
prettyLegend()
prettyAxes().dark()
prettyLegend()
使用說明
當(dāng)拖拽圖例到其他位置,圖例框會跟隨:
當(dāng)調(diào)整圖窗大小導(dǎo)致圖例框大小異常時,在圖例附近晃動鼠標即可修復(fù)大小:
完整代碼
function legendBox=prettyLegend(ax) if nargin<1 ax=gca; end hold on legendTitleColor=[0,0,0]; if mean(ax.Color)<0.5 legendTitleColor=[1,1,1]; ax.Legend.TextColor=[1,1,1]; end ax.Legend.AutoUpdate='on'; % ax.Legend.FontSize=11; % ax.Legend.Title.FontSize=14; ax.Legend.AutoUpdate='off'; % 如果在圖窗外則不設(shè)框 if ~isempty(regexpi(ax.Legend.Location,'out', 'once')) ax.Legend.Box='off'; lgdPos=ax.Legend.Position; % 依據(jù)legend所處figure坐標和axes范圍計算Legend坐標 xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; ax.Legend.UserData.NewBkg=[]; % 隱藏原標題 ax.Legend.Title.Visible='off'; % 繪制新legend標題 ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),[' ',ax.Legend.Title.String],... 'FontSize',ax.Legend.Title.FontSize,'VerticalAlignment','top','FontWeight','bold','Color',legendTitleColor); else ax.Legend.Box='off'; lgdPos=ax.Legend.Position; % 依據(jù)legend所處figure坐標和axes范圍計算Legend坐標 xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xDiff=(xyMax(1)-xyMin(1)); yDiff=(xyMax(2)-xyMin(2)); % 繪制圓角矩形作為新框 ax.Legend.UserData.NewBkg=rectangle(ax,'Position',[xyMin,xDiff,yDiff],'Curvature',0.2,... 'LineWidth',1.2,'EdgeColor',[0.39 0.41 0.39],'FaceColor',[1 1 1 .2]); %ax.Legend.Title.FontSize=14; % 隱藏原標題 ax.Legend.Title.Visible='off'; % 繪制新legend標題 ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),[' ',ax.Legend.Title.String],... 'FontSize',ax.Legend.Title.FontSize,'VerticalAlignment','top','FontWeight','bold','Color',legendTitleColor); end % 返回值 legendBox.Title=ax.Legend.UserData.NewTitle; legendBox.Box=ax.Legend.UserData.NewBkg; oriFunc=ax.Parent.WindowButtonMotionFcn; set(ax.Parent,'WindowButtonMotionFcn',@bt_move);% 設(shè)置鼠標移動回調(diào) function bt_move(~,~) oriFunc(); if ~isempty(regexpi(ax.Legend.Location,'out', 'once')) lgdPos=ax.Legend.Position; % 依據(jù)legend所處figure坐標和axes范圍計算Legend坐標 xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xyMin(1)=max(xyMin(1),ax.XLim(1)); xyMin(2)=max(xyMin(2),ax.YLim(1)); xyMax(1)=min(xyMax(1),ax.XLim(2)); xyMax(2)=min(xyMax(2),ax.YLim(2)); % 重設(shè)位置屬性 ax.Legend.UserData.NewTitle.Position=[xyMin(1),xyMax(2)]; else lgdPos=ax.Legend.Position; % 依據(jù)legend所處figure坐標和axes范圍計算Legend坐標 xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),... (lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)]; xyMin(1)=max(xyMin(1),ax.XLim(1)); xyMin(2)=max(xyMin(2),ax.YLim(1)); xyMax(1)=min(xyMax(1),ax.XLim(2)); xyMax(2)=min(xyMax(2),ax.YLim(2)); xDiff=(xyMax(1)-xyMin(1)); yDiff=(xyMax(2)-xyMin(2)); % 重設(shè)位置屬性 ax.Legend.UserData.NewBkg.Position=[xyMin,xDiff,yDiff]; ax.Legend.UserData.NewTitle.Position=[xyMin(1),xyMax(2)]; end end end
到此這篇關(guān)于詳解Matlab如何繪制圓角半透明圖例的文章就介紹到這了,更多相關(guān)Matlab圓角半透明圖例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項)
這篇文章主要介紹了C++實現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07使用mmap實現(xiàn)大文件的復(fù)制(單進程和多進程)
這篇文章主要為大家詳細介紹了使用mmap實現(xiàn)大文件的復(fù)制,單進程與多進程的兩種情況,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10C++11?lambda(匿名函數(shù))表達式詳細介紹
lambda 表達式(lambda expression)是一個匿名函數(shù),C++11中的lambda表達式用于定義并創(chuàng)建匿名的函數(shù)對象,以簡化編程工作,下面這篇文章主要給大家介紹了關(guān)于C++11?lambda(匿名函數(shù))表達式的相關(guān)資料,需要的朋友可以參考下2022-07-07C語言MFC導(dǎo)出dll回調(diào)函數(shù)方法詳解
這篇文章主要為大家介紹了C語言MFC導(dǎo)出dll回調(diào)函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11