0
votes

I want to add objects to my matlab plots which have defined x limits but span the whole y range. Examples are vertical lines or shaded regions delimited by two x values. I am aware of the option to use the current plot limits like this:

plot(1:10)
yl = ylim();
% Use y limits of current plot as y values
patch([ 3 3 5 5 ],[ yl(1) yl(2) yl(2) yl(1) ], 'red');

enter image description here

However I want my users to be able to increase plot y limits afterwards (e.g. to synchronize limits of multiple plots) and also want them to continue profiting from Matlab's automatic setting of plot limits.

This would be archivable if I would use the following code to set the y coordinates of my objects to the largest and smallest possible integers, respectively (intmax() and intmin() in Matlab) and tell Matlab not to consider that object during calculation of plot limits.

plot(1:10)
% Make graphical object which spans the whole possibly y range
p = patch([ 3 3 5 5 ],[ intmin intmax intmax intmin ], 'red');
% Does something like the following function exist?
exemptFromPlotLimitsCalculation(p)

Is this possible in Matlab?

2

2 Answers

1
votes

You could plot the patch (or fill) really large (for example by using realmax) and exclude it from rescaling by setting the property YLimInclude to off

patch([3 3 5 5], realmax*[ -1 1 1 -1], 'red', 'YLimInclude', 'off');
1
votes

have a look at this

In the postActionCallback you can resize your patch