2

If we have a figure

plot(x, y);
grid on;

We get something like this

enter image description here

But now, I wish to hide the axis, so I tried the commands below:

axis off
set(gca,'xtick',[])
set(gca,'ytick',[])
set(gca,'visible','off')

Together they successfully hid the axis, but the grid was also deleted!

set(gca, 'xticklabel', []) can hide the label, but not the axis.

So, how do I hide the axis, ticks and labels, leaving only the plot and grid?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
null
  • 1,167
  • 1
  • 12
  • 30
  • I answered from a python / matplotlib perspective (didn't read clearly enough...), but am not sure it's true in Matlab itself, so I deleted the answer. For python's matplotlib, see [`tick_params`](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html). Perhaps this will help. – Brendan Jul 17 '19 at 12:59
  • @BrendanCox Thank u, I saw your answer, but even in matplotib, I saw you still didn't hide the axes. – null Jul 17 '19 at 13:00
  • Have you tried hiding the `ticklabels` but not the `ticks`? – avermaet Jul 17 '19 at 13:06
  • @avermaet yes, it won't delete the grid, but the axes are still there. – null Jul 17 '19 at 13:07
  • What do you mean by axes? The `XTicks`? Or `box off` – avermaet Jul 17 '19 at 13:08
  • Ah, so you want to hide the axis lines themselves? In matplotlib, this appears to be done via the `spines` property. See [this answer](https://stackoverflow.com/questions/925024/how-can-i-remove-the-top-and-right-axis-in-matplotlib) – Brendan Jul 17 '19 at 13:08
  • @avermaet both of them. `box off` for all four borders (top, bottom, left, right). – null Jul 17 '19 at 13:09
  • Is `figure; plot(x,y); ax=gca; ax.GridColor='b';` working for you ? If so, you could try to change the axes color to the same color as the background so they aren't visible. And then make the grid black or whatever color you want using the above. – avermaet Jul 17 '19 at 13:35

2 Answers2

5

You can set Xcolor and Ycolor to none so the axis won't be displayed:

%dummy data
x = [-5:.1:5];
y = normpdf(x,0,1);
plot(x, y);

%grid on
grid on;

%Set the axis color to none.
set(gca,'XColor','none','Ycolor','none')

enter image description here

obchardon
  • 10,614
  • 1
  • 17
  • 33
2

I'm not sure I understood what you wanted to achieve, but if this is what you meant,

enter image description here

here's how to do it:

function [] = q57076281()
% Plot some data with a grid:
x = linspace(0,2*pi,100);
y = sin(x);
figure(); hP = plot(x,y); hAx = hP.Parent; 
grid(hAx, 'on');

% Remove the box:
box(hAx, 'off'); 

% Hide the labels:
set(hAx, 'XTickLabel', [], 'YTickLabel', []);

% Hide the axes:
hXl = struct(hAx.XAxis).Axle; hXl.Visible = 'off';
hXl = struct(hAx.YAxis).Axle; hXl.Visible = 'off';

% Hide the ticks:
hAx.TickLength = [0,0];
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • That `struct` wrapper trick is neat for direct property referencing, but throws a warning (in R2017b at least). It would probably be better practise to use `hXl = get( hAx.XAxis, 'Axle' );`. Interestingly, replacing the intermediate variable `hXl` with a direct `set` command for the `Visible` property doesn't seem to work... – Wolfie Jul 17 '19 at 15:15