How can I set good Axis Limits manually?

11 Ansichten (letzte 30 Tage)
Katy Weihrich
Katy Weihrich am 21 Nov. 2022
I am creating some figures that require me to set the y-axis limits manually.
There are some factors that are important to me:
1) I would like to be able to identify the closest possible ticks that enable me to plot all of the data (e.g. in the case of ylim([... 600]) would be okay for max = 599.98, but not for max = 600.12)
2) It needs to handle data that varies widely in its range (e.g. preferred Outputs could be: xlim([0.5 0.7]); xlim(14 17]); xlim(100 145]); xlim(460 570]); ect...).
3) The ticks should be placed at "rounded" points (e.g. if the data min = 104.34 & max = 143.99; then (i think) a tick distance of iOP_tick = 5 placed at xx0 or xx5 places would be good --> xlim(100 145]) instead of xlim(104 144]))
So far I have the code below. It is functional, however, this seems very bit unflexible and I can only get to the right thresholds for iOP_tickdiff by trial and error (the above ones have not been checked yet if they make sense). I feel like I will run into annoying problems in a few months if I want to apply a different dataset and my "optimal" turn out not to be so "optimal" after all. For example, this code cannot handle negative numbers at all.
%% Inputs:
% OP_Axislimits --> Data Minimum and Maximum (e.g.: OP_Axislimits = [163.65; 467.34])
%% Data Wrangling:
% iOP_tickdiff --> "optimal" distance between ticks on the Axis
% i_possticks --> possible ticks for the axis
% iml --> index (/location) of the possible tick closest to OP_Axislimits
% ilocin/ax --> iml adjusted to not overlapp with the data
%% Output:
% i_ticks --> "optimal" ticks for the axis
% i_tickmin/ax --> "optimal" axis limits
%% Code:
i_axesdiff = OP_Axislimits(2,1) -OP_Axislimits(1,1);
iOP_tickdiff = nan;
if i_axesdiff <= 1; iOP_tickdiff = 0.1; end
if (i_axesdiff> 1) && (i_axesdiff<= 10); iOP_tickdiff = 1; end
if (i_axesdiff> 10) && (i_axesdiff<=100); iOP_tickdiff = 5; end
if (i_axesdiff>100) && (i_axesdiff<=200); iOP_tickdiff = 10; end
if (i_axesdiff>200) && (i_axesdiff<=300); iOP_tickdiff = 15; end
if i_axesdiff >300; iOP_tickdiff = 20; end
i_possticks = 0:iOP_tickdiff:OP_Axislimits(2,1)+iOP_tickdiff;
[~,iml] = min(abs(i_possticks-OP_Axislimits(1,1)));
if OP_Axislimits(1,1) < i_possticks(iml)
i_tickmin = i_possticks(iml-1); ilocin = iml-1; else
i_tickmin = i_possticks(iml); ilocin = iml; end
[~,iml] = min(abs(i_possticks-OP_Axislimits(2,1)));
if OP_Axislimits(2,1) > i_possticks(iml)
i_tickmax = i_possticks(iml+1); ilocax = iml+1; else
i_tickmax = i_possticks(iml); ilocax = iml; end
i_ticks = i_possticks(ilocin:ilocax);
%% Application:
xlim([i_tickmin i_tickmax])
xticks(i_ticks)
Is there a more standardized way to do this? Does Matlab have a function that can help, or is there a way to improve this code by considering proportions? I had a look around and could not find something and I thought I would ask. Am I just overlooking a very easy solution?
PS: After writing the whole question down, I think the main issue is finding a way to automatically select the best iOP_tickdiff values. Is there a guide or formula that I could use for it?

Akzeptierte Antwort

sai charan sampara
sai charan sampara am 22 Sep. 2023
Hello,
I understand that you are trying to use the variable “iOP_tickdiff” to represent the spacing of elements in “i_possticks” array and from that generate suitable limits and ticks for the plot. There is no built-in function in MATLAB to exactly achieve this, but to address this issue, you can use the following code:
N=20;
i_tickmax=5*(floor(OP_Axislimits(2,1)/5)+1);
i_tickmin=5*(floor(OP_Axislimits(1,1)/5));
iOP_tickdiff=max(0.1,(i_tickmax-i_tickmin)/N);
iOP_tickdiff=min(20,iOP_tickdiff);
i_ticks=i_tickmin:iOP_tickdiff:i_tickmax;
Here “i_tickmax” and “i_tickmin” are the nearest rounded upper and lower bounds of the x data. N is the number of points in the “i_possticks” array and can be a design choice. From the code in the question, it seems there are nearly 20 points in the “i_possticks” array. Hence, N can be assigned as 20. In this way the closest possible ticks can be obtained, and data of any range can be handled. This works for both positive and negative limits. The points are rounded but are not always integer values.
There is a property called “XTickMode” of the axis data type of plots which can be set to “auto” to automatically generate the approximate x ticks. You can refer to the below documentation to learn more about the axis property of plots.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by