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?

Dan
  • 45,079
  • 17
  • 88
  • 157
Denny Alappatt
  • 135
  • 1
  • 2
  • 5

4 Answers4

9

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.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • 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`. – Ayberk Özgür Jun 10 '17 at 13:29
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.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • I needed to do this only for the y-axis and found a clean way thanks to Undocumented Matlab: http://stackoverflow.com/a/32073460/1011724 – Dan Aug 18 '15 at 13:13
  • 1
    @Dan this is great! +1 - the new graphics engine makes everything so easy. I just hope it gets documented some day. – Robert Seifert Aug 18 '15 at 13:14
  • @Dan's answer using gca.YRuler.Axle.Visible property is much better solution. – hyiltiz Oct 31 '16 at 17:31
  • 1
    @hyiltiz 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! – Robert Seifert Oct 31 '16 at 22:16
  • 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. – hyiltiz Nov 02 '16 at 05:34
  • @hyiltiz "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. – Robert Seifert Nov 02 '16 at 06:43
  • 1
    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! – hyiltiz Nov 03 '16 at 01:52
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.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
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); 
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Brilliant ... I was crawling through the properties and couldn't find anything to separate the grid color from the axis line. – Hoki Sep 14 '14 at 23:05
  • 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 :-) – Luis Mendo Sep 14 '14 at 23:15
  • Cool ideas. Kudos @Luis Mendo and thewaywewalk. – Denny Alappatt Sep 15 '14 at 16:31
  • 1
    By the way, @thewaywewalk, colors of your lines are awesome. can you share the details of color scheme used for the line plots. – Denny Alappatt Sep 15 '14 at 16:41
  • @DennyAlappatt: [read this](http://undocumentedmatlab.com/blog/hg2-update) or wait for the next version of Matlab. ;) – Robert Seifert Sep 15 '14 at 21:52
  • This works. But you may need to change the `'linewidth'` property. Without changing, I got this: https://i.stack.imgur.com/Z6eIN.jpg – Sardar Usama Jun 25 '17 at 00:26
  • 1
    @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 answer – Luis Mendo Jun 25 '17 at 03:28