MATLAB Answers

How to remove the y axis and tick marks but keep the tick label and axis label?

88 views (last 30 days)
Adenine Koo
Adenine Koo on 6 Nov 2020
Answered: Benjamin Kraus on 16 Dec 2020
May I know how to remove the y axis and tick marks (marked with red arrow), but keep its label ('Run Number') and tick labels (1 to 6) as in the plot below?
My code:
fwdSpikeX was attached as mat file
ItrNum_fwd = 6;
runstart = -40.948564603973250;
runend = 40.948564603973250;
binWidth = 5;
binRange = -42.5:5:42.5;
% For each fwd run, plot the spike firing positions as raster plot, subplot 5
subplot(3,2,5);
for ff = 1:ItrNum_fwd
NoSpikeFlag = isempty(fwdSpikeX{ff});
if ~NoSpikeFlag
plot(fwdSpikeX{ff}, ff, 'k.'); % Plot spikes as black dots along x axis
hold on
end % for NoSpikeFlag
end % for ff
x1 = runstart;
x2 = runend;
line([x1 x1], [0 ItrNum_fwd+1]); % Plot vertical line at runstart
hold on
line([x2 x2], [0 ItrNum_fwd+1]); % Plot vertical line at runend
hold on
axis([binRange(1) binRange(end) 0 ItrNum_fwd+1]);
ax = gca;
ax.Box = 'off';
ax.YDir = 'reverse';
ax.YTick = (1:1:ItrNum_fwd);
ax.YTickLabel = (1:1:ItrNum_fwd);
ax.XTick = (binRange(1):binWidth:binRange(end));
ax.XTickLabel = (0:binWidth:binRange(end) - binRange(1));
ax.TickDir = 'out';
ax.YLabel.String = ('Run Number');
ax.XLabel.String = ('Positions along X-axis');
drawnow
Thank you!
Here is my desired output:

Answers (2)

VBBV
VBBV on 6 Nov 2020
%if true
% code
%end
ax.YColor = 'w';% use white color
ax.Ytick = [];
  2 Comments
VBBV
VBBV on 6 Nov 2020
%if true
% code
%end
ax = gca
set(ax,'YColor','b');% use blue color
set(ax,'YTick',{}); % no tick marks ,empty
ax.YTickLabel = ({'1','2','3','4','5','6'}); % specfify your ticklabels
ax.YLabel.String = 'Run Number';
It may be that you always need same number of ticks and ticklabels
Better to set the color of YTick as white and pass it as argument

Sign in to comment.


Benjamin Kraus
Benjamin Kraus on 16 Dec 2020
You can remove the tick marks by setting the TickLength property on the axes to [0 0], but that will remove ticks from both the x and y axis, and unfortunately there is no way to remove the y-axis without also removing the tick labels.
Therefore, the best solution is to create your own tick labels, which is not as complicated as it sounds.
For example:
ax = axes;
plot(rand(100,1)+0.05,randi(6,100,1), '.')
ax.YLim = [0 7];
ax.YAxis.Visible = 'off';
ytickvalues = 1:6;
x = zeros(size(ytickvalues));
str = string(ytickvalues);
text(x, ytickvalues, str, 'HorizontalAlignment', 'right');
xline(0.05,'b')
box off

Tags

Community Treasure Hunt

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

Start Hunting!