【转】Matlab坐标修改及旋转

本文详细介绍Matlab中绘制二维图像的方法,包括如何设置曲线样式、坐标轴、刻度及图例等,帮助读者掌握Matlab绘图的高级技巧。

转自http://blog.sina.com.cn/s/blog_531bb7630100y43v.html

和http://blog.sina.com.cn/s/blog_6399df820100tr8x.html

和http://blog.163.com/yinhexiwen@126/blog/static/640482662013914105522753/

1、坐标轴删除

set(gca,'xtick',[])%去掉x轴的刻度

set(gca,'ytick',[]) %去掉xy轴的刻度

set(gca,'xtick',[],'ytick',[]) %同时去掉x轴和y轴的刻度

2、Matlab中“坐标轴刻度”的不同风格 

x=1:8;

subplot(2,2,1)
plot(x)
%tick style 0(auto)


subplot(2,2,2)
plot(x)
set(gca,'xtick',[1 3 6 8]);%style 1
set(gca,'ytick',[]);%style 2

subplot(2,2,3)
plot(x)
set(gca,'xtick',[1 3 6 8]);
set(gca,'xticklabel',sprintf('.4f|',get(gca,'xtick')));%style3
set(gca,'ytick',[2 4 5 7]);
set(gca,'yticklabel',{'Two','Four','Five','Seven'});%style 4

subplot(2,2,4)
plot(x)
set(gca,'xminortick','on');%style 5
set(gca,'ticklength',[0.05 0.025]);%style 6
set(gca,'tickdir','out');%style 7

另附Maltab坐标调整程序一段:

x=20:10:20000;
y=rand(size(x));
semilogx(x,y);
set(gca,'XLim',[20 20000]);
set(gca,'XMinorTick','off');
set(gca,'XTick',[2031.5 63 125 250 500 1000 2000 4000 8000 16000]);
set(gca,'XGrid','on');
set(gca,'XMinorGrid','off');

 

3、matlab坐标刻度调整

subplot(3,2,1)

plot(x)

title('默认格式')

subplot(3,2,2)

plot(x)

set(gca,'xtick',[1 3 6 8]);

set(gca,'ytick',[]);

title('X自定义间隔,Y关闭')

subplot(3,2,3)

plot(x)

set(gca,'xtick',[1 3 6 8]);

set(gca,'xticklabel',sprintf('.4f|',get(gca,'xtick')))

set(gca,'ytick',[2 4 5 7]);

set(gca,'yticklabel',{'Two','Four','Five','Seven'});

title('XY自定义间隔、精度及显示方式')

subplot(3,2,4)

plot(x)

set(gca,'xminortick','on');%style 5

set(gca,'ticklength',[0.05 0.025]);

set(gca,'tickdir','out');

title('XY坐标刻度显示方式')

subplot(3,2,5)

plot(x)

set(gca,'xtick',[min(x) (max(x)+min(x))/2 max(x)]);

set(gca,'ytick',[min(x) (max(x)+min(x))/2 max(x)]);

title('论文中常用的标准3点式显示')

x=20:10:20000;

y=rand(size(x));

subplot(3,2,6)

semilogx(x,y);

set(gca,'XLim',[20 20000]);

set(gca,'XMinorTick','off');

set(gca,'XTick',[20 31.5 63 125 250 500 1000 2000 4000 800016000]);

set(gca,'XGrid','on');

set(gca,'XMinorGrid','off');

title('自定义网格显示')

%%%%%%%%%%%%%%%%%%%%%%

%顺便附上可以格式化坐标刻度的程序段

x=get(gca,'xlim');

y=get(gca,'ylim');

set(gca,'xtick',[x(1) (x(1)+x(2))/2 x(2)]);

set(gca,'ytick',[y(1) (y(1)+y(2))/2 y(2)]);

-------------

get(gca,'xlim');是获取最大最小刻度的

如果需要获取所有在坐标轴上显示的刻度,需要使用get(gca,'ytick')


a=linspace(1,2,10)

plot(a,'--pr','linewidth',1.5,'MarkerEdgeColor','r','MarkerFaceColor','m','MarkerSize',10)

legend('a','Location','best')

title('a','FontName','Times New Roman','FontWeight','Bold','FontSize',16)

xlabel('T','FontName','Times New Roman','FontSize',14)

ylabel('a','FontName','Times New Roman','FontSize',14,'Rotation',0)

axis auto equal

set(gca,'FontName','Times New Roman','FontSize',14)

 

1.曲线线型、颜色和标记点类型

plot(X1,Y1,LineSpec, ) 通过字符串LineSpec指定曲线的线型、颜色及数据点的标记类型。

 

     线型            颜色        数据点标记类型

 标识符  意义     标识符意义     标识符   意义

    -    实线     r   红色        +     加号

    -. 点划线     g   绿色            圆圈

   --    虚线     b   蓝色        *     星号

    :    点线     c  蓝绿色             点

                  m  洋红色       x   交叉符号

                  y   黄色   square(s) 方格

                  k   黑色  diamond(d) 菱形

                  w   白色       ^  向上的三角形

                                 v  向下的三角形

                                 >  向左的三角形

                                 <  向右的三角形

                             pentagram(p) 五边形

                              hexagram(h) 六边形

 

2.设置曲线线宽、标记点大小,标记点边框颜色和标记点填充颜色等。

plot(…,’Property Name’, Property Value, …)

 

Property Name 意义    选项

 

LineWidth 线宽    数值,如0.51等,单位为points

MarkerEdgeColor 标记点边框线条颜色颜色字符,如’g, ’b’等

MarkerFaceColor 标记点内部区域填充颜色颜色字符

MarkerSize 标记点大小   数值,单位为points

 

 

 

 

3.坐标轴设置

范围设置:

a. axis([xmin xmax ymin ymax])设置坐标轴在指定的区间

b. axis auto 将当前绘图区的坐标轴范围设置为MATLAB自动调整的区间

c. axis manual 冻结当前坐标轴范围,以后叠加绘图都在当前坐标轴范围内显示

d. axis tight 采用紧密模式设置当前坐标轴范围,即以用户数据范围为坐标轴范围比例:

a. axis equal 等比例坐标轴

b. axis square 以当前坐标轴范围为基础,将坐标轴区域调整为方格形

c. axis normal 自动调整纵横轴比例,使当前坐标轴范围内的图形显示达到最佳效果

范围选项和比例设置可以联合使用,默认的设置为axis auto normal

 

4.坐标轴刻度设置

set(gca, XTick, [0 1 2]) X坐标轴刻度数据点位置

set(gca,'XTickLabel',{'a','b','c'}) X坐标轴刻度处显示的字符

set(gca,'FontName','Times New Roman','FontSize',14)设置坐标轴刻度字体名称,大小

FontWeight,bold’ 加粗 ‘FontAngle,italic’ 斜体

对字体的设置也可以用在title, xlabel, ylabel等中

 

5.图例

legend('a','Location','best') 图例位置放在最佳位置

 

6.更多的设置可以在绘图窗口中打开绘图工具,Inspector… 中查找

 

Various line types, plot symbols and colors may be obtained with

PLOT(X,Y,S) where S is a character string made from one element

from any or all the following 3 columns:

 

          b     blue       .     point              -     solid

          g     green       o     circle          :     dotted

          r     red           x     x-mark          -. dashdot

          c     cyan       +     plus             -- dashed

          m     magenta    *     star          (none)   no line

          y     yellow        s     square

          k     black       d     diamond

          w     white       v     triangle (down)

                           ^     triangle (up)

                           <     triangle (left)

                           >     triangle (right)

                           p     pentagram

                           h     hexagram

 

在使用Matlab时,经常需要将得到的数值表达成二维或三维图像。

 

plot(vector1,vector2)可以用来画两个矢量的二维图,例如

x=1:0.1:2*pi;

plot(x,sin(x))可以画正弦函数在0-2pi的上的图像。

 

plot函数可以接一些参数,来改变所画图像的属性(颜色,图像元素等)。下面是一些属性的说明

           b     blue(蓝色)       .     point(点)       -     solid(实线)

           g     green(绿色)      o     circle(圆圈)    :     dotted(点线)

           r     red(红色)        x     x-mark(叉号)    -.    dashdot (点画线)

           c     cyan(墨绿色)     +     plus(加号)       --    dashed(虚线)

           m     magenta(紫红色) *     star(星号)      (none) no line

           y     yellow(黄色)     s     square(正方形)

           k     black(黑色)      d     diamond(菱形)

                               v     triangle (down)

                               ^     triangle (up)

                               <     triangle (left)

                               >     triangle (right)

                               p     pentagram

                               h     hexagram

例如,plot(x,y,'.r')表示用点来画图,点的颜色是红色。

 

plot函数可以接一些参数,来改变所画图像的属性(颜色,图像元素等)。下面是一些属性的说明

 

           b     blue(蓝色)       .     point(点)       -     solid(实线)

 

           g     green(绿色)      o     circle(圆圈)    :     dotted(点线)

 

           r     red(红色)        x     x-mark(叉号)    -.    dashdot (点画线)

 

           c     cyan(墨绿色)     +     plus(加号)       --    dashed(虚线)

 

           m     magenta(紫红色) *     star(星号)      (none) no line

 

           y     yellow(黄色)     s     square(正方形)

 

           k     black(黑色)      d     diamond(菱形)

 

                               v     triangle (down)

 

                               ^     triangle (up)

 

                               <     triangle (left)

 

                               >     triangle (right)

 

                               p     pentagram

 

                               h     hexagram

 

 

    Example

 

       x = -pi:pi/10:pi;

 

       y = tan(sin(x)) - sin(tan(x));

 

       plot(x,y,'--rs','LineWidth',2,...

 

                       'MarkerEdgeColor','k',...

 

                       'MarkerFaceColor','g',...

 

                       'MarkerSize',10)

 

           xlabel('x');

 

           ylabel('y');

 

·         用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的运动情况。

 

 

 

legend函数的基本用法是

 

LEGEND(string1,string2,string3, ...)

 

分别将字符串1、字符串2、字符串3……标注到图中,每个字符串对应的图标为画图时的图标。

例如:

 

plot(x,sin(x),'.b',x,cos(x),'+r')

 

legend('sin','cos')这样可以把"."标识为'sin',把"+"标识为"cos"

 

还可以用LEGEND(...,'Location',LOC) 来指定图例标识框的位置

 

这些是Matlab help文件。后面一段是对应的翻译和说明

 

        'North'              inside plot box near top

 

        'South'              inside bottom

 

        'East'               inside right

 

        'West'               inside left

 

        'NorthEast'          inside top right (default)

 

        'NorthWest           inside top left

 

        'SouthEast'          inside bottom right

 

        'SouthWest'          inside bottom left

 

        'NorthOutside'       outside plot box near top

 

        'SouthOutside'       outside bottom

 

        'EastOutside'        outside right

 

        'WestOutside'        outside left

 

        'NorthEastOutside'   outside top right

 

        'NorthWestOutside'   outside top left

 

        'SouthEastOutside'   outside bottom right

 

        'SouthWestOutside'   outside bottom left

 

        'Best'               least conflict with data in plot

 

        'BestOutside'        least unused space outside plot

 

        'North'             图例标识放在图顶端

 

        'South'            图例标识放在图底端

 

        'East'               图例标识放在图右方

 

        'West'              图例标识放在图左方

 

        'NorthEast'       图例标识放在图右上方(默认)

 

        'NorthWest      图例标识放在图左上方

 

        'SouthEast'      图例标识放在图右下角

 

        'SouthWest'     图例标识放在图左下角

 

(以上几个都是将图例标识放在框图内)

 

        'NorthOutside'          图例标识放在图框外侧上方

 

        'SouthOutside'         图例标识放在图框外侧下方

 

        'EastOutside'           图例标识放在图框外侧右方

 

        'WestOutside'          图例标识放在图框外侧左方

 

        'NorthEastOutside'   图例标识放在图框外侧右上方

 

        'NorthWestOutside' 图例标识放在图框外侧左上方

 

        'SouthEastOutside'   图例标识放在图框外侧右下方

 

        'SouthWestOutside' 图例标识放在图框外侧左下方

 

(以上几个将图例标识放在框图外)

 

        'Best'                      图标标识放在图框内不与图冲突的最佳位置

 

        'BestOutside'           图标标识放在图框外使用最小空间的最佳位置

 

还是用上面的例子

 

legend('sin','cos','location','northwest')可以将标识框放置在图的左上角。

 

    Examples:

 

        x = 0:.2:12;

 

        plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));

 

        legend('First','Second','Third');

 

        legend('First','Second','Third','Location','NorthEastOutside')

 

        b = bar(rand(10,5),'stacked'); colormap(summer); hold on

 

        x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...

 

                 'markeredgecolor','y','markerfacecolor',[.6 0 .6],...

 

                 'linestyle','-','color','r','linewidth',2); hold off

 

        legend([b,x],'Carrots','Peas','Peppers','Green Beans',...

 

                  'Cucumbers','Eggplant')


1. label旋转
坐标轴的label可以在label axis 的时候设置旋转角度 xlabel(str,'Rotation',Rotaiondegree)
>> xlabel('This is xlabel','Rotation',15,'fontsize',20)
matlab中坐标轴label、title的旋转和xticklabel的旋转 - 瞳孔 - 瞳孔

2. title旋转
figure title也可以通过设置旋转属性来实现旋转,和label一样
>> hold on
>> title('This is title','rotation',15,'fontsize',20)
matlab中坐标轴label、title的旋转和xticklabel的旋转 - 瞳孔 - 瞳孔

3. ticklabel的旋转
matlab中ticklabel并没有Rotation这一属性可以设置,所以得另想办法。
text是可以设置旋转属性(Rotation)的,所以可以用text函数给figure在相应位置标上ticklabel,然后旋转。

 plot([0 1],[0 1]);
% 获取xticklabel的值
 xtl=get(gca,'XTickLabel'); 
 % 获取xtick的值
 xt=get(gca,'XTick'); 
% 获取ytick的值         
yt=get(gca,'YTick');   
% 设置text的x坐标位置们         
xtextp=xt;                   
 % 设置text的y坐标位置们      
 ytextp=(yt(1)-0.2*(yt(2)-yt(1)))*ones(1,length(xt)); 
% rotation,正的旋转角度代表逆时针旋转,旋转轴可以由HorizontalAlignment属性来设定,
% 有3个属性值:left,right,center
 text(xtextp,ytextp,xtl,'HorizontalAlignment','right','rotation',45,'fontsize',15); 
% 取消原始ticklabel
 set(gca,'xticklabel','');

matlab中坐标轴label、title的旋转和xticklabel的旋转 - 瞳孔 - 瞳孔

### MATLAB坐标系的旋转和平移 在 MATLAB 中,可以通过构建仿射变换矩阵来实现坐标系的旋转和平移操作。对于二维和三维空间中的点或图形,可以使用 `affine2d` 或者 `affine3d` 函数创建一个表示特定几何变换的对象。 #### 构建仿射变换矩阵 当需要先沿着某个方向平移再绕着某轴线旋转时,可以根据给定的角度θ以及位移向量 (tx, ty),构造出相应的变换矩阵 T: \[T=\begin{bmatrix}R & \vec{t}\\0& 1\end{bmatrix}\] 其中 R 是由 θ 定义的标准正交基下的旋转分量;而 \(\vec{t}\) 则代表了平移部分[^1]。 具体来说,在二维情况下,如果要顺时针旋转 α 度并且分别沿 X 和 Y 方向移动 tx 和 ty,则对应的齐次坐标形式下总的换矩阵可写作如下所示的形式: \[T_{2D}= \left[\begin{array}{ccc} \cos{\alpha}&-\sin{\alpha}&tx\\ \sin{\alpha}&\cos{\alpha}&ty\\ 0&0&1 \end{array}\right]\][^1] 而在三维场景里,除了上述提到的操作外还可以围绕 Z 轴做额外的变化。此时完整的变换表达式变为: \[T_{3D}= \left[\begin{array}{cccc} r_{xx}& r_{xy}& rz_x& tx \\ r_{yx}& r_{yy}& ry_z& ty \\ rz_x&r_yz& rz_z& tz \\ 0&0&0&1 \end{array}\right]\] 这里 \(r\) 表示的是相对于各个主轴的方向余弦组成的子矩阵。 #### 使用 MATLAB 的内置函数执行变换 为了简化这些复杂的计算过程,MATLAB 提供了一些方便易用的功能来进行此类运算。比如可以直接调用 `maketform()` 来定义自定义的空间映射关系,也可以借助于更高级别的接口如 `imwarp()` 对图像数据应用指定类型的变形效果[^2]。 但是针对点云或者其他离散型的数据集而言,推荐采用专门设计用来处理这类情况的方法 —— 即通过 `affine3d()` 创建描述所需运动学特性的对象实例之后传递给 `pctransform()` 执行实际的位置调整工作。 下面是基于此思路的一个简单案例展示如何读取外部文件内的点云集并对其施加一定角度范围内的随机翻动作后再重新渲染出来查看最终成果: ```matlab % 加载PLY格式模型文件作为输入源 ptCloud = pcread('teapot.ply'); % 展示原始状态下的茶壶外观特征 figure; pcshow(ptCloud); % 设定待实施的具体变动参数值(此处设定了45度倾斜程度) theta = randi([-90, 90], 1); rotMat = [ cosd(theta), -sind(theta), 0; sind(theta), cosd(theta), 0; 0, 0, 1]; % 将其封装成适配API需求结构体变量以便后续调用 tform = affine3d(rotMat); % 获取经过修改后的新版本实体副本 ptCloudOut = pctransform(ptCloud, tform); % 输出经加工处理过的结果图样至新窗口内观察对比差异之处 figure; pcshow(ptCloudOut); ``` 这段脚本不仅实现了基本功能而且还加入了少许随机因素使得每次运行都能得到略有不同的视觉呈现效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值