plotyy and a third plot

1 次查看(过去 30 天)
Matlab2010
Matlab2010 2012-5-2
I get the feeling plotyy is most asked about function in matlab.
I can do a plotyy with two variables great:
function test_plotyy()
close all;
clc;
clear;
figure;
ts1 = timeseries();%ts1 > ts2
ts2 = timeseries();
for t = 1: 1000
thisT = now;
ts1 = addsample(ts1,'Data', 1000 + randn(1),'Time', thisT);
ts2 = addsample(ts2,'Data', 900 + randn(1),'Time', thisT);
ts3 = ts1 - ts2;
from = thisT - (10 / (24*60*60)); %just load 10s of data
ts1 = getsampleusingtime(ts1, from, thisT);
ts2 = getsampleusingtime(ts2, from, thisT);
ts3 = getsampleusingtime(ts3, from, thisT);
myTime = ts1.Time;
[AX,H1,H2] = plotyy(myTime,ts1.Data, myTime, ts3.Data);
set(get(AX(1),'Ylabel'),'String','\bf ts1 and ts2');
set(get(AX(2),'Ylabel'),'String','\bf ts3');
set(H1, 'LineWidth', 3, 'Color','g');
set(H2, 'LineWidth', 3, 'Color','r');
datetick(AX(1), 'x');
datetick(AX(2), 'x');
linkaxes(AX,'x');
grid on;
xlabel('');
str = {'ts1','ts2'};
legend([H1 H2], str, 'Location','Best');
drawnow;
pause(1);
end
end
This all works fine (with the exception that the axis colours dont match -- anyone know how to fix that?).
I then introduce a third variable, ts3 to be plotted:
function test_plotyy()
close all;
clc;
clear;
figure;
ts1 = timeseries();%ts1 > ts2
ts2 = timeseries();
for t = 1: 1000
thisT = now;
ts1 = addsample(ts1,'Data', 1000 + randn(1),'Time', thisT);
ts2 = addsample(ts2,'Data', 900 + randn(1),'Time', thisT);
ts3 = ts1 - ts2;
from = thisT - (10 / (24*60*60)); %just load 10s of data
ts1 = getsampleusingtime(ts1, from, thisT);
ts2 = getsampleusingtime(ts2, from, thisT);
ts3 = getsampleusingtime(ts3, from, thisT);
myTime = ts1.Time;
[AX,H1,H2] = plotyy(myTime,ts1.Data, myTime, ts3.Data);
hold(AX(1), 'on');
set(AX(1),'nextplot','add');
H3=plot(AX(1),myTime, ts2.Data, 'LineWidth', 3, 'Color','b');
%ylim(AX(1), [floor(min(ts2.Data)) ceil(max(ts1.Data))]);
hold(AX(1), 'off');
set(get(AX(1),'Ylabel'),'String','\bf ts1 and ts2');
set(get(AX(2),'Ylabel'),'String','\bf ts3');
%ylim(AX(2), [floor(min(ts3.Data)) ceil(max(ts3.Data))])
set(H1, 'LineWidth', 3, 'Color','g');
set(H2, 'LineWidth', 3, 'Color','r');
datetick(AX(1), 'x');
datetick(AX(2), 'x');
linkaxes(AX,'x');
grid on;
xlabel('');
str = {'ts1','ts2','ts3'};
legend([H1 H3 H2], str, 'Location','Best');
drawnow;
pause(1);
end
end
can I get this to work? Not at all. There are so many things going wrong, i'm not going to list them all. main ones are: 1. legends dont work 2. colors dont work 3. hold doesnt work 4. ylim doesnt work
...
SOOOO frustrating!!
any help very welcome!

回答(1 个)

Thomas
Thomas 2012-5-2
The reason your colors of the axis and your plot and legends dont go together is because you are changing the default color of the plot after using plotyy (i.e. plotyy plot y1 and y2 axis using the default colors blue and green and then you set the colors of the plots to 'g' and 'r')
Change your code in the first example to :
set(H1, 'LineWidth', 3);
set(H2, 'LineWidth', 3);
************************
In the second example you are trying to plot multiple yy data ont he same axis and plotyy is usually never happy with that since you have different y limits ( you re trying 3, so you can plot the ts1 and ts2 together that have limits between 900 and 1000 on left y axis and the ts3 with limits around 100 on right y axis
modified code as follows: ts1 = timeseries();%ts1 > ts2 ts2 = timeseries();
for t = 1: 1000
thisT = now;
ts1 = addsample(ts1,'Data', 1000 + randn(1),'Time', thisT);
ts2 = addsample(ts2,'Data', 900 + randn(1),'Time', thisT);
ts3 = ts1 - ts2;
from = thisT - (10 / (24*60*60)); %just load 10s of data
ts1 = getsampleusingtime(ts1, from, thisT);
ts2 = getsampleusingtime(ts2, from, thisT);
ts3 = getsampleusingtime(ts3, from, thisT);
ts1andts2.Data=[ts1.Data,ts2.Data]; % concatenate ts1 and ts2
myTime = ts1.Time;
[AX,H1,H2] = plotyy(myTime,ts1andts2.Data, myTime, ts3.Data);
set(get(AX(1),'Ylabel'),'String','\bf ts1 and ts2');
set(get(AX(2),'Ylabel'),'String','\bf ts3');
%ylim(AX(2), [floor(min(ts3.Data)) ceil(max(ts3.Data))])
set(H1, 'LineWidth', 3);
set(H2, 'LineWidth', 3);
datetick(AX(1), 'x');
datetick(AX(2), 'x');
linkaxes(AX,'x');
grid on;
xlabel('');
str = {'ts1','ts2','ts3'};
% legend([H1 H3 H2], str, 'Location','Best');
legend(str)
drawnow;
pause(1);
end

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by