Matlab adjust axis tick labels, limits, and tick locations

2023-05-16

From: https://cn.mathworks.com/matlabcentral/answers/92565-how-do-i-control-axis-tick-labels-limits-and-axes-tick-locations

The following example can be used as a resource and guide for adjusting axis tick labels, limits, and tick locations.

If you are plotting earnings versus time, consider labeling your X-tick marks with weeks, months, or even years. Below is a list of properties that control the X-tick locations and labels. These are properties of axes objects.

 


 XLim
 XLimMode                       [{auto}|manual]
 XTick
 XTickMode                      [{auto}|manual]
 XTickLabel   

NOTE: The Y and Z axes have similar properties that start with Y or Z respectively.

The 'XLim' property is used to store the limits of the X-axis. By default, MATLAB chooses the limits; however, you can specify the limits using "axis([xmin xmax ymin ymax zmin zmax])" or "set(gca,XLim,[xmin xmax])". Setting this property will automatically change the 'XLimMode' to 'manual'.

'XLimMode' is used to determine if MATLAB is controlling the X-limits or if you are controlling them. By default, it is set to 'auto', which implies that MATLAB chooses the limits. To prevent MATLAB from changing the limits when the figure is resized or printed, set this property to 'manual'. If you set the 'XLim' property, or use the AXIS command, 'XLimMode' is automatically set to 'manual'.

'XTick' is the property in which MATLAB stores the location of the X-tick marks. Generally, this property is used by MATLAB; however, you can set this property so that only the desired tick marks are drawn. Setting this property automatically changes the 'XTickMode' property to manual.

'XTickMode' is used to determine whether MATLAB or you control the tick locations. By default, it is set to 'auto', which implies that MATLAB controls the locations of the tick marks. To prevent MATLAB from changing the tick locations or number of ticks when the figure is resized or printed, change this property to 'manual'. If 'XTick' is set by you, this property is automatically set to 'manual'.

'XTickLabel' is the property in which MATLAB stores the strings used to label the tick marks. Normally, this property contains the string representation of the 'XTick' property. For example, if 'XTick' contains the vector [2 4 6 8], then 'XTickLabel' contains the following string array:

 


 2
 4
 6
 8  

Usually, the number of rows in 'XTickLabel' is equal to the number of tick marks. If this is not true, then MATLAB will cycle through the X-tick labels to label each of the tick marks. For example, if the previous string array only contained the first two rows, the ticks along the X-axis would be labelled 2--4--2--4.

'XTickLabel' can be a cell array of strings, or a string array. If you use a string array, you must pay attention to the fact that every row must contain the same number of columns. So, if you change the 'XTickLabel' to contain 1 and 100, you will have to pad the 1 with spaces. For example:

 


set(gca,'XTickLabel',['1  ';'100'])
  

% Alternatively, use a cell array of strings:
set(gca,'XTickLabel',{'1','100'})
  

Now that we have established the properties required to change the tick location and labels, let's work through an example:

 


% Generate a random vector of earning that can range from
% $0 - $2000.  Each element of earnings correspond to the
% earnings for a given month.
earnings = 1000 + 1000*rand(1,12) - 1000*rand(1,12);
  

% Generate a bar plot of the earnings with 12 bins 
% (1 bin per month)
bar(earnings);
  

% Set the XLim so that it ranges from .5 - 12.5.  
% The reason that it starts at .5 and ends at 12.5 is
% because each bin is .5 data units wide.
set(gca,'XLim',[.5 12.5]);% This automatically sets the
                          % XLimMode to manual.
  

% Set XTick so that only the integer values that 
% range from 0.5 - 12.5 are used.
set(gca,'XTick',[1:12])  % This automatically sets 
                         % the XTickMode to manual.
  

% Set the XTickLabel so that abbreviations for the
% months are used.
months = ['Jan';
          'Feb';
          'Mar';
          'Apr';
          'May';
          'Jun';
          'Jul';
          'Aug';
          'Sep';
          'Oct';
          'Nov';
          'Dec'];
set(gca,'XTickLabel',months)
  

Remember, the Y- and Z-axes both have properties similar to the ones described in this solution.

Another thing you may want to do is have fewer tick labels than tick marks. The following code demonstrates how to do this:

 


plot(0:4,0:4)
set(gca,'XLim',[0 4])
set(gca,'XTick',[0:0.5:4])
set(gca,'XTickLabel',['0';' ';'1';' ';'2';' ';'3';' ';'4'])
  

---------------------------------EDIT ------------------------------

New helper functions have been introduced in MATLAB R2016b to control the following properties -

 

1. xlim - Set or query x-axis limits

2. xtickformat - Specify x-axis tick label format

3. xticklabels - Set or query x-axis tick labels

4. xticks - Set or query x-axis tick values

5. xtickangle - Rotate x-axis tick labels

 

The same example as above using the latest helper functions - 

 


% Generate a random vector of earning that can range from
% $0 - $2000.  Each element of earnings correspond to the
% earnings for a given month.
earnings = 1000 + 1000*rand(1,12) - 1000*rand(1,12);
  

% Generate a bar plot of the earnings with 12 bins
% (1 bin per month)
bar(earnings);
  

% Set the XLim so that it ranges from .5 - 12.5. 
% The reason that it starts at .5 and ends at 12.5 is
% because each bin is .5 data units wide.                           
xlim([.5 12.5]); % This automatically sets the
                            % XLimMode to manual.
  

% Set XTick so that only the integer values that
% range from 0.5 - 12.5 are used.
xticks(1:12);  % This automatically sets
                           % the XTickMode to manual.
  

% Set the xticklabels so that abbreviations for the
% months are used.
months = ['Jan';
            'Feb';
            'Mar';
            'Apr';
            'May';
            'Jun';
            'Jul';
            'Aug';
            'Sep';
            'Oct';
            'Nov';
            'Dec'];
xticklabels(months);
  

% Set the xtickangle to rotate the x-axis tick lables
xtickangle(45);
  

The second example to have fewer tick labels than tick marks can be re-written as
  

​plot(0:4,0:4)
xlim([0 4])
xticks([0:0.5:4])
xticklabels(['0';' ';'1';' ';'2';' ';'3';' ';'4'])  

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matlab adjust axis tick labels, limits, and tick locations 的相关文章

  • 在 matlab/octave 中将数据集分成两个子集 [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 将数据集分为两个子集 例如 训练 和 测试 其中 训练集包含 80 的数据 测试集包含剩余的 20 分裂的意思是生成一个长度等于的逻辑索引
  • 查找数组中元素之间的平均差异的有效方法

    希望标题不会让人困惑 通过例子来展示很简单 我有一个像这样的行向量 1 5 6 我想找到每个元素之间的平均差异 此示例中的差异为 4 和 1 因此平均值为 2 5 这是一个小例子 我的行向量可能非常大 我是 MatLab 新手 那么有没有一
  • 从soap标头中删除mustUnderstand属性

    如何从轴客户端中的soap标头中删除mustunderstand属性 即使我没有特别设置它 当我设置soap标头信息mustundertand时 actor属性会自动添加到soap消息中 有人知道如何删除它们吗 我正在使用 Axis2 1
  • 是否有一个函数可以检查矩阵是否对角占优(行占优)

    矩阵是对角占优 http en wikipedia org wiki Diagonally dominant matrix 按行 如果对角线处的值在绝对意义上大于该行中所有其他绝对值的总和 对于列也是如此 只是相反 matlab中有没有函数
  • 如何从绘图处理程序中绘图?

    我有绘图的处理程序或图形的处理程序 例子 h plot 1 0 2 10 xx get h xx DisplayName Annotation 1x1 handle Color 0 0 1 LineStyle LineWidth 0 500
  • 为什么matlab的mldivide比dgels好这么多?

    Solve Ax b 真正的双 A是超定的 Mx2 其中 M gt gt 2 b是MX1 我运行了大量的数据mldivide 并且结果非常好 我用 MKL 写了一个 mex 例程LAPACKE dgels但它远没有那么好 结果有大量噪音 并
  • 在 Matlab 中显示有理数

    我有两个整数 m n 它们一起形成 m n 形式的有理数 现在我只想以这种理性的形式在 Matlab 中显示它们 我可以通过这样做来做到这一点 char sym m n 所以 如果 例如m 1 n 2 Matlab将显示1 2 然而 如果m
  • 在 MATLAB 中创建共享库

    一位研究人员在 MATLAB 中创建了一个小型仿真 我们希望其他人也能使用它 我的计划是进行模拟 清理一些东西并将其变成一组函数 然后我打算将其编译成C库并使用SWIG https en wikipedia org wiki SWIG创建一
  • 优化 MATLAB 代码(嵌套 for 循环计算相似度矩阵)

    我正在 MATLAB 中基于欧几里德距离计算相似度矩阵 我的代码如下 for i 1 N M N is the size of the matrix x for whose elements I am computing similarit
  • 归一化互相关的基础知识

    我正在尝试使用范数校正2 归一化互相关 http en wikipedia org wiki Cross correlation Normalized cross correlation 来自 MATLAB 用于计算发育中胚胎中移动形状的速
  • 检查图像中是否有太薄的区域

    我正在尝试验证雕刻机的黑白图像 更多的是剪贴画图像 不是照片 我需要考虑的主要事情之一是区域的大小 或线条的宽度 因为机器无法处理太细的线条 所以我需要找到比给定阈值更细的区域 以此图为例 竖琴的琴弦可能太细而无法雕刻 我正在阅读有关 Ma
  • 从筛查乳腺 X 光检查数字数据库 (DDSM) 获取数据

    我正在尝试以可读格式获取 DDSM 数据集 有谁有 DDSM heathusf 程序的工作版本 可以在 Linux 或 Windows 上正常运行吗 我知道 DDSM 的 jpeg 程序有一个适用于 linux 的工作版本 位于http w
  • 将数据提示堆栈放在轴标签顶部,并在轴位置发生更改后更新轴标签

    此问题仅适用于 unix matlab Windows 用户将无法重现该问题 我在尝试创建位于 y 轴标签顶部的数据提示时遇到问题 下图很能说明问题 正如您所看到的 在 ylabel 附近创建的数据提示将到达 ylabel 文本的底部 而期
  • 动态调整自定义刻度数

    Taking SO 的一个例子 https stackoverflow com a 7139485 97160 我想根据当前视图调整轴刻度 这是默认行为 除非设置自定义的刻度数 下图展示了由此产生的行为 左侧是默认行为 右侧是带有自定义刻度
  • 如何在Matlab中绘制网络?

    我有一个矩阵AMatlab中的维数mx2每行包含两个节点的标签 显示网络中的直接链接 例如 如果网络有4矩阵的节点A可能A 1 2 1 3 2 1 2 4 3 2 4 1 4 2 其中第一行表示有一个链接来自1 to 2 第二行表示有一个链
  • 如何在向量中的所有点之间绘制线?

    我有一个包含二维空间中一些点的向量 我希望 MATLAB 用从每个点到每个其他点绘制的线来绘制这些点 基本上 我想要一个所有顶点都连接的图 你能用情节来做到这一点吗 如果可以 怎么做 一种解决方案是使用该函数为每个点组合创建一组索引MESH
  • 在 matlab 代码中使用 dll 文件

    我需要使用 Matlab 中由 dll 文件定义的函数 我有一个例子 那个家伙将 dll 转换为 mexw32 文件 但我知道我是如何做到这一点的 我尝试使用加载库但它没有创建任何文件 我怎样才能做到这一点 loadlibrary http
  • 如何使用Matlab将数据保存到Excel表格中?

    我想将数据以表格形式保存在 Excel 工作表中 它应该看起来像 Name Age R no Gpa Adnan 24 18 3 55 Ahmad 22 12 3 44 Usman 23 22 3 00 每次当我执行我的文件时类数据 m 下
  • 在 MATLAB 中模拟 C++ 模板

    我试图找出创建 C 模板或 Java 通用对象的替代方案的最佳方法 出于多种不同的原因 我过去曾多次想这样做 但现在我想做的是为几个相关的类创建 saveobj 和 loadobj 函数 我的想法是 我想要一组通用的例程来创建默认结构 然后
  • matlab中无限while嵌套在for循环中

    我想做一个while循环 嵌套在for在 Matlab 中循环以查找数据中不同对之间的距离 我的数据具有以下形式 ID lon lat time 1 33 56 40 89 803 2 32 45 41 03 803 3 35 78 39

随机推荐

  • PL/SQL基础(1):语法

    本篇是 Oracle基础小结 系列之一 本篇目录 1 什么是PL SQL xff1f 2 PL SQL基本结构 3 PL SQL符号定义 4 PL SQL数据类型 5 PL SQL条件句法 6 PL SQL循环 什么是PL SQL xff1
  • PL/SQL基础(2):单元

    本篇是 Oracle基础小结 系列之一 PL SQL程序单元包括 xff1a PL SQL匿名块 PL SQL函数 PL SQL存储过程 PL SQL包 PL SQL触发器等 这里就用过的几个做简单记录 xff0c 另外虽然PL SQL异常
  • Oracle基础小结

    最近做了一些C 43 Oracle的工作 xff0c 在这里做一些笔记以备忘 xff0c 主要记录PL SQL的基础及小问题的解决 C 连接操作Oracle数据库的知识点 如果有想对oracle数据库的使用有基础性了解的也可以参阅 该系列目
  • PL/SQL基础(3):小专题

    本篇是 Oracle基础小结 系列之一 这里汇集了使用PL SQL中遇到的一些小问题和相关小专题文章的链接 xff0c 目前列出来一些 xff0c 后面还会陆续添加 专题1 xff1a 字符串函数和字符串截取 对于在使用存储过程中习惯性使用
  • 阿里云云效Maven制品仓库的ip白名单列表

    阿里云的云效提供了一系列的云开发工具 xff0c 其中包括 Maven 制品仓库 xff0c 可以提供便捷的 mvn 私库服务 但是因为公司基于安全考虑 xff0c 防火墙策略非常严格 xff0c 仅允许 ip 白名单列表内的数据包可以正常
  • Dokuwiki安装(linux)

    Dokuwiki安装 xff08 linux xff09 一 简介 dokuwiki是一个开源wiki引擎程序 xff0c 运行于PHP环境下 无需数据库 Doku Wiki 程序小巧而功能强大 灵活 xff0c 适合中小团队和个人网站知识
  • 1、Oracle PL/SQL中的字符串及函数介绍

    该文章是 PL SQL基础 xff08 3 xff09 xff1a 小专题 系列文章之一 Oracle中常用的字符串类型有 xff1a 固定长度 xff08 CHAR等 xff09 可变长度 xff08 VARCHAR2等 xff09 和大
  • 2、Oracle PL/SQL字符串分割截取

    该文章是 PL SQL基础 xff08 3 xff09 xff1a 小专题 系列文章之一 Oracle中的instr和substr函数 Oracle PL SQL中可以通过instr xff08 获取特定字符串的索引 xff09 和subs
  • 4、Oracle PL/SQL编译错误查看与处理

    该文章是 PL SQL基础 xff08 3 xff09 xff1a 小专题 系列文章之一 在编译Oracle PL SQL函数等时 xff0c 难免会遇到错误 例如 xff1a Function GETSTR1 已编译 Errors che
  • 3、Oracle PL/SQL中Date格式及格式转换

    该文章是 PL SQL基础 xff08 3 xff09 xff1a 小专题 系列文章之一 Oracle 插入日期 xff08 时间 xff09 时报错 xff1a ORA 01861 文字与格式字符串不匹配 这是由于插入的日期格式和数据库现
  • 5、Oracle数据库insert后获取自增的ID

    该文章是 PL SQL基础 xff08 3 xff09 xff1a 小专题 系列文章之一 在 insert 后使用 select 序列名 CURRVAL from dual 可以获取 insert后自增的ID 具体 SQL 语句 xff1a
  • 解决cmd 中ping>nul语句提示命令符无法识别

    问题描述 xff1a 在批量使用chrome exe ftp data hdf amp ping n10 127 0 0 1 gt nul 下载数据时 xff0c 命令行没有因为ping命令暂停 解决 xff1a 怀疑是ping这部分命令存
  • C#控件限制输入字符数且可用退格

    对于C 控件 xff08 例如textbox xff09 的输入限制长度 xff0c 直接想到的方法是在控件的KeyPress事件时判断控件已有的字符数来限制 假设控件名称为DAForm myBox4 xff0c KeyPress事件简单的
  • XXX事件的重载均与委托"System.EventHandler"不匹配

    在给动态创建控件添加事件时容易遇到的一个错误就是 xff1a XXX事件的重载均与委托 34 System EventHandler 34 不匹配 假设控件是MovePicBox xff0c 使用如下代码添加KeyPress事件 xff0c
  • 外部启动c#窗体程序传参问题

    问题 xff1a 需要在一个软件里启动另一个独立的C 窗体软件并传入参数 xff0c 例如下面的启动语句 string language 61 34 en us 34 System Diagnostics Process Start 34
  • C#控件控制输入文本长度

    C 在控制控件输入文本的长度时要注意两个问题 xff1a 1 传递的事件参数类型要是 KeyPressEventArgs xff1b 2 对退格键 xff08 backspace xff09 做例外处理 xff0c 不然在输入到最大程度时无
  • python打印等腰三角形

    d 61 int input 39 enter an int 39 l 61 39 39 2 d 1 d 初始化列表 for i in range d l i 61 list l i 字符串转列表 x 61 i y 61 0 x 61 d
  • 7、Oracle的;与ORA-00911: invalid character

    写SQL查询 Oracle中的数据时容易遇到一个奇怪的问题 xff1a 在一般的SQL developer查询分析器中写好的SQL语句运行一切正常 xff0c 放到C 写的程序中提交 ORACLE执行就报错 错误代码如下 xff1a ORA
  • C语言变量声明加冒号的用法

    有些信息在存储时 xff0c 并不需要占用一个完整的字节 xff0c 而只需占几个或一个二进制位 例如在存放一个开关量时 xff0c 只有0和1 两种状态 xff0c 用一位二进位即可 为了节省存储空间 xff0c 并使处理简便 xff0c
  • Matlab adjust axis tick labels, limits, and tick locations

    From https cn mathworks com matlabcentral answers 92565 how do i control axis tick labels limits and axes tick locations