線條和散點圖中的半透明標記

Matlab R2014b 開始,使用 Yair Altman 引入的無證特徵 很容易實現線條和散點圖的半透明標記。

基本思想是獲取標記的隱藏控制代碼,併為 EdgeColorData 中的最後一個值應用值 <1 ,以實現所需的透明度。

這裡我們去看 scatter

%// example data
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);

%// plot scatter, get handle
h = scatter(x,y); 
drawnow; %// important

%// get marker handle
hMarkers = h.MarkerHandle; 

%// get current edge and face color
edgeColor = hMarkers.EdgeColorData
faceColor = hMarkers.FaceColorData

%// set face color to the same as edge color
faceColor = edgeColor;

%// opacity
opa = 0.3;

%// set marker edge and face color
hMarkers.EdgeColorData = uint8( [edgeColor(1:3); 255*opa] ); 
hMarkers.FaceColorData = uint8( [faceColor(1:3); 255*opa] ); 

StackOverflow 文件

和一條線 plot

%// example data
x = linspace(0,3*pi,200);
y1 = cos(x);
y2 = sin(x);

%// plot scatter, get handle
h1 = plot(x,y1,'o-','MarkerSize',15); hold on
h2 = plot(x,y2,'o-','MarkerSize',15); 
drawnow; %// important

%// get marker handle
h1Markers = h1.MarkerHandle; 
h2Markers = h2.MarkerHandle; 

%// get current edge and face color
edgeColor1 = h1Markers.EdgeColorData;
edgeColor2 = h2Markers.EdgeColorData;

%// set face color to the same as edge color
faceColor1 = edgeColor1;
faceColor2 = edgeColor2;

%// opacity
opa = 0.3;

%// set marker edge and face color
h1Markers.EdgeColorData = uint8( [edgeColor1(1:3); 255*opa] ); 
h1Markers.FaceColorData = uint8( [faceColor1(1:3); 255*opa] ); 
h2Markers.EdgeColorData = uint8( [edgeColor2(1:3); 255*opa] ); 
h2Markers.FaceColorData = uint8( [faceColor2(1:3); 255*opa] ); 

StackOverflow 文件

用於操作的標記手柄是用圖形建立的。該 drawnow 命令確保圖的創作被稱為和延誤的情況下避免錯誤的後續命令之前。