Where to format plot?

3 次查看(过去 30 天)
Viesturs Veckalns
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