詳解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ì)坐標(biāo)區(qū)域最好不是白色的,哎我們直接和上一篇一結(jié)合不就好了(Matlab繪制酷炫坐標(biāo)區(qū)域的方法詳解)
例如最后加上兩行(注意請(qǐng)將圖例修飾加在坐標(biāo)區(qū)域修飾后面):
prettyAxes().dark2()
prettyLegend()

prettyAxes().dark()
prettyLegend()

使用說明
當(dāng)拖拽圖例到其他位置,圖例框會(huì)跟隨:

當(dāng)調(diào)整圖窗大小導(dǎo)致圖例框大小異常時(shí),在圖例附近晃動(dòng)鼠標(biāo)即可修復(fù)大?。?/p>

完整代碼
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坐標(biāo)和axes范圍計(jì)算Legend坐標(biāo)
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=[];
% 隱藏原標(biāo)題
ax.Legend.Title.Visible='off';
% 繪制新legend標(biāo)題
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坐標(biāo)和axes范圍計(jì)算Legend坐標(biāo)
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;
% 隱藏原標(biāo)題
ax.Legend.Title.Visible='off';
% 繪制新legend標(biāo)題
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è)置鼠標(biāo)移動(dòng)回調(diào)
function bt_move(~,~)
oriFunc();
if ~isempty(regexpi(ax.Legend.Location,'out', 'once'))
lgdPos=ax.Legend.Position;
% 依據(jù)legend所處figure坐標(biāo)和axes范圍計(jì)算Legend坐標(biāo)
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坐標(biāo)和axes范圍計(jì)算Legend坐標(biāo)
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Matlab實(shí)現(xiàn)有雪花飄落的圣誕樹的繪制
圣誕節(jié)快到了(雖然還有十天),一起來用MATLAB畫個(gè)簡單圣誕樹叭~代碼幾乎取消了全部的循環(huán),因此至少需要17b之后的版本,僅存的循環(huán)用來讓樹旋轉(zhuǎn)起來,讓雪花飄落起來,讓樹頂上的星光搖曳起來~感興趣的可以試一試2022-12-12
C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++利用socket傳輸大文件的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了C/C++如何使用socket傳輸大文件的實(shí)現(xiàn)代碼,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-10-10
使用mmap實(shí)現(xiàn)大文件的復(fù)制(單進(jìn)程和多進(jìn)程)
這篇文章主要為大家詳細(xì)介紹了使用mmap實(shí)現(xiàn)大文件的復(fù)制,單進(jìn)程與多進(jìn)程的兩種情況,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
C++11?lambda(匿名函數(shù))表達(dá)式詳細(xì)介紹
lambda 表達(dá)式(lambda expression)是一個(gè)匿名函數(shù),C++11中的lambda表達(dá)式用于定義并創(chuàng)建匿名的函數(shù)對(duì)象,以簡化編程工作,下面這篇文章主要給大家介紹了關(guān)于C++11?lambda(匿名函數(shù))表達(dá)式的相關(guān)資料,需要的朋友可以參考下2022-07-07
C語言MFC導(dǎo)出dll回調(diào)函數(shù)方法詳解
這篇文章主要為大家介紹了C語言MFC導(dǎo)出dll回調(diào)函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

