Given two vectors of the same length is there a way to determine the axes ranges that would result if the data were plotted without plotting the data?
I am looking to update xlim and ylim manually after the plotted data is changed* but with out relying on the automatic resizing as there are other things plotted that I dont want to be used in determining the plot ranges. Also I would rather not redraw the entire figure with just the modified data then redraw the larger thing.
(plotted data is changed*: I am manipulating the XData and YData parameter of plotted data directly with a handle.)

 채택된 답변

Adam Danz
Adam Danz 2019년 8월 21일
편집: Adam Danz 2019년 8월 21일

0 개 추천

If vector1 and vector1 are the x and y values to be plotted, the xlim and ylim for a tight axis would be,
xlim([min(vector1), max(vector1)]);
ylim([min(vector2), max(vector2)]);
If you'd like to access those values from the line object handle 'h',
xlim([min(h.XData), max(h.XData)]);
ylim([min(h.YData), max(h.YData)]);
If you'd like to add 5% of the data range on each side so your max and min data points are not on the axis edges,
xlim([min(x),max(x)] + [-1,1]*.05*range(x))
ylim([min(y),max(y)] + [-1,1]*.05*range(y))

댓글 수: 4

Chris
Chris 2019년 8월 21일
편집: Chris 2019년 8월 21일
Tight axes will look bad for this application,
By default ML does some nice rounding/flooring/ceiling-ing/ to get good tickmarks ranges with some buffer around the edges of the plot. I am hoping to precalculate something like what axis returns below.
>> xx = [-0.01, 1, 2.2]; yy = [0.2 3.01, 2];
>> plot(xx,yy)
>> axis
ans =
-0.5 2.5 0 3.5
edit:
I think this might work
>> aa = [min(xx), max(xx)] + [-1,1] .* mean(diff(get(gca, 'xtick')))
aa =
-0.51 2.7
>> bb = min(diff((get(gca, 'xtick')')))
bb =
0.5
>> round(aa./bb)*bb
ans =
-0.5 2.5
Was hoping there was a direct way. Above looks to work with larger magnitudes of data. thanks for the help.
Adam Danz
Adam Danz 2019년 8월 21일
편집: Adam Danz 2019년 8월 21일
The 3rd block of code in my answer will add a buffer around your data so the axes are not "tight". If you'd like to take that a step further and make sure the axis limits begin and end at a certain interval (like multiples of 0.5) you easily round those limits.
Here's a demo.
int = 0.5;
xl = [floor(min(x)/int)*int,ceil(max(x)/int)*int];
yl = [floor(min(y)/int)*int,ceil(max(y)/int)*int];
xlim(xl)
ylim(yl)
Adam Danz
Adam Danz 2019년 8월 21일
편집: Adam Danz 2019년 8월 22일
Your proposal requires that an axis already exists. If you want to base "int" from my solution on the axis tick interval,
int = mean(diff(get(gca,'xtick')));
xl = [floor(min(x)/int)*int,ceil(max(x)/int)*int];
Chris
Chris 2019년 8월 23일
yep good edit. initilization wont be a problem and the changed data points will be of the same magnitude so reusing tick should work.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 8월 21일

댓글:

2019년 8월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by