线条和散点图中的半透明标记

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 命令确保图的创作被称为和延误的情况下避免错误的后续命令之前。