Where to format plot?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a script (Matlab R2015b)
...
BLOCK1
...
for i = 0:100
...
figure(1)
ax = gca;
ax.XTick = (0, 1, 3);
ax.XTickLabel = {'one', 'two', 'three'};
plot(x, y);
...
end
...
BLOCK2
...
I would like to move the plot formatting (lines beginning with ax.) away from the for loop to BLOCK1 or BLOCK2, in order to reserve the for loop only for the actual calculations.
Unfortunately when I move formatting to BLOCK1 all formatting effects disappear or if I move formatting to BLOCK2 the program gets stuck.
By analogy if I created another for loop where I filled the same plot I would have to do the formatting again.
In short, how to format the plot at the beginning of the script and make that formatting stick.
댓글 수: 0
답변 (1개)
Nick Counts
2016년 11월 8일
편집: Nick Counts
2016년 11월 8일
look at the axis property
ax.NextPlot.
During axes creation, I believe this is set to 'replace' by default. When some of the higher level plotting functions are called (plot, scatter, and other's I'm sure) they trigger an axes reset
ax.reset
You should be able to avoid this by using
hold on
Alternatively, you could try to set the ax.NextPlot to a value like 'add' (see the documentation,) but the hold function is handling that for you under the hood, so in my opinion it's a little easier/clearer
Without seeing your code, it's impossible to tell why it's getting stuck when you move the formatting to BLOCK2
The following code works for me, with formatting both before and after the plot loop:
Example 1:
hfig = figure(1)
ax = gca;
ax.XTick = [1, 2, 3]
ax.XTickLabel = {'one', 'two', 'three'};
hold on
for i = 0:100
plot( [1:3], randi(100, 1, 3) );
end
Example 2:
hfig = figure(1)
ax = gca;
hold on
for i = 0:100
plot( [1:3], randi(100, 1, 3) );
end
ax.XTick = [1, 2, 3]
ax.XTickLabel = {'one', 'two', 'three'};
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!