In the past two weeks I explained how HG2 (in R2014b) enables us to customize the axes rulers, baselines, box and grid-lines in ways that were previously impossible in HG1 (R2014a or earlier). Today I will describe another useful undocumented property of the HG2 axes – Backdrop.
The axes’ Backdrop property (a
matlab.graphics.axis.decorator.Backdrop object) controls the background color of the axes’ content area. This is normally white, but can be set to any color, including translucency:
% Create the plot x = -10:0.1:10; y = 1e7*sin(x)./x; hLine = plot(x,y); hAxes = gca; box('off'); % Set a light-yellow backdrop hAxes.Backdrop.FaceColor = [1, 1, 0, 0.4];
By default, Backdrop spans the entire (rectangular) axes content area. But we can customize its vertex points using the Face.VertexData property, which accepts an array of 3xN singles:
% Note: VertexData is specified in normalized (0-1) units, not data units hAxes.Backdrop.Face.VertexData = single([0,1,0.8,0; 0,0.43,0.75,1; 0,0,0,0]); % default: [0,1,1,0; 0,0,1,1; 0,0,0,0] >> hAxes.Backdrop.Face.get AmbientStrength: 0.3 BackFaceCulling: 'none' ColorBinding: 'object' ColorData: [4x1 uint8] ColorType: 'truecoloralpha' DiffuseStrength: 0.6 HandleVisibility: 'off' HitTest: 'off' Layer: 'back' NormalBinding: 'none' NormalData:  Parent: [1x1 Backdrop] PickableParts: 'all' SpecularColorReflectance: 1 SpecularExponent: 10 SpecularStrength: 0.9 StripData:  Texture:  TwoSidedLighting: 'off' VertexData: [3x4 single] VertexIndices:  Visible: 'on'
The down side is that whenever we modify anything in the axes (or even get some axes property values), Face.VertexData is reset back to its default value of [0,1,1,0; 0,0,1,1; 0,0,0,0], so we need to either update it in a timer and/or in a property listener.
We can create a tint (gradient color) effect by setting the Face.ColorData property to match the vertex points and modifying Face.ColorBinding from the default ‘object’ to ‘interpolated’ (other alternative values are ‘discrete’ and ‘none’):
colorData = uint8([255, 150, 200, 100; ... % instead of [255;255;0;102] = light-yellow 255, 100, 50, 200; ... 0, 50, 100, 150; ... 102, 150, 200, 50]); set(hAxes.Backdrop.Face, 'ColorBinding','interpolated', 'ColorData',colorData);
The tint effect does not rely on the vertex – we can set a tinted background for the entire content area by modifying Face.ColorData without modifying the vertexes:
Using a patch
Of course, one could say that using Backdrop is not much better than creating a patch that has the specified vertex points and colors. For example (note that Backdrop has different color and vertex units than a patch):
% Create a patch to replace the backdrop cdata = [255, 150, 200, 100; ... 255, 100, 50, 200; ... 0, 50, 100, 150 ... ]' / 255; % alpha values are not directly accepted by patch CData vertexData = [0,1,0.8,0; 0,0.43,0.75,1]; % in normalized units xdata = hAxes.XLim(1) + diff(hAxes.XLim)*vertexData(1,:); % in data units ydata = hAxes.YLim(1) + diff(hAxes.YLim)*vertexData(2,:); % in data units hPatch = patch(xdata, ydata, 'k', 'Parent',hAxes, 'EdgeColor','none', ... 'FaceVertexCData',cdata, 'FaceColor','interp', ... 'FaceVertexAlpha',[102;150;200;50]/255, 'FaceAlpha','interp');
This results in a gradient very similar to the Backdrop, although not exactly the same. Perhaps the color interpolation for Backdrop is different than for patches, or maybe I just made a mistake somewhere above…
I don’t have a good answer as to why MathWorks chose to add the Backdrop property to HG2 axes and what advantage it might have over a separate patch. If anyone has an idea, please post a comment below. We can be pretty sure that this was discussed at MathWorks, since Michelle Hirsch, who heads Matlab product development at MathWorks, posted a utility that implements a patch-based gradient backdrop back in 2010 and has recently rehosted it on GitHub:
Next week, I will conclude the mini-series on axes customizations by describing some additional undocumented axes features.