一次修改多个XTick值

时间:2017-01-14 00:09:03

标签: matlab matlab-figure scatter-plot

我想在Matlab中创建散点图矩阵(版本R2014b),并且每个x和y轴只有两个刻度(最小值和最大值),因为真实世界的示例有更多的绘图,并且更容易阅读。下面是一个示例,如您所见,第二个for循环不能按预期编辑刻度。另外,有没有办法在没有for循环的情况下设置它?

% Example data
        example =    [   -5.2844    5.0328  307.5500
  -12.0310    5.1611  237.9000
  -15.9510    7.5500  290.7600
  -11.5500   13.6850  285.8400
   -1.9356    9.4700  273.2600
   -9.2622    4.7456  232.6000
   -6.5639    5.2272  265.3800
   -4.4795   14.8320  281.1300
   -2.1474   13.0690  288.7300
   -3.7342   11.7450  265.2200
  -11.9040   10.9660  286.5700
   -2.1789    6.7442  258.9700
   -2.8316   11.8210  281.7500
   -2.6170    7.4740  244.8600
   -6.8770    1.6623  116.9800
  -10.2210    5.7575  300.2200
   -3.9430    5.9715  253.6000
   -3.3690    5.6530  230.9700
    2.6090    3.2750  236.8700
   -5.1430    8.4060  273.9600
   -7.9360    2.7855  254.8200
   -2.8665    2.0241  176.0600
   -0.0960    3.3165  228.6800
   -8.8465   10.3240  289.2100
   -8.1930   16.4070  289.7000
   -6.5840   13.4010  283.2600
    2.2405    8.4625  270.1000
   -2.2505    7.5555  285.9100
   -4.6955    6.2985  279.2000
   -0.7610   12.5210  283.2000
   -0.7510    9.9470  279.9100
    1.7120    8.5990  285.5700
   -0.6245    7.6905  251.9100
  -19.2320    6.8545  306.2700
   -4.1255    9.8265  298.2000
    2.9486    3.8881  250.2100
    1.7333    5.5000  240.7300
   -6.5881    3.9152  234.3800
   -7.9543    8.0771  276.8000
   -6.9641    8.8573  284.2800
  -10.3280    7.4768  291.8700
   -8.0818    5.1250  250.6600
  -10.1490    3.9427  271.0000
   -2.7786    3.7673  213.6500
  -14.5410   11.1500  288.9100
   -6.8118   11.0210  280.2000
   -2.6459    6.5127  213.2600
    1.4036    4.2023  253.9400
   -5.0014    9.3900  267.0600
   -9.7382   12.0990  290.8800]

% Labels for data
data_labels = {'Variable1','Variable2',...
    'Variable3'};

% Make scatterplot matrix with histogram on diagonal
figure()
[H,AX]= plotmatrix(example(:,:));

% label the axes of the plots (rotated) works fine
for i = 1:length(AX)
    ylabel(AX(i,1),data_labels{i},'Rotation',0,'HorizontalAlignment','right',...
        'FontSize',12);
    xlabel(AX(end,i),data_labels{i},'Rotation',60,'HorizontalAlignment','right',...
        'FontSize',12);
end

% Set ticks (not working yet)
NumTicks = 2;
for i = 1:length(AX)
    L = get(AX(i,i),'XLim');
    set(AX(i,i),'XTick',linspace(L(1),L(2),NumTicks));
    % set y ticks here too
end

1 个答案:

答案 0 :(得分:1)

在索引i时,您错误地使用了AX两个下标。

AX(i)   % Rather than AX(i,i)

此外,最好使用numel而不是length并使用变量而不是i作为循环变量(因为这是0 + 1i内置的MATLAB) 。此外,您可以将单个线性索引用于AX,而不是指定行和列下标。

for k = 1:numel(AX)
    L = get(AX(k),'XLim');
    set(AX(k),'XTick',linspace(L(1),L(2),NumTicks));
end

如果您想在循环中执行此操作,您可以在技术上使用cellfun执行此操作,并且您可以使用以下语法在多个绘图对象中设置相同的属性

set(array_of_plot_handles, {'Property'}, array_of_values_to_set)

因此适用于您的问题,就像

% Get all XLims as a cell array
oldlims = get(AX, 'XLim');

% Compute the new xlims
newlims = cellfun(@(x)linspace(x(1), x(2), NumTicks), oldlims, 'UniformOutput', false);

% Now set the values
set(AX, {'XTick'}, newlims);

或者如果你真的希望它是一行

set(AX, {'XTick'}, cellfun(@(x)linspace(x(1),x(2),NumTicks), get(AX, 'Xlim'), 'UniformOutput', false))