一次修改多个 XTick 值

Modify multiple XTick values at once

我想在 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

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

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

此外,最好使用 numel 而不是 length 并使用变量而不是 i 作为循环变量(因为这是一个 MATLAB built-in for 0 + 1i).此外,您可以在 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))