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 的相关文章

  • 如何消除 matplotlib 轴的相对偏移

    当我尝试对具有足够大数字的范围进行绘图时 我得到一个所有刻度都有相对偏移的轴 例如 plot 1000 1001 1002 1 2 3 我在横坐标轴上得到这些刻度 0 0 0 5 1 0 1 5 2 0 1e3 问题是如何删除 1e3并得到
  • matlab mex 文件和 C++ dll (Windows)

    我有一个带有 Test 类的 DLL 标题 class MY EXPORT Test public int doit const string str 和来源 int Test doit const string str return in
  • 带有 twinx() 的辅助轴:如何添加到图例

    我有一个带有两个 y 轴的图 使用twinx 我还给线条添加了标签 并想用legend 但我只成功获取图例中一个轴的标签 import numpy as np import matplotlib pyplot as plt from mat
  • 检查Matlab中脚本需要使用的函数

    我有一个别人写的代码包 我正在运行一个脚本 它调用一些函数 这些函数又调用更多函数 等等 我想获取不是 MATLAB 内置函数但属于包的一部分的函数列表 我尝试使用matlab codetools requiredFilesAndProdu
  • Matlab - 如果值包含xxx,则删除元胞数组中的行

    在 Matlab 中 如何删除包含变量字符串的元胞数组中的元胞 假设我的元胞数组是 C svnTrunk RadarLib radarlb utilities scatteredInterpolant m C svnTrunk RadarL
  • 在 matlab 中求 3d 峰的体积

    现在我有一个带有峰值的 3D 散点图 我需要找到其体积 我的数据来自图像 因此 x 和 y 值表示 xy 平面上的像素位置 z 值是每个像素的像素值 这是我的散点图 scatter3 x y z 20 z filled 我试图找到数据峰值的
  • 使用mat2cell将MxN的矩阵划分为1xN大小的M矩阵

    我有一个大小为 MxN 的矩阵 比方说 1867x3 1867 行和 3 列 我想将其分成 1867 个大小为 1x3 的单元格 我使用了mat2cell X 1 1866 这里X是矩阵 1867x3 结果给出了两个单元格 一个单元格的大小
  • 不等间隔时间序列的移动平均线

    我有一个证券交易所股票价格的数据集 时间 价格 但数据点之间的间隔并不相等 从 1 到 2 分钟不等 在这种情况下计算移动平均值的最佳实践是什么 如何在Matlab中实现呢 我倾向于认为 点的权重应该取决于自上一个点以来的最后时间间隔 Ma
  • 为什么旋转 3D 点云后顶点法线会翻转?

    我有两个人脸 3D 点云样本 蓝色点云表示目标面 红色点云表示模板 下图显示目标面和模板面在不同方向上对齐 目标面大致沿 x 轴 模板面大致沿 y 轴 Figure 1 The region around the nose is displ
  • 如何在 Matlab 中对数组应用低通或高通滤波器?

    有没有一种简单的方法可以将低通或高通滤波器应用于 MATLAB 中的数组 我对 MATLAB 的强大功能 或数学的复杂性 有点不知所措 需要一个简单的函数或一些指导 因为我无法从文档或网络搜索中找到答案 看着那 这filter http w
  • MATLAB:具有复数的 printmat

    我想使用 MATLAB 的printmat显示带有标签的矩阵 但这不适用于复数 N 5 x rand N 1 y rand N 1 z x 1i y printmat x y z fftdemo N 1 2 3 4 5 x y x iy O
  • 在 Matlab 中将 datenum 转换为 datetime 的最快方法

    我在 Matlab 中将 datenum 转换为 datetime 时遇到问题 Given dnum floor now floor now 1 我尝试了以下方法 datenum dnum 但这没有用 我发现有效的方法是 datetime
  • 括号中的波形符字符

    在 MATLAB 中 以下代码执行什么操作 m func returning matrix 波浪号运算符 的作用是什么 在 Matlab 中 这意味着不要将函数中相应的输出参数分配到赋值的右侧 因此 如果func returning mat
  • matlab中的排列函数是如何工作的

    这是一个有点愚蠢的问题 但我似乎无法弄清楚排列在 matlab 中是如何工作的 以文档为例 A 1 2 3 4 permute A 2 1 ans 1 3 2 4 到底是怎么回事 这如何告诉 matlab 3 和 2 需要交换 哇 这是我迄
  • 如何找到平面和 3d 矩阵之间的交平面

    如果我有一堆图像并且尺寸如下 size M 256 256 124 我有 3 个点 它们的坐标是 coor a 100 100 124 coor b 256 156 0 coor c 156 256 0 如何创建 M 与这 3 个点定义的平
  • 如何在放置颜色条后保持子图大小不变

    假设我们有一个 1 2 子图 我们在其中绘制了一些图形 如下所示 subplot 1 2 1 surf peaks 20 subplot 1 2 2 surf peaks 20 然后我们要添加一个颜色条 colorbar 我不希望结果中的正
  • WSDL2Java 抛出无法找到主类:org.apache.axis.wsdl.WSDL2Java

    我正在尝试从远程 Web 服务创建 java 文件 我下载了axis 1 4 将lib文件夹复制到c data axis lib其中包含这些文件 axis jar 轴 ant jar commons discovery 0 2 jar co
  • 如何更改Plotyy第二轴的颜色和字体大小?

    我使用 MATLAB 的plotyy 函数绘制了两条曲线 AX H1 H2 plotyy voltage span amplitude voltage span Ca SR The problem is that I cannot chan
  • MATLAB 中的霍夫变换

    有谁知道如何使用霍夫变换来检测二值图像中最强的线 A zeros 7 7 A 6 10 18 24 36 38 41 1 使用 rho theta 格式 其中 theta 以 45 为步长 从 45 到 90 以及如何在 MATLAB 中显
  • Matlab dec2bin 给出错误的值

    我正在使用 Matlab 的 dec2bin 将十进制数转换为二进制字符串 但是 我得到了错误的结果 例如 gt gt dec2bin 13339262925365424727 ans 101110010001111010010100111

随机推荐

  • 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