MATLAB fail to create Chart with Multiple x-Axes and y-Axes
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
balandong
am 10 Okt. 2017
Kommentiert: balandong
am 11 Okt. 2017
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')
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 10 Okt. 2017
Bearbeitet: Walter Roberson
am 10 Okt. 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 Kommentare
Walter Roberson
am 10 Okt. 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);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!