Last week I explained how to customize plot-lines with transparency and color gradient. Today I wish to show how we can achieve similar effects with plot markers. Note that this discussion (like the preceding several posts) deal exclusively with HG2, Matlab’s new graphics system starting with R2014b (well yes, we can also turn HG2 on in earlier releases).
As Paul has noted in a comment last week, we cannot simply set a 4th (alpha transparency) element to the MarkerFaceColor and MarkerEdgeColor properties:
>> x=1:10; y=10*x; hLine=plot(x,y,'o-'); drawnow; >> hLine.MarkerFaceColor = [0.5,0.5,0.5]; % This is ok >> hLine.MarkerFaceColor = [0.5,0.5,0.5,0.3]; % Not ok While setting the 'MarkerFaceColor' property of Line: Color value must be a 3 element numeric vector
Lost cause? – not in a long shot. We simply need to be a bit more persuasive, using the hidden MarkerHandle property:
>> hMarkers = hLine.MarkerHandle; % a matlab.graphics.primitive.world.Marker object >> hMarkers.get EdgeColorBinding: 'object' EdgeColorData: [4x1 uint8] EdgeColorType: 'truecolor' FaceColorBinding: 'object' FaceColorData: [4x1 uint8] FaceColorType: 'truecolor' HandleVisibility: 'off' HitTest: 'off' Layer: 'middle' LineWidth: 0.5 Parent: [1x1 Line] PickableParts: 'visible' Size: 6 Style: 'circle' VertexData: [3x10 single] VertexIndices:  Visible: 'on' >> hMarkers.EdgeColorData' % 4-element uint8 array ans = 0 114 189 255 >> hMarkers.FaceColorData' % 4-element uint8 array ans = 128 128 128 255
As we can see, we can separately attach transparency values to the marker’s edges and/or faces. For example:
hMarkers.FaceColorData = uint8(255*[1;0;0;0.3]); % Alpha=0.3 => 70% transparent red
And as we have seen last week, we can also apply color gradient across the markers, by modifying the EdgeColorBinding/FaceColorBinding from ‘object’ to ‘interpolated’ (there are also ‘discrete’ and ‘none’), along with changing the corresponding FaceColorData/EdgeColorData from being a 4×1 array to a 4xN array:
>> colorData = uint8([210:5:255; 0:28:252; [0:10:50,40:-10:10]; 200:-10:110]) colorData = 210 215 220 225 230 235 240 245 250 255 0 28 56 84 112 140 168 196 224 252 0 10 20 30 40 50 40 30 20 10 200 190 180 170 160 150 140 130 120 110 >> set(hMarkers,'FaceColorBinding','interpolated', 'FaceColorData',colorData)
This can be useful for plotting comet trails, radar/sonar tracks, travel trajectories, etc. We can also use it to overlay meta-data information, such as buy/sell indications on a financial time-series plot. In fact, it opens up Matlab plots to a whole new spectrum of customizations that were more difficult (although not impossible) to achieve earlier.
Throughout today, we’ve kept the default FaceColorType/EdgeColorType value of ‘truecolor’ (which is really the same as ‘truecoloralpha’ as far as I can tell, since both accept an alpha transparency value as the 4th color element). If you’re into experimentation, you might also try ‘colormapped’ and ‘texturemapped’.
p.s. – other chart types have similar internal objects that can be customized. For example, bar charts have internal Face and Edge properties that correspond to internal objects that can be similarly modified:
>> get(h.Edge) AlignVertexCenters: 'on' AmbientStrength: 0.3 ColorBinding: 'object' ColorData: [4×1 uint8] ColorType: 'truecolor' DiffuseStrength: 0.6 HandleVisibility: 'on' HitTest: 'off' Layer: 'middle' LineJoin: 'miter' LineStyle: 'solid' LineWidth: 0.5 NormalBinding: 'none' NormalData:  Parent: [1×1 Bar] ...