Controlling dual axis plots
6 次查看(过去 30 天)
显示 更早的评论
Hello all, I have been using a plot involving two y axis plot for a common x axis(just the same units, not the same dataset). I couldn't use plotyy for this because this involves three sets of data for one of the axis(it seems plotyy only works for one set for each axis). Now, I plotted the sets of data as per the steps defined in the help file:
grid on
hl1=line(time,V1,'Color','r');
hl2=line(time,V2,'Color','g');
hl3=line(time,V3,'Color','c');
legend(['one'],['two'],['three']);
ax1=gca;
set(ax1,'XColor','k','YColor','k');
xlabel('time(s)');ylabel('Voltage (V)');
ax2=axes('Position',get(ax1,'Position'),'XaxisLocation',...
'bottom','YaxisLocation','right','Color','none',...
'Xcolor','k','Ycolor','k');
hl2=line(dtime,resistance,'Color','k','Parent',ax2);
legend(['Piezo Sensor'],'location','SouthEast');
title(['Total Voltage summary and piezo response vs time']);
xlabel('time(s)');ylabel('resistance');
First issue is that two legend boxes appear- the left is for axes for the left, and the other for the right. I would like to have them be combined in the same legend box. If they cannot be, I would at least be able to move them- I cannot change the position of the legend box appropriated for the left side.
Second issue is that when I zoom into a certain portion in the graph, I can only zoom into the graph plot in the right axis (not the entire thing).
It seems all of the problems I have been having is due to the code assigning full control to the right side axis. I would like to see and control the plot as one entity, in terms of zooming and legend boxes.
thanks for your help in advance.
sam
采纳的回答
Kelly Kearney
2013-3-27
To put all the lines in one legend, pass the line handles directly to legend (rather than the axis handle, which is the default when you don't specify).
First, make sure all your lines have unique handles (in your example, you use hl2 twice), then replace the two legend calls with
legend([hl1 hl2 hl3 hl4], {'one', 'two', 'three', 'Piezo Sensor'});
To link the zooming, use linkaxes
linkaxes([ax1 ax2]);
Also, you should be able to use plotyy, which will take care of the linking without you having to manually specify it. If your three V variables are vectors, then you can do it in one call:
[ax, ln1, ln2] = plotyy(time, [V1 V2 V3], dtime, resistance);
legend([ln1; ln2], {'one', 'two', 'three', 'Piezo Sensor'});
If they are matrices, or for some other reason they can't be concatenated for plotting, you can add use plotyy once and add the additional lines later:
[ax, ln1, ln2] = plotyy(time, V1, dtime, resistance);
hold(ax(1), 'on');
plot(ax(1), time, V2, 'r');
plot(ax(1), time, V3, 'm');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Two y-axis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!