指定轴刻度和标签
Customizing the tick values and labels along an axis can help highlight particular aspects of your data. These examples show some common customizations, such as modifying the tick value placement, changing the tick label text and formatting, and rotating the tick labels.
Change Tick Value Locations and Labels
创建X作为-10到10之间的200个线性间隔值。创建Y作为X的余弦。绘制数据。
x = linspace(-10,10,200);y = cos(x);情节(x,y)
更改tick值位置X-axis andy-轴。将位置指定为增加值的向量。值不需要均匀。
另外,更改与每个刻度值相关的标签X-轴。使用字符向量的单元格数字来指定标签。要在标签中包含特殊角色或希腊字母,请使用Tex Markup,例如\ pi
for the
象征。
Xticks([ - 3*pi -2*pi -pi 0 pi 2*pi 3*pi])Xticklabels({{'-3 \ pi',,,,'-2\pi',,,,' - \ pi',,,,'0',,,,'\ pi',,,,'2\pi',,,,'3\pi'})yticks([ - 1 -0.8 -0.2 0 0.2 0.8 1])
For releases prior to R2016b, instead set the tick values and labels using theXTICK
,,,,Xticklabel
,,,,ytick
,,,,andytickLabel
属性轴
目的。For example, assign the轴
反对变量,例如AX = GCA
。Then set theXTICK
使用点表示法,例如ax.xtick = [-3*pi -2*pi -pi 0 pi 2*pi 3*pi]
。对于R2014B之前的发行版,请使用放
函数以设置属性。
旋转刻度标签
创建一个散点图并沿每个轴旋转刻度标签。将旋转指定为标量值。正值表示逆时针旋转。负值表示顺时针旋转。
x = 1000*rand(40,1);y = rand(40,1);散射(x,y)Xtickangle(45)ytickangle(90)
For releases prior to R2016b, specify the rotation using theXTicklabelRotation
andyticklabelrotation
属性轴
目的。For example, assign the轴
反对变量,例如AX = GCA
。Then set theXTicklabelRotation
使用点表示法,例如ax.XTickLabelRotation = 45
。
更改刻度标签格式
Create a stem chart and display the tick label values along they-axis as US dollar values.
利润= [20 40 50 40 50 60 70 60 70 60 60 70 80 90];STEM(利润)xlim([0 15])ytickformat('美元')
For more control over the formatting, specify a custom format. For example, show one decimal value in theX- 使用'%.1f'
。显示y- 轴tick标签作为英镑的使用'\ xa3%.2F'
。选项\ xa3
指示磅符号的Unicode字符。有关指定自定义格式的更多信息,请参见XtickFormat
功能。
xtickformat('%.1f')ytickformat('\ xa3%.2F')
单个轴控制的标尺对象
MATLABcreates a ruler object for each axis. Like all graphics objects, ruler objects have properties that you can view and modify. Ruler objects allow for more individual control over the formatting of theX-轴,y- 轴,或z-轴。Access the ruler object associated with a particular axis through thexaxis
,,,,Yaxis
, 或者Zaxis
property of the轴
目的。标尺的类型取决于沿轴的数据类型。对于数字数据,MATLAB创建一个数字器
目的。
ax = gca;ax.xaxis
ans =具有属性的数字:限制:[0 15]比例:'线性'指数:0 tickvalues:[0 5 10 15] ticklabelformat:'%.1f'显示所有属性
使用标尺对象在指数标签中控制值
与yvalues that range between -15,000 and 15,000. By default, they- 轴tick标签使用指数符号,指数为4,基础为10。将指数值更改为2。设置Exponent
与y-轴。Access the ruler object through theYaxis
property of the轴
目的。The exponent label and the tick labels change accordingly.
x = linspace(0,5,1000);y = 100*exp(x)。*sin(20*x);图(x,y)ax = gca;ax.yaxis.exponent = 2;
Change the exponent value to 0 so that the tick labels do not use exponential notation.
ax.yaxis.exponent = 0;