필터 지우기
필터 지우기

Where to format plot?

조회 수: 5 (최근 30일)
Viesturs Veckalns
Viesturs Veckalns 2016년 11월 8일
댓글: Viesturs Veckalns 2016년 11월 8일
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.

답변 (1개)

Nick Counts
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'};
  댓글 수: 2
Viesturs Veckalns
Viesturs Veckalns 2016년 11월 8일
Using hold on fixes the problem in case of plot, but if I use semilogy instead, I lose the logarithmic scale for the y axis.
Viesturs Veckalns
Viesturs Veckalns 2016년 11월 8일
I discovered I can write ax.YScale = 'log'; and use plot instead of semilogy.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by