Where to format plot?
3 次查看(过去 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!