How to make only x-axis invisible (y-axis stays visible)?

I have two axes (top, bottom) in a GUI. I do not want the x-axis of the top ghraph to be present (because it is the same as the bottom x-axis). I could not find how to handle separately the x and y axis visibility.
Csaba

Antworten (11)

Wayne King
Wayne King am 23 Mai 2013
without seeing your code, a simple way is just to set the 'xtick' property to []
plot(randn(100,1));
set(gca,'xtick',[])

3 Kommentare

Csaba
Csaba am 23 Mai 2013
Thanks.
It is almost good. I still have the axis itself (ticks and labels have disappeared).
Csaba
Does this get closer?
subplot(211)
plot(randn(100,1))
set(gca,'xtick',[]);
set(gca,'xcolor',[1 1 1])
subplot(212)
plot(randn(100,1))
Csaba
Csaba am 24 Mai 2013
Sorry, no. It makes the axis white but I have a picture behind the graph. So white axis (and actually any colors) are visible in my case. I do not want the x-axis at all.
Csaba

Melden Sie sich an, um zu kommentieren.

Muhammad Shah
Muhammad Shah am 17 Dez. 2018
This Question is posted in 2013, but still last week got more than a thousand views, and I also needed this info, and I got very helpful information in this post, but surprizingly later I got an other solution in Matlab documentation and that was not yet here, and it is probably the smalest code, and I tested it and it worked nice, so I decided to addd it, here it is:
axis off ;
And thats it your axes are gone.

2 Kommentare

Csaba
Csaba am 20 Dez. 2018
Dear Muhammad,
First, this solution was already published (see above).
Second, in 2013 this command did not exist.
Dear Muhammad, thank you this was really helpful!

Melden Sie sich an, um zu kommentieren.

Johann Riemensberger
Johann Riemensberger am 27 Okt. 2016

7 Stimmen

Hi, axes('Color','none','XColor','none');
works for me Bests Johann

2 Kommentare

Wow after all that...thanks Johann!
Csaba
Csaba am 25 Dez. 2018
It does not work on the Matlab 2013. So I cannot accept this answer.

Melden Sie sich an, um zu kommentieren.

Jorge Mariscal Harana
Jorge Mariscal Harana am 5 Jul. 2017
Bearbeitet: Walter Roberson am 27 Mai 2023
Hi,
Try:
ax1.YAxis.Visible = 'off'; % remove y-axis
ax1.XAxis.Visible = 'off'; % remove x-axis
Hope that helps, J

3 Kommentare

Note: this uses syntax and properties available from R2014b, and so cannot could not have been used in the 2013 time-frame the question was originally asked for.
How about removing only one y axis (say right side) and not both?
Thanks
@Ankit Labh ax1.Box = false;
This removes both the top and right side axes, but not the left and bottom axes.

Melden Sie sich an, um zu kommentieren.

Rini Varghese
Rini Varghese am 9 Okt. 2018
Bearbeitet: Rini Varghese am 14 Apr. 2022
Try the following:
h = gca;
h.XAxis.Visible = 'off';

2 Kommentare

Cg Gc
Cg Gc am 14 Feb. 2019
This works great. Thank you.
This is better
If I set
set(gca,'xtick',[])
the grid will also vanish.
But with your code, the grid stays . Thanks

Melden Sie sich an, um zu kommentieren.

John Barber
John Barber am 24 Mai 2013
This solution might be overkill, but you can get that effect with my File Exchange program 'oaxes', available here: http://www.mathworks.com/matlabcentral/fileexchange/30018. The following will show only a y axis at the left edge of the plot:
oa = oaxes;
oa.XAxisLine = 'off';
oa.XLabel = '';
oa.YLabel = '';
oa.Arrow = 'off';
oa.Origin = [-Inf -Inf 0];
% If you want the normal y label to be visible:
ylabel('my y axis...')
set(get(gca,'YLabel'),'visible','on')
This should get you close to what you are looking for. The oaxes documentation will give you more information about the properties used in the example above, including an explanation of the difference between the oaxes 'YLabel' property which is set to empty above, and the parent axes' 'YLabel' text object. The main difference in appearance I am getting is that the oaxes ticks are bidirectional (they extend out on both sides from the axes line), while a normal axes has ticks that only extend to one side. Currently, there is no way to change this in oaxes, but I might add it in a future release.
-John
José-Luis
José-Luis am 24 Mai 2013
Bearbeitet: José-Luis am 24 Mai 2013
h = axes;
plot(h,rand(10,1));
pos = get(h,'Position');
new_h = axes('Position',pos);
linkaxes([h new_h],'y');
pos(3) = eps; %Edited here
set(new_h,'Position',pos,'XTick',[],'XTickLabel',[]);
set(h,'Visible','off');

4 Kommentare

Csaba
Csaba am 24 Mai 2013
Thanks. I tried a similar code before (making the line width zero). Running your code I get the following error:
************************************************************
Error using set
Width and height must be > 0
Error in prrrr_1 (line 8)
set(new_h,'Position',pos,'XTick',[],'XTickLabel',[]);
********************************************************
So the pos(3)=0; command makes the width zero which MATLAB does not accepts.
Csaba
My bad, please see the edited code where the width is made very small instead of zero. This would be sort of a kludge.
Csaba
Csaba am 27 Mai 2013
Yes, it works, although it is a workaround and not a solution.
Jan
Jan am 24 Nov. 2017
[EDITED, moved from flag] andreas jensen wrote:
Overly complicated and doesn't work

Melden Sie sich an, um zu kommentieren.

Martin
Martin am 24 Mär. 2016

0 Stimmen

I solved something similar that way:
set(axis_h,'XColor',axis_h.Parent.Color);
-Martin
This wasn't an option when the question was originally asked, but now you can change the Visible property of the appropriate ruler object that is part of the axes. Compare the axes without the ruler being changed:
ax = axes;
plot(ax, 1:10);
with one that does have the ruler turned off.
figure
ax2 = axes;
plot(ax2, 1:10);
% Get the ruler for the X axis
x = ax2.XAxis;
% Make it invisible
x.Visible = 'off';

1 Kommentar

That still doesn't answer the original question. The question was, could he remove the top axis only and keep the bottom axis. No one has answered this successfully

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 23 Mai 2013

Kommentiert:

am 4 Apr. 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by