仅更改底部x轴的颜色

问题描述:

我想将图形的底部x轴更改为蓝色,同时保持其他三面都是黑色。是否有一种简单的方法可以做到这一点,但我不知道?仅更改底部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 

output

警告:这改变的XTickLabelsautomanual模式并且因此任何缩放/平移不会自动更新刻度颜色。

+0

谢谢@SardarUsama我会给这个去。 –

你可以通过访问一些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)); 

而这里的情节:

enter image description here

注:这项工作应作为最后的步骤更新情节。调整轴大小或进行其他更改(特别是任何改变X轴刻度标记的内容)可能会引发警告并且无法正确呈现,因为上述设置已手动更改,因此在其他情况下不会自动更新。将其他属性设置为'manual'可能有助于避免此问题,例如XTickModeXTickLabelMode

+0

非常感谢!看起来有点混乱,所以我会做你的话,并将其作为最后一步。 –