How can I set good Axis Limits manually?

4 views (last 30 days)
Katy Weihrich
Katy Weihrich on 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?

Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!