is matlab automatically updating xlim and ylim?
5 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1712536/image.jpeg)
The code to put the text at the wanted location is as follows:
XL=xlim; YL=ylim
% str = strcat("E_{ex}=",num2str(exact_energy),",E_{var}=",num2str(var_energy),",overlap=", num2str(overlap),", relative error=",num2str(relative_error));
% title(str);
str = strcat('$(',labels(sw1),'1) ',' U=',num2str(g),', M=1$');
text(aa*(XL(2)-XL(1))+XL(1),bb*(YL(2)-YL(1))+YL(1),...
str,'fontsize',16 , 'Interpreter','latex');
set(gca,'fontsize',myfont)
The idea is to first take the ylim and then put a string at the wanted location. However, in many times, the string is not located as desired. It seems that the ylim is changed by matlab later.
What is the solution?
3 个评论
Aquatris
2024-6-10
What exactly do you mean by the text is not positioned in the desired location? Is the X and Y position of the text change after you do subplot? Can you provide the script?
回答(1 个)
Steven Lord
2024-6-10
If you want to prevent MATLAB from automatically updating the axes limits when you add new data to the axes, set the XLimMode, YLimMode, and/or ZLimMode properties of the axes to 'manual'. The easiest way to do this is to set the axes limits manually using the xlim function (and similarly ylim and zlim for the Y and Z axes respectively.)
However, from the apperance of your second plot I'm guessing you're using the yyaxis function. I'm not 100% certain but if I recall correctly that function tries to do some amount of synchronization of the limits of the axes ticks (which may require control over the limits on the two axes it creates) so if you're trying to put some text "in the upper-left corner" or something similar you probably want to create and plot into both axes, get the limits (using ylim to query rather than to set) of the left axes (you'll need to use yyaxis left before calling ylim), then calculate the text coordinates on that axes from those limits and call text.
Alternately, if putting the text above the axes is an option, just call the title function. Or since you're using release R2023b, you could use the ysecondarylabel function. The figures displayed in the ysecondarylabel function examples look pretty similar to what you've shown.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!