单独使用Matlab绘图标签

时间:2014-08-13 15:08:55

标签: matlab

我想为matlab图中的每个刻度制作单独的标签。

我能做到这一点
xtick = [1, 10, 20]; 
xticklabels = {'January', 'February', 'December'};
set(gca, 'XTick', xtick);
set(gca, 'XTickLabel', xticklabels);

由于琴弦很长,我想以倾斜的方式制作琴弦。所以如果有人能帮助我以倾斜的方式展示标签,我会非常高兴。

谢谢

1 个答案:

答案 0 :(得分:1)

我曾经遇到过类似的问题,我在Mathworks的Matlab答案中找到了一个例子。基本上,您使用标签创建文本对象并旋转它们。否则,文件交换here上的提交看起来非常好。希望有所帮助!

clear
clc

% Generate some test data.  Assume that the X-axis represents months.
x = 1:12;
y = 10*rand(1,length(x));

% Plot the data.
h = plot(x,y,'+');

% Reduce the size of the axis so that all the labels fit in the figure.
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .2, pos(3) .65])

% Add a title.
title('This is a title')

% Set the X-Tick locations so that every other month is labeled.
Xt = 1:2:11;
Xl = [1 12];
set(gca,'XTick',Xt,'XLim',Xl);

% Add the months as tick labels.
months = ['Jan';
          'Feb';
          'Mar';
          'Apr';
          'May';
          'Jun';
          'Jul';
          'Aug';
          'Sep';
          'Oct';
          'Nov';
          'Dec'];
ax = axis;    % Current axis limits
axis(axis);    % Set the axis limit modes (e.g. XLimMode) to manual
Yl = ax(3:4);  % Y-axis limits

% Place the text labels
t = text(Xt,Yl(1)*ones(1,length(Xt)),months(1:2:12,:));
set(t,'HorizontalAlignment','right','VerticalAlignment','top', ...
      'Rotation',45);

% Remove the default labels
set(gca,'XTickLabel','')

% Get the Extent of each text object.  This

% loop is unavoidable.
for i = 1:length(t)
  ext(i,:) = get(t(i),'Extent');
end
% Determine the lowest point.  The X-label will be
% placed so that the top is aligned with this point.
LowYPoint = min(ext(:,2));
% Place the axis label at this point
XMidPoint = Xl(1)+abs(diff(Xl))/2;
tl = text(XMidPoint,LowYPoint,'X-Axis Label', ...
          'VerticalAlignment','top', ...
          'HorizontalAlignment','center');