仅更改底部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
模式并且因此任何缩放/平移不会自动更新刻度颜色。
答
你可以通过访问一些undocumented features在较新版本的MATLAB中做到这一点。具体而言,要访问轴的XRuler
属性的Axle
和MajorTickChild
属性(均存储LineStrip
对象)。然后,你可以修改ColorBinding
和ColorData
性能,使用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
和XTickLabelMode
。
+0
非常感谢!看起来有点混乱,所以我会做你的话,并将其作为最后一步。 –
谢谢@SardarUsama我会给这个去。 –