MATLAB Answers

How to add tick just for the left axis

22 views (last 30 days)
Dear all,
Here is my plot:
I turned off the thicklength in this plot using this code:
set(gca,'TickLength',[0 0])
Now I want to add thick just for left side like this plot below that I found it on the internet:
Thank you all


dpb on 4 May 2020
It's "tick"; "thick" is adjective... :)
MATLAB doesn't do that -- the ticks are shown on both axes if 'box on' is in effect; the other axis isn't shown if 'box off' is in effect but then the plot area isn't outlined.
If simply must have box and only the one set of tick marks, draw them with line
Behzad Navidi
Behzad Navidi on 4 May 2020
Thank you so much. I'm Sorry for my typo I will correct it in the question title.
dpb on 4 May 2020
It's potentially confusing to readers-- I thought at first you wanted heavier/bolder lines on the one axis vis a vis the other...
I think it's a logical enhancement request -- the way MATLAB HG2 works (similar in this regard to HG1) is annoying in that if you have to axes and box on the two conflict with each other.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 4 May 2020
Edited: Adam Danz on 4 May 2020
Here are two methods to show ticks only on the left axis.
Use yyaxis
This method creates a 2nd y axis on the right that is independently customizable and not used (unless you want to use it).
ax = axes();
box(ax, 'on') % default
% Set axis tick length and direction
ax.XAxis.TickLength = [0 0];
ax.YAxis.TickDirection = 'both';
% Create 2nd y-axis on right and set y axes colors
% to match the x-axis color
yyaxis right
ax2 = gca();
ax.YAxis(1).Color = ax.XAxis.Color;
ax.YAxis(2).Color = ax.XAxis.Color;
% Remove ticks on right axis
ax.YAxis(2).TickValues = [];
% Return control back to the left axes
yyaxis left
See image below for results.
Use xline and yline at axis limits
ax = axes();
box(ax, 'off') % default
ax.XAxis.TickLength = [0 0];
If you want the y-ticks to be in both directions,
ax.YAxis.TickDirection = 'both';
If you want the right and upper axes lines but without ticks, you can make is appear as though 'box' is 'on' by setting the axis limits and adding a xline and yline.
% This syntax just sets the axis limits to their current value
xlim(ax, xlim(ax))
ylim(ax, ylim(ax))
% Set right and upper axis lines to same color as axes
xline(max(xlim(ax)), 'k-', 'Color', ax.XAxis.Color)
yline(max(ylim(ax)), 'k-', 'Color', ax.YAxis.Color)
If the axis limits change after this change, the pseudo-box lines will no longer be positioned correctly so this should happen after all other plotting and positioning is done on the axes. The first method is therefore safer.
Final result for both methods


Behzad Navidi
Behzad Navidi on 4 May 2020
Dear Adam Danz,
Thank you, Yes I want something like the figure that you are attached.
I tried the first and the second method, but I don't know why when I add them after plotting code, the generated figure is:
Adam Danz
Adam Danz on 4 May 2020
In both methods I shared, the first line of code is producing an axes.
You don't need to do that part. You just need to switch in your axes handles instead of mine. Think about what the code is doing and execute each line individually to see what it does.
Behzad Navidi
Behzad Navidi on 4 May 2020
Really thank you. It fixed my problem.

Sign in to comment.

More Answers (0)





Translated by