Autoscale axis by a factor of 1000 around the minimum and maximum values of two data sets: Problem with all positive or all negative min/max values
2 views (last 30 days)
I have spent a few hours thinking about how to do this, and can't find a method that works.
Let's say I have 2 vectors that I want to fit, and then plot the fitline. Matlab stops graphing the fitline at the axis limits, so if one wants the fit to continue beyond the axis limits, he has to set those axis limits himself before plotting the fit.
I am using this command to autoscale back to an optimal view of the data points after the fits are plotted:
axis([LPData.xlimits(1,1) LPData.xlimits(1,2) LPData.ylimits(1,1) LPData.ylimits(1,2)])
Where LPData is just the structure used to exchange data between the several functions I use overall. Not relevant.
The limits are created using these commands. They are meant to leave a 5% overhang on all sides of the data points.
LPData.xlimits=([min(min(LPData.matx)),max(max(LPData.matx))] + [-1,1]*.05*range(LPData.matx,'all'));
LPData.ylimits=([min(min(LPData.maty)),max(max(LPData.maty))] + [-1,1]*.05*range(LPData.maty,'all'));
So, now to the actual problem:
Say I have a minimum of 0.42 and a maximum of 4.2, in both x and y for simplicity
I would then want the axis before the fits are plotted to be set to [-42 420 -42 420].
I don't know how to get matlab to go by a factor (like 1000) into away from the data set in both directions.
axis([LPData.xlimits(1,1)*1000 LPData.xlimits(1,2)*1000 LPData.ylimits(1,1)*1000 LPData.ylimits(1,2)*1000])
This would work if the lower limits of both sets are negative and the upper limits are positive. But it doesnt work if they are both either positive or negative, as the lower limit will be behind the highest plotted value if all values are positive (say we have a lower limit of 4.2, 4.2*1000=4200, so the data point at 4,2 isn't plotted) or negative (same thing other way around).
I can't find a method that covers all cases, and frankly don't really know to proceed. I think it is possible, I just don't know how.
Thank you & stay healthy