How to get the values of the XAxis Minor Ticks (of an inset plot)
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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?
0 comentarios
Respuestas (3)
Gayathri
el 2 de Sept. de 2024
Editada: Gayathri
el 2 de Sept. de 2024
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
el 2 de Sept. de 2024
Editada: Star Strider
el 2 de Sept. de 2024
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_mtv = ax2.XAxis.MinorTickValues
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 comentarios
dpb
el 2 de Sept. de 2024
"... 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
el 2 de Sept. de 2024
Editada: dpb
el 2 de Sept. de 2024
@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
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 comentarios
dpb
el 3 de Sept. de 2024
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...
Ver también
Categorías
Más información sobre Axis Labels en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!