getAxes
类: matlab.graphics.chartcontainer.ChartContainer
命名空间: matlab.graphics.chartcontainer
语法
ax = getAxes(obj)
输入参数
obj
— 类的对象
图对象
从 matlab.graphics.chartcontainer.ChartContainer
基类继承的类的对象。
输出参量
ax
— 坐标区对象
坐标区对象 | 坐标区对象组成的数组
坐标区对象,或坐标区对象组成的数组。当您在类定义中调用绘图函数时,ax
的内容对于指定目标坐标区很有用。您还可以使用 ax
对坐标区设置属性。
根据图的内容,ax
可以是标量坐标区对象,也可以是坐标区对象组成的数组:
如果图尚未包含坐标区对象,
getAxes
会创建笛卡尔坐标区,并将其以ax
形式返回。如果图包含一个笛卡尔、极坐标或地理坐标区对象,
ax
将以该对象形式返回。如果图包含多个坐标区对象,则
ax
是一个由这些对象组成的数组。
getAxes
仅返回笛卡尔、极坐标或地理坐标区对象。它不返回作为 TiledChartLayout
的子级的其他类型的对象。
示例
将坐标区传递给绘图函数
通常通过 setup
方法来调用绘图函数和设置坐标区的保留状态。在这两种情况下,您都必须指定目标坐标区。
在类定义文件中创建 setup
方法。在该方法中,调用 getAxes
以获取坐标区对象 ax
。然后通过将 ax
作为第一个参量传递给 plot
和 hold
函数,绘制两个线条。在方法的末尾调用 hold(ax,'off')
。
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
更改坐标区属性
在您的类定义文件中定义 setup
方法。在该方法中,调用 getAxes
以获取坐标区对象 ax
。然后设置 x 轴的颜色和坐标区的字体角度。在调用任何绘图函数之前调用 hold(ax,'on')
。然后在方法的末尾调用 hold(ax,'off')
。
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
局限性
对坐标区设置
OuterPositon
、InnerPosition
、Position
或PositionConstraint
属性可能产生意外的结果。在这种情况下,请对图的实例配置位置。不推荐更改坐标区的
Parent
属性。更好的做法是,对图的实例指定Parent
属性。
版本历史记录
在 R2019b 中推出R2020a: 调用 getAxes
方法会将坐标区对象作为 TiledChartLayout
对象的子对象返回
当您在从 matlab.graphics.chartcontainer.ChartContainer
继承的图中调用 getAxes
方法时,该方法现在返回一个坐标区对象,该对象是 TiledChartLayout
对象的子对象。如果图中没有坐标区,getAxes
会创建一个笛卡尔坐标区对象。图不再有坐标区对象,除非您通过调用 getAxes
方法或一个坐标区创建函数(axes
、polaraxes
或 geoaxes
)创建一个坐标区对象。
由于这些更改,图中的坐标区可能不是当前坐标区。如果在类方法中调用以下类型的函数而不指定目标坐标区对象,代码可能会产生意外结果。
绘图函数 - 例如,
plot
、scatter
、bar
或surf
修改坐标区的函数 - 例如,
hold
、grid
或title
在 R2019b 中,坐标区对象是图对象的子对象,它是类方法的作用域内的当前坐标区。
要更新代码,请在调用绘图函数以及调用修改坐标区的函数时,将坐标区对象指定为第一个输入参量。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)