Where to format plot?

18 views (last 30 days)
Viesturs Veckalns
Viesturs Veckalns on 8 Nov 2016
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.

Answers (1)

Nick Counts
Nick Counts on 8 Nov 2016
Edited: Nick Counts on 8 Nov 2016
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 Comments
Viesturs Veckalns
Viesturs Veckalns on 8 Nov 2016
I discovered I can write ax.YScale = 'log'; and use plot instead of semilogy.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!