MATLAB fail to create Chart with Multiple x-Axes and y-Axes

3 views (last 30 days)
Dear all, the objective was to two x-Axes and y-Axes. However, I am unable to produce the intended graph even follow the MATLAB Example. The 2 signals does not overlap into each other, instead one of the signal cover entirely the first signal that being plot.
I really appreciate if someone can point what I have been missing. Thanks in advance
The following code has been used but without success.
load('st_data.mat');
figure
errorbar(st_X_hour, st_Vec_Y1_mean, st_Vec_Y1_Sd, '--x','MarkerSize',15)
ax1 = gca; % current axes
set(ax1, 'Xtick', st_Xaxis_intrmit, 'XTickLabel',st_XLabel,'XAxisLocation','top'); xtickangle(90)
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,'XAxisLocation','top','YAxisLocation','right', 'Color','none');
plot(st_X_hour,st_Y2,'Parent',ax2,'Color','k')

Accepted Answer

Walter Roberson
Walter Roberson on 10 Oct 2017
Edited: Walter Roberson on 10 Oct 2017
When ax2 is created, by default its NextPlot property is set to 'replace'. When the next graphics operation is done (the plot call), the graphics system sees that replace is in effect, and does a cla(), which leaves position the same but happens to reset the Color property back to the default [1 1 1].
You can deal with this by,
ax2 = axes('Position', ax1_pos, 'XAxisLocation', 'top', 'YAxisLocation', 'right', 'Color', 'none', 'NextPlot', 'add');
or you can deal with it by
hold(ax2, 'on')
before doing the plot().
But do not just deal with it by
set(ax2, 'Color', 'none')
after doing the plot(), as the cla that was done on your behalf also reset the axis locations.
  3 Comments
Walter Roberson
Walter Roberson on 10 Oct 2017
I think it would be easier if you used plotyy
ax = plotyy(st_X_hour, st_Vec_Y1_mean, st_X_hour, st_Y2, @(x,y) errorbar(x, y, st_Vec_Y1_Sd, '--x', 'MarkerSize', 15), @(x,y) plot(x, y, 'k') );
set(ax(1), 'Xtick', st_Xaxis_intrmit, 'XTickLabel',st_XLabel);
xtickangle(ax(1), 90);
balandong
balandong on 11 Oct 2017
Hi Walter, Thank you for the really excellent suggestion, never knew about the existence of PLOTYY. I thought only YYAXIS right & left have such capability.
I know this question beyond the original scope of this thread, but im tempted to know how can access the second Axes? I tried ax(2), but nothing is changing! FYI, I want to change the label at the bottom X-axis into number, such that
ax = plotyy(st_X_hour, st_Vec_Y1_mean, st_X_hour, st_Y2, @(x,y) errorbar(x, y, st_Vec_Y1_Sd, '--x', 'MarkerSize', 15), @(x,y) plot(x, y, 'k') );
set(ax(1), 'Xtick', st_Xaxis_intrmit, 'XTickLabel',st_XLabel,'XAxisLocation','top');
xtickangle(ax(1), 90);
ylim(ax(1),[0.5 5.5])
nox=273:20:400;
set(ax(2), 'Xtick', nox, 'XTickLabel',1:numel(nox),'XAxisLocation','bottom');
However, running the above code changing nothing!.
Similarly, I want to change the interval of thicklabel at the second Y-axis. I think the syntax should be the same.
Thanks in advance.
ps- I still wonder how you able to entertain so many MATLAB related queries in a day. I am still amazed from your matlab activity log

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!