使用notBoxPlot生成蜂群箱线图时如何调整标记点透明度?
I've worked with notBoxPlot before, so I know this quirk! The issue here is that the P.data objects returned by the function are line objects, and older MATLAB versions don't support the MarkerFaceAlpha property directly on line objects. Even in newer versions, sometimes you need a slightly different approach to get transparency working. Here are two reliable solutions:
Solution 1: Use AlphaData (Works for Most MATLAB Versions)
Instead of MarkerFaceAlpha, you can set the AlphaData property of the line objects. This controls the transparency of both the marker fill and edge (if you have one). Try this adjusted code:
P = notBoxPlot(random(10,10)); set([P.data],'MarkerSize',4,... 'markerFaceColor',[0.4,0.4,0.5],... 'markerEdgeColor', 'none',... 'AlphaData', 0.5); % Set transparency here (0 = fully transparent, 1 = fully opaque)
If you ever need to separate fill and edge transparency (and you're on MATLAB R2020a or newer), you can pair AlphaData with MarkerEdgeAlpha for granular control.
Solution 2: Modify the notBoxPlot Function Directly
If the first method doesn't play nice with your MATLAB version, you can tweak the function to bake in transparency support permanently:
- Open
notBoxPlot.min the MATLAB Editor. - Search for the section where data points are plotted (look for lines starting with
plot(). You'll find code similar to this:h(i) = plot(x,y,markerStyle,'Color',dataColor,'MarkerSize',markerSize); - Add the
MarkerFaceAlphaparameter to the plot call. For example:h(i) = plot(x,y,markerStyle,'Color',dataColor,'MarkerSize',markerSize,'MarkerFaceAlpha',0.5); - Save the modified function. Now every call to
notBoxPlotwill generate markers with your desired transparency. For extra flexibility, you could even add an optional input argument to let you adjust transparency on the fly!
Why MarkerFaceAlpha Didn't Work Initially
Older MATLAB releases (pre-R2014b) only supported MarkerFaceAlpha for patch objects, not line objects. Newer versions do support it for lines, but AlphaData is more universally compatible across different MATLAB versions.
内容的提问来源于stack exchange,提问作者Meow

