Matlab 仅更改底部X轴的颜色
我想将图形的底部x轴更改为蓝色, 同时保持其他三面为黑色。 有没有一种我不知道的简单方法? 一种可能的解决方法是在顶部放置一个空轴并隐藏其刻度 示例:Matlab 仅更改底部X轴的颜色,matlab,plot,matlab-figure,Matlab,Plot,Matlab Figure,我想将图形的底部x轴更改为蓝色, 同时保持其他三面为黑色。 有没有一种我不知道的简单方法? 一种可能的解决方法是在顶部放置一个空轴并隐藏其刻度 示例: %Some random plot x = 0:0.1:4*pi; y = cos(x); plot(x,y); %Adjustments ax1 = gca; %Current axes
%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
,因此任何缩放/平移都不会自动更新刻度颜色 您可以在较新版本的MATLAB中通过访问一些。具体地说,您希望访问轴的XRuler
属性的axe
和MajorTickChild
属性(都存储LineStrip
对象)。然后可以使用VertexData
属性修改ColorBinding
和ColorData
属性:
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轴记号的任何操作)可能会引发警告,并且无法正确渲染,因为上述设置已手动更改,因此在其他操作发生时不会自动更新。将其他属性设置为
“手动”
可能有助于避免这种情况,例如和。非常感谢!看起来有点混乱,所以我会照你说的做,并将此作为最后一步。谢谢@SardarUsama,我会尝试一下。