Controlling dual axis plots

4 views (last 30 days)
Samuel
Samuel on 26 Mar 2013
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
  1 Comment
Samuel
Samuel on 27 Mar 2013
Hmm I guess not many people have encountered this issue? hmm

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 27 Mar 2013
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');
  2 Comments
Samuel
Samuel on 28 Mar 2013
Perfect answer. Your answer takes care of my problem involving plotyy and multiple axes better than matlab help files.
Thanks very much.
Samuel
Samuel on 31 Mar 2013
Hey
minor question- if i wanted to plot additional data to the right axis as well and update the legend so it contains the data, what should i do? i tried using the command hold to ax(2) but it doesnt show it in the legend and also the grid seems to also get messed up. thanks in addition to ur help in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Two y-axis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!