Add a label to a minor tick, in an inset plot

6 views (last 30 days)
How to add a label of one minor tick (in the x-axis) of an inset plot?
% create a plot
x1 = linspace(0,1000);
y1 = sin(2*pi*x1);
plot(x1,y1)
set(gca,'Fontsize',20,'FontWeight','normal','LineWidth',1);
% create the inset plot
ax2 = axes('Position',[.45 .45 .4 .4]);
box on
plot(x1,y1) % <-- inset plot
set(ax2,'XScale','log') % <-- set log-scale for the x-axis of the inset plot
set(ax2,'Fontsize',20,'FontWeight','normal','LineWidth',1);
ax2.TickLength = [0.04 0];
% Set the axes limits
xlim([0 60])
ylim([0 Inf])
% Display the values/positions of both the major and minor ticks
drawnow
ax2_mtv = ax2.XAxis.MinorTickValues % <-- get the values/positions of the minor ticks in the inset plot
ax2_mtv = 1x7
15 25 30 35 45 50 55
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ax2.XTick % <-- get the values/positions of the the major ticks in the inset plot
ans = 1x3
20 40 60
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
My desired output is to add the label of the highlighted (in yellow) minor tick "ax2_mtv(1)", i.e."15", as follows, and without getting extra minot ticks (please see my comment below):
  3 Comments
Yukthi S
Yukthi S on 2 Sep 2024
Edited: Yukthi S on 2 Sep 2024
Hi @Sim
You can use "text" function available in MATLAB to add label at minor tick 15.
% Create a main plot
x1 = linspace(0, 1000);
y1 = sin(2*pi*x1);
plot(x1, y1)
set(gca, 'FontSize', 20, 'FontWeight', 'normal', 'LineWidth', 1);
% Create the inset plot
ax2 = axes('Position', [.45 .45 .4 .4]);
box on
plot(x1, y1) % Inset plot
set(ax2, 'XScale', 'log') % Set log-scale for the x-axis of the inset plot
set(ax2, 'FontSize', 20, 'FontWeight', 'normal', 'LineWidth', 1);
ax2.TickLength = [0.04 0];
% Set the axes limits
xlim([0 60])
ylim([0 Inf])
% Display the values/positions of both the major and minor ticks
drawnow
ax2_mtv = ax2.XAxis.MinorTickValues; % Get the values/positions of the minor ticks in the inset plot
minor_tick_position = ax2_mtv(1); % This is the position '15' you want to label
% Add a text label "x=fifteen" at the position of the minor tick
text(minor_tick_position, 0, 'x=fifteen', 'FontSize', 14, 'VerticalAlignment', 'top', ...
'HorizontalAlignment', 'center', 'Parent', ax2);

Sign in to comment.

Accepted Answer

dpb
dpb on 2 Sep 2024
Edited: dpb on 2 Sep 2024
% create a plot
x1 = linspace(0,1000);
y1 = sin(2*pi*x1);
hF=figure; % create a figure handle to put stuff into...
hAx1=axes(hF,'Fontsize',20,'FontWeight','normal','LineWidth',1); % put the axes in the desired figure
hL1=plot(hAx1,x1,y1); % and the first line in that axis
hAx1.YAxis.TickLabelFormat='%.1f'; % fix ugly origin formatting
% create the inset plot
hAx2=axes(hF,'Position',[.45 .45 .4 .4],'Fontsize',20,'FontWeight','normal','LineWidth',1, ...
'box', 'on');
hL2=plot(hAx2,x1,y1); % <-- inset plot
hAx2.XScale='log';
hAx2.TickLength = [0.04 0];
xlim(hAx2,[0 60]), ylim(hAx2,[0 Inf])
hAx2.YAxis.TickLabelFormat='%.1f'; % fix ugly origin formatting
drawnow
xMajTicks=hAx2.XAxis.TickValues
xMajTicks = 1x5
20 30 40 50 60
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xMinTicks=hAx2.XAxis.MinorTickValues
xMinTicks = 1x20
12 14 16 18 22 24 26 28 32 34 36 38 42 44 46 48 52 54 56 58
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
minorTicks=[15];
hAx2.XAxis.MinorTickValues=minorTicks;
hTxt=text(hAx2,hAx2.XAxis.MinorTickValues,-0.025,string(hAx2.XAxis.MinorTickValues), ...
'HorizontalAlignment','center','VerticalAlignment','top', ...
'FontName',hAx2.FontName,'FontSize',hAx2.FontSize);
%drawnow
From the properties documenation for the numeric ruler --
Minor tick mark display, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). ...
  • 'on' — Display minor tick marks between the major tick marks on the axis. This is the default value for an axis with a log scale. The space between the major tick marks determines the number of minor tick marks. If the MinorTickValues property is set to empty [], then no minor tick marks appear. Specify the tick mark locations by setting the MinorTickValues property.
  • 'off' — Do not display minor tick marks. This is the default value for an axis with a linear scale.
You'll first note that the minor ticks are not spaced at 0, 5, 10, ..., etc. because with a log axis, the origin cannot start at 0 and minor ticks are set by default dividing the space between tick marks by an internally-derived number. The setting of a log axis for a variable which contains zero also triggers an internal algorithm to set the base lower limit of a log axis since there are an infinite number of decades between 0 and any number greater than zero, it will set the lower axis limit to the decade that encompasses the first nonzero element in the data for the subject axis. The default number of elements for linspace is 100 so the first element in x1 is 1000/(100-1) --> 1000/99 = 10.101010... Consequently, as you can observe the first decade shown is 10-100 but also note the 10^1 left tick mark isn't shown because the effective xlim is [10.1010 60], not [10 60]
Uncomment the following line and you'll notice the difference...
%xlim(hAx2,[10 60])
As the above illustrates, though, you can set minor ticks wherever you want them; the above illustrates simply leaving the one additional; you can write the code to add them between the various tick values at the even values as desired.
There is no complementary 'MinorTickValuesLabels' property as there is for ticks so you can't write in arbitrary labels and they're not labeled being minor ticks so you have to write those labels manually with text(). The horizontal position is easy; you have to figure out the vertical displacement from the x axis by trial and error. Note I set the font characteristics to match the axis values.
I also added a nicety on the y axis labels to include the decimal in the origin so the formatting is the same for all on the y axis; I've always thought it proverbial-appendage-ugly with the default '%g' formatting.
And, lastly, note don't need the last drawnow anymore because the code isn't retrieving data after a change without making some other graphics change that required the event queue to be flushed (drawnow). Now that I write that, I didn't try that particular perturbation to see if it would likewise fix the original issue or not; I'm guessing it would, but the drawnow solution is the correct one for that particular problem case.
  4 Comments
dpb
dpb on 3 Sep 2024
Don't need no loops here...
minorTicks=[13 15 18];
hAx2.XAxis.MinorTickValues=minorTicks;
hTxt=text(hAx2,hAx2.XAxis.MinorTickValues,repmat(-0.025,size(hAx2.XAxis.MinorTickValues), ...
string(hAx2.XAxis.MinorTickValues), ...
'HorizontalAlignment','center','VerticalAlignment','top', ...
'FontName',hAx2.FontName,'FontSize',hAx2.FontSize);
since all functions used are vectorized...
I was also going to note that unless the smaller tick marks are really, really important, one could just add to the tick value array and get the labelling for free...
minorTicks=[13 15 18];
hAx2.XAxis.TickValues=sort([hAx2.XAxis.TickValues minorTicks]);
Sim
Sim on 3 Sep 2024
Thanks a lot, the "trick" of your answer is perfect to me... and.... yes, in my case, the little tick marks are important in showing a zoomed area of a plot in a proper and clear way :-) Thanks for your contribution in advancing science :-)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!