Remove only axis lines without affecting ticks and tick labels
Asked Answered
W

4

9

Is there a way to remove only the axis lines in the Matlab figure, without affecting ticks and tick labels.

I know that box toggles the upper and right axes lines and ticks and that works perfectly for me.
But my problem is that I want eliminate the bottom and left lines (only lines!) but keeping the ticks and tick labels.

Any tricks?

Whitlow answered 14/9, 2014 at 22:51 Comment(0)
S
7

There is another undocumented way (applicable to MATLAB R2014b and later versions) of removing the lines by changing the 'LineStyle' of rulers to 'none'.

Example:

figure;
plot(1:4,'o-');  %Plotting some data
pause(0.1);      %Just to make sure that the plot is made before the next step
hAxes = gca;     %Axis handle
%Changing 'LineStyle' to 'none'
hAxes.XRuler.Axle.LineStyle = 'none';  
hAxes.YRuler.Axle.LineStyle = 'none';
%Default 'LineStyle': 'solid', Other possibilities: 'dashed', 'dotted', 'dashdot'

output


This is different from Dan's answer which uses the 'visible' property of rulers.

Speciality answered 25/6, 2017 at 0:51 Comment(0)
D
10

Yair Altman's Undocumented Matlab demonstrates a cleaner way to do this using the undocumented axes rulers:

plot(x,y);
ax1 = gca;
yruler = ax1.YRuler;
yruler.Axle.Visible = 'off';
xruler = ax1.XRuler;
xruler.Axle.Visible = 'off'; %// note you can do different formatting too such as xruler.Axle.LineWidth = 1.5;

A nice feature of this approach is that you can separately format the x and y axis lines.

Drislane answered 18/8, 2015 at 13:11 Comment(1)
Thanks for the answer. For those who pulled their hair out like me trying to figure out why the XRuler of an axes with a bar was not disappearing: bar appears to have a ShowBaseLine property that must be turned off.Iglesias
S
8

Solution for Matlab versions prior to R2014b

You can introduce a new white bounding box and put it on top.

// example data
x = linspace(-4,4,100);
y = 16 - x.^2;

plot(x,y); hold on
ax1 = gca;
set(ax1,'box','off')  %// here you can basically decide whether you like ticks on
                      %// top and on the right side or not

%// new white bounding box on top
ax2 = axes('Position', get(ax1, 'Position'),'Color','none');
set(ax2,'XTick',[],'YTick',[],'XColor','w','YColor','w','box','on','layer','top')

%// you can plot more afterwards and it doesn't effect the white box.
plot(ax1,x,-y); hold on
ylim(ax1,[-30,30])

Important is to deactivate the ticks of the second axes, to keep the ticks of the f rist one.

enter image description here

In Luis Mendo's solution, the plotted lines are fixed and stay at their initial position if you change the axes properties afterwards. That won't happen here, they get adjusted to the new limits. Use the correct handle for every command and there won't be much problems.

Dan's solution is easier, but does not apply for Matlab versions before R2014b.

Step answered 15/9, 2014 at 5:30 Comment(7)
I needed to do this only for the y-axis and found a clean way thanks to Undocumented Matlab: https://mcmap.net/q/1124665/-remove-only-axis-lines-without-affecting-ticks-and-tick-labelsDrislane
@Drislane this is great! +1 - the new graphics engine makes everything so easy. I just hope it gets documented some day.Step
@Dan's answer using gca.YRuler.Axle.Visible property is much better solution.Rademacher
@Rademacher And you think that's worth a downvote? especially, because my answer also applies for old Matlab versions, and Dan's isn't! Please inform youself, what downvotes are for!Step
I am simply downvoting so that Dan's answer will have bigger vote, and OP might change his accepted answer. It is true that your answer work for any graphics system (not only Matlab), but rather is a ugly work around instead of solving the problem.Rademacher
@Rademacher "I am simply downvoting so that Dan's answer will have bigger vote" And this is exactly, how it does not work. Every answer should get votes independently of another. If you think an answer is good you upvote it, if you think its bad you downvote it. As far as I can see you think it's good, it's a not soo ugly solution, but there is another which is better, so upvote both! And the OP won't get noticed of your votes anyway and as he is a first timer, he probably won't even come back soon. I really don't care about the vote count, I just don't accept your downvote reason as valid.Step
Fair enough. They should be evaluated independently. Although I still deem your solution too hairy, it is universal and is directly applicable in any graphics system without knowing anything about how those graphics system works (e.g. knowing some secret undocumented features). So, considering independently, I am upvoting you. Thx for the education!Rademacher
S
7

There is another undocumented way (applicable to MATLAB R2014b and later versions) of removing the lines by changing the 'LineStyle' of rulers to 'none'.

Example:

figure;
plot(1:4,'o-');  %Plotting some data
pause(0.1);      %Just to make sure that the plot is made before the next step
hAxes = gca;     %Axis handle
%Changing 'LineStyle' to 'none'
hAxes.XRuler.Axle.LineStyle = 'none';  
hAxes.YRuler.Axle.LineStyle = 'none';
%Default 'LineStyle': 'solid', Other possibilities: 'dashed', 'dotted', 'dashdot'

output


This is different from Dan's answer which uses the 'visible' property of rulers.

Speciality answered 25/6, 2017 at 0:51 Comment(0)
D
2

You could "erase" the axis lines by plotting a white line over them:

plot(1:4,1:4) %// example plot

box off %// remove outer border
hold on
a = axis; %// get axis size
plot([a(1) a(2)],[a(3) a(3)],'w'); %// plot white line over x axis
plot([a(1) a(1)],[a(3) a(4)],'w'); %// plot white line over y axis

Result:

enter image description here


As noted by @SardarUsama, in recent Matlab versions you may need to adjust the line width to cover the axes:

plot(1:4,1:4) %// example plot

box off %// remove outer border
hold on
a = axis; %// get axis size
plot([a(1) a(2)],[a(3) a(3)],'w', 'linewidth', 1.5); %// plot white line over x axis.
                                                     %// Set width manually
plot([a(1) a(1)],[a(3) a(4)],'w', 'linewidth', 1.5); 
Denigrate answered 14/9, 2014 at 23:2 Comment(7)
Brilliant ... I was crawling through the properties and couldn't find anything to separate the grid color from the axis line.Humpy
Thanks. I couldn't find any property either. The closest is axis off, but that also removes ticks, labels and even background. So I came up with this not-terribly-elegant solution of covering the axis lines up :-)Denigrate
Cool ideas. Kudos @Luis Mendo and thewaywewalk.Whitlow
By the way, @thewaywewalk, colors of your lines are awesome. can you share the details of color scheme used for the line plots.Whitlow
@DennyAlappatt: read this or wait for the next version of Matlab. ;)Step
This works. But you may need to change the 'linewidth' property. Without changing, I got this: i.sstatic.net/Z6eIN.jpgSpeciality
@SardarUsama Thanks for the heads up. I get the same as you with R2015b. The graph in my answer was probably obtained with R2010b. In R2015b, width 1.5 seems to work fine (although it probably depends on the system). I have updated the answerDenigrate

© 2022 - 2024 — McMap. All rights reserved.