Search code examples
matlabplotmatlab-figure

Change the color of the bottom xaxis only


I would like to change the bottom x-axis of my graph to blue,‍‍‍‍‍‍‍ whilst keeping the other three sides black.‍‍‍‍‍‍‍‍‍‍‍‍‍ Is there an easy way to do this which I'm unaware of?‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Solution

  • A possible workaround is to put an empty axis on top and hide its ticks.

    Example:

    %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

    Caveat: This changes the mode of XTickLabels from auto to manual and thus any zooming/panning will not automatically update the tick colors.