matlab.graphics.chartcontainer.ChartContainer.getAxes - Get axes for chart container subclass - MATLAB (original) (raw)
Class: matlab.graphics.chartcontainer.ChartContainer
Namespace: matlab.graphics.chartcontainer
Get axes for chart container subclass
Description
[ax](#mw%5Fa459e303-32a3-4760-aa01-ecd19b5729bb) = getAxes([obj](#mw%5F799f0810-2d11-4c83-8b74-ade1e0ff2049))
returns one or more axes objects for a chart that inherits from thematlab.graphics.chartcontainer.ChartContainer
base class.
Input Arguments
Object of the class that inherits from thematlab.graphics.chartcontainer.ChartContainer
base class.
Output Arguments
Axes object, or an array of axes objects. The contents of ax
is useful for specifying the target axes when you call plotting functions within your class definition. You can also use ax
to set properties on the axes.
Depending on the contents of the chart, ax
might be a scalar axes object or an array of axes objects:
- If the chart does not already contain an axes object,
getAxes
creates a Cartesian axes and returns it asax
. - If the chart contains one Cartesian, polar, or geographic axes object,
ax
is returned as that object. - If the chart contains multiple axes objects,
ax
is an array of those objects.
getAxes
returns only Cartesian, polar, or geographic axes objects. It does not return other types of objects that are descendents of theTiledChartLayout
.
Examples
The setup
method is a common place to call plotting functions and set the axes hold state. In both cases, you must specify the target axes.
Create a setup
method in your class definition file. Within that method, call getAxes
to get the axes object ax
. Then plot two lines by passing ax
as the first argument to theplot
and hold
functions. Callhold(ax,'off')
at the end of the method.
classdef TwoLinesPlot < matlab.graphics.chartcontainer.ChartContainer
properties
% ...
end
methods(Access = protected)
function setup(obj)
% Get the axes
ax = getAxes(obj);
% Plot two lines in the axes
line1 = plot(ax,[1 2 3 4 5],[3 5 1 4 9]);
hold(ax,'on')
line2 = plot(ax,[1 2 3 4 5],[30 52 21 9 18]);
% Turn off hold state
hold(ax,'off')
end
function update(obj)
% ...
end
end
end
Define a setup
method in your class definition file. Within that method, call getAxes
to get the axes object ax
. Then set the _x_-axis color and the font angle for the axes. Callhold(ax,'on')
before calling any plotting functions. Then callhold(ax,'off')
at the end of the method.
classdef RedAxisPlot < matlab.graphics.chartcontainer.ChartContainer
properties
% ...
end
methods(Access = protected)
function setup(obj)
ax = getAxes(obj);
ax.XColor = [1 0 0];
ax.FontAngle = 'italic';
hold(ax,'on')
% Call plotting functions
% ...
hold(ax,'off')
end
function update(obj)
% ...
end
end
end
Limitations
- Setting the
OuterPositon
,InnerPosition
,Position
, orPositionConstraint
properties on the axes might produce unexpected results. Instead, configure the position on an instance of your chart. - Changing the
Parent
property of the axes is not recommended. Instead, specify theParent
property on an instance of your chart.
Version History
Introduced in R2019b
When you call the getAxes
method in a chart that inherits frommatlab.graphics.chartcontainer.ChartContainer
, the method now returns an axes object that is a child of a TiledChartLayout
object. If there are no axes in the chart, getAxes
creates a Cartesian axes object. The chart no longer has an axes object until you create one by calling the getAxes
method or one of the axes creation functions: axes
,polaraxes
, or geoaxes
.
As a consequence of these changes, the axes in your chart might not be the current axes. Your code might produce unexpected results if you call the following types of functions within your class methods without specifying the target axes object.
- Plotting functions — For example,
plot
,scatter
,bar
, orsurf
- Functions that modify the axes — For example,
hold
,grid
, ortitle
In R2019b, the axes object is a child of the chart object, and it is the current axes within the scope of your class methods.
To update your code, specify the axes object as the first input argument when calling plotting functions and functions that modify the axes.