How to get the values of the XAxis Minor Ticks (of an inset plot)

조회 수: 7 (최근 30일)
Sim
Sim 2024년 9월 2일
편집: Sim 2024년 9월 3일
I have an inset plot, in a log-scale, and I would like to get the values of its XAxis Minor Ticks, with the following command:
ax2.XAxis.MinorTickValues
However, when I run the following code
% 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);
% Result
ax2.XAxis.MinorTickValues
ans = []
In other words, the followin command
ax2.XAxis.MinorTickValues
returns an empty array!
Another comment: if I go to the Command Window (when the figure is still on the monitor of the computer) and I type the same command, I get the correct values
>> ax2.XAxis.MinorTickValues
ans =
Columns 1 through 9
10 20 30 40 50 60 70 80 90
Columns 10 through 18
100 200 300 400 500 600 700 800 900
Column 19
1000
How can I get the values of the XAxis Minor Ticks of an inset plot, within a code, and not as an "external command", in the Command Window?

답변 (3개)

Gayathri
Gayathri 2024년 9월 2일
편집: Gayathri 2024년 9월 2일
Hello @Sim,
I understand that you are getting an empty array while running "ax2.XAxis.MinorTickValues" in code but getting output while running the same code in command window. It might be due to the timing of when the "MinorTickValues" property is being accessed. In MATLAB, certain properties, like "MinorTickValues", may not be initialized until the plot is fully rendered and visible. You can use "drawnow" to force MATLAB to complete the rendering of the figure before accessing the "MinorTickValues" property as shown below.
drawnow;
ax2.XAxis.MinorTickValues
For more information about "drawnow" you can refer the below mentioned link.
Hope this resolves the issue mentioned.

Star Strider
Star Strider 2024년 9월 2일
편집: Star Strider 2024년 9월 2일
Assign the output to a variable —
% 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);
% Result
ax2.XAxis.MinorTick = true
ax2 =
Axes with properties: XLim: [10 1000] YLim: [-1 1] XScale: 'log' YScale: 'linear' GridLineStyle: '-' Position: [0.4500 0.4500 0.4000 0.4000] Units: 'normalized' Use GET to show all properties
ax2_mtv = ax2.XAxis.MinorTickValues
ax2_mtv = 1x19
10 20 30 40 50 60 70 80 90 100 200 300 400 500 600 700 800 900 1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
For some reason, this is being inconsistent here. It appears to be necessary first to turn the minor ticks on, and second not to suppress (with at trailing semicolon ‘;’) the command that turns them on. I get the same behaviour in MATLAB Online. This behaviour may be a bug.
EDIT — Corrected typographical errors.
.
  댓글 수: 6
dpb
dpb 2024년 9월 2일
"... and I do not understand the reasons behind this behaviour..."
Buried in the innards of HG2; it attempts to minimize the number of screen refreshes to keep graphics response time as quick as possible.
I would hypothesize that the requested action of simply returning the values doesn't trigger a screen refresh because nothing has happened that will cause the graphics display to change so there is no need for a refresh at that point programmatically. Changing the focus to the command window, otoh, causes a refresh and then the internal data have been updated.
I tried the same thing as @Star Strider and got it to work the first time, but then it didn't later; only Mathworks would have the ability to actually explain explicitly why...

댓글을 달려면 로그인하십시오.


dpb
dpb 2024년 9월 2일
편집: dpb 2024년 9월 2일
@Gayathri recognized the underlying problem which can be very confusing because it is so erratic in what does/does not get updated there's no predicting what operation(s) require it -- and it's rare enough it is almost never on one's radar unless one just happens to "get bit" a second time recently enough after having had a first occurrence.
However, for complicated handling of such graphics, I would strongly recommend keeping a better track of object handles; my first thought was that since the code wasn't encapsulated in a function the axes handles were getting mixed up and you were accessing another axis in the command window, but that alone didn't always solve the problem; it worked once and like @Star Strider's thoughts, thought had it solved, but then a second run after adding only some additional comments, not code, it failed again and couldn't get it to work correctly even reverting back. It's truly a mind-boggling symptom when it does occur.
Anyways, I'd suggest something more on the order of
% 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
% 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';
drawnow
hAx2.XAxis.MinorTickValues
ans = 1x19
10 20 30 40 50 60 70 80 90 100 200 300 400 500 600 700 800 900 1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now you have access to everything specifically and are sure are talking to the right object and focus is where intend it to be...
  댓글 수: 5
dpb
dpb 2024년 9월 3일
Well, that depends... :) I agree @Gayathri was the first to identify the issue w/ the event queue not being updated and that drawnow solves the initial issue, so precedence goes there. Personally, whether or not I was the one to point it out and provide the other code, in cases of mixing multiple axes I think good practice is to always identify the actual objects...
Sim
Sim 2024년 9월 3일
편집: Sim 2024년 9월 3일
Yes, sorry, I need to give credit to @Gayathri for identifying the issue first. I am using your sligtly more sophisticated solution, even though the initial hint comes from @Gayathri........ I would really like to accept all the answers... I could, mabye, accept the @Gayathri answer, and ask if people could upvote your answer in order to reach 4 points each, or, viceversa... You all deserve credit!
Also the @Star Strider solution is cool :-)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by