仅更改底部 x 轴的颜色

Change the color of the bottom xaxis only

我想将图形的底部x轴更改为蓝色,同时将其他三个方面保持黑色。 =]

一种可能的解决方法是在顶部放置一个空轴并隐藏其刻度。

示例:

%Some random plot
x = 0:0.1:4*pi;
y = cos(x);
plot(x,y);

%Adjustments
ax1 = gca;                %Current axes
%Now changing x-axis color to blue
set(ax1,'XColor','b');    %or ax1.XColor='b' for  >=R2014b 
ax2=axes('Position',get(ax1,'Position'),... %or ax1.Position for >=R2014b
    'XAxisLocation','top','YAxisLocation','right','Color','none',...
    'XTickLabels',[] ,'YTickLabels',[],...
     'XTick', get(ax1,'XTick'));  %or ax1.XTick for >=R2014b
linkaxes([ax1 ax2]);      %for zooming and panning

警告: 这会将 XTickLabels 的模式从 auto 更改为 manual,因此任何 zooming/panning 都不会自动更新刻度颜色。

您可以通过访问一些 undocumented features 在较新版本的 MATLAB 中执行此操作。具体来说,您想要访问轴的 XRuler 属性 的 AxleMajorTickChild 属性(均存储 LineStrip 对象)。然后你可以修改 ColorBindingColorData 属性,使用 VertexData 属性 来这样做:

XColor = [0 0 1];                             % RGB triple for blue
hAxes = axes('Box', 'on', 'XColor', XColor);  % Create axes
drawnow;                                      % Give all the objects time to be created
hLines = hAxes.XRuler.Axle;                   % Get the x-axis lines
nLinePts = size(hLines.VertexData, 2)./2;     % Number of line vertices per side
hTicks = hAxes.XRuler.MajorTickChild;         % Get the x-axis ticks
nTickPts = size(hTicks.VertexData, 2)./2;     % Number of tick vertices per side
set(hLines, 'ColorBinding', 'interpolated', ...
            'ColorData', repelem(uint8([255.*XColor 255; 0 0 0 255].'), 1, nLinePts));
set(hTicks, 'ColorBinding', 'interpolated', ...
            'ColorData', repelem(uint8([255.*XColor 255; 0 0 0 255].'), 1, nTickPts));

剧情如下:

注意:这应该作为更新图的最后一步来完成。调整轴的大小或对轴进行其他更改(特别是任何更改 x 轴刻度线的内容)可能会引发警告并且无法正确呈现,因为上面的设置已被手动更改,因此不会在其他操作发生时自动更新。将其他属性设置为 'manual' 可能有助于避免这种情况,例如 XTickMode and XTickLabelMode.