This code gives me a strange plot I cannot figure why. Many thanks for any help
Show older comments
figure(2);
start=floor(TrajX(1));
stop=floor(TrajX(length(TrajX)));
P3_FzInv = flip(P3_Fz*-1);
subplot(2,1,1); % top plot, P3Fz v. T4T3
[ax,z1,z2]=plotyy(t(start:stop),z1(start:stop),(start:stop),z2(start:stop));
axis('square');
grid on;
set(z1,'LineWidth',2);
set(z2,'LineWidth',2);
xlabel(ax(1),'seconds');
ylabel(ax(1),'Fz <<--- Power Site --->> P3');
ylabel(ax(2),'T3 <<--- Power Site --->> T4');
hold on;
FigHandle = figure(2);
set(FigHandle, 'Position', [100, 100, 1049, 895])
3 Comments
Don
on 14 Jan 2016
dpb
on 14 Jan 2016
Well, we certainly can't tell what might be "strange" with no description whatsoever of what you see or expect and no data with which to recreate it.
At a minimum you'll have to attach the figure and comment upon it; better would be to make a runnable sample including some data that illustrates the problem.
Accepted Answer
More Answers (4)
Walter Roberson
on 15 Jan 2016
plotyy() works by creating a second axes, and so it is not compatible with using subplot(). subplot() works by creating axes to plot into, not by subdividing the figure into sections.
You can pass an axes or axes pair into subplot(); see http://www.mathworks.com/help/matlab/ref/plotyy.html . Notice though that even if you pass an axes pair, it will only pay attention to the first of the two axes and will generate its own axes for the second plot.
Work around:
ax1 = subplot(2,1,1);
[ax,p1,p2] = plotyy(ax1, t(start:stop),z1(start:stop),t(start:stop),z2(start:stop));
set(ax(2), 'Position', get(ax(1), 'Position'))
7 Comments
dpb
on 15 Jan 2016
Edited: Walter Roberson
on 15 Jan 2016
[ax,h3,h2]=plotyy(t(start:stop),z3(start:stop),t(start:stop),z2(start:stop));
That's not quite what Walter suggestd, Don. He used
[ax,h3,h2]=plotyy(ax1,t(start:stop),z3(start:stop), ...
t(start:stop),z2(start:stop));
where ax1 is the handle of the axes from the subplot call.
I did the following here and didn't see any issue w/ the size of the axes...
>> ax1=subplot(2,1,1);
>> hAx=plotyy(ax1,1:10,rand(1,10),1:10,10*rand(1,10));

dpb
on 16 Jan 2016
Thx for the fixup, Walter...
Don
on 16 Jan 2016
Star Strider
on 16 Jan 2016
You can Vote by looking for the ‘Thumbs up’ icon just under Walter’s raccoon picture.
Walter Roberson
on 16 Jan 2016
Nyctereutes procyonoides viverrinus ("Tanuki", "Japanese Racoon-dog") artistic representation to be more accurate. It is a Canidae (canine), as opposed to racoons which are Procyonidae.
Don
on 17 Jan 2016
dpb
on 15 Jan 2016
1 vote
I noticed the following this AM I missed yesterday --
You're missing the t variable for the x-vector on the RH axes in the plotyy call so that'll plot versus the indices array instead of t. That will, indeed, "look funny"
Don
on 18 Jan 2016
0 votes
5 Comments
Really belongs as another comment to the previous answer rather than an Answer itself, but that aside, it's only that it kinda' breaks up the ordering.
What need is to see the actual code that you used to create the final version to see what you did, precisely, as noted in previous comment the code you posted couldn't have generated that attached plot and we don't know what variables you used or saved.
But, presuming from the above image it's the two lines in the upper subplot you're speaking of, then you needed to have saved the line handles as well as the axes handles from the plotyy call(*) to make changes most simply to the drawn lines; you can always go "handle-diving" and retrieve whatever is in the figure/axis of question but there's rarely a need if you keep the info while building the plot.
That would mean the plotyy call would look something like
[hAx1,hL1,hL2]=plotyy(....
Since you only drew a line on each of the two axes (as opposed to mixture of, say, bar patches and lines or other objects) and you want a constant new width on them, it's only an additional call
set([hL1 hL2],'linewidth',2)
Done! There are methods for later releases than R2012b that don't use set but I don't believe they handle the vector of handles form.
As for the "How To", the plotyy doc says
[AX,H1,H2] = plotyy(...) returns the handles of the two axes created in AX and
the handles of the graphics objects from each plot in H1 and H2.
AX(1) is the left axes and AX(2) is the right axes.
This is really all the information you need from plotyy; everything else follows from the general behavior of handle graphics which, admittedly, is a large subject with which to become familiar. One simply has to invest some time in learning how HG works as a cost of becoming more adept with Matlab.
The first thing in thinking of how to go about making modifications is the consideration of which properties belong to which objects. In this case, it should be clear (I'd hope) that the line width will be a property of the drawn lines, not that of the parent axis. That and perhaps even more obviously that you used plot should give you the clue to go look at what plot documentation says and that the return value is
h = plot(...) returns a column vector of handles to lineseries objects,
one handle per line.
which gives you the information that the plotyy call does need the optional additional return values for you to use later. Hopefully that gives you kind of a road map for how to think through the process that will help going forward.
Optionally, there's the "trick" of using an anonymous function whose handle can be passed to plotyy but that holds the additional information of other properties you'd like to set but can't pass via the expanded argument list facility of plot.
() And, to be completely clear, the "line handles" referred to aren't really the outputs of *plotyy itself; it is just a wrapper around the two plotting routines ( plot by default, others by passing explicitly) which actually do the work after plotyy creates the two axes. Consequently what the two alternate returns contain is dependent entirely on what those functions themselves return. Hence, also, that the documentation for plotyy cannot contain the information on what is contained in those return values for any given call; it depends on the invocation as to which routines are used and so the earlier comment of having to follow the trail of bread crumbs to, in this case, plot (but it could've been bar or any other 2D routine).
ADDENDUM
One more refinement to your plot I'd suggest (and take the opportunity yet again to nag TMW about not having fixed it even yet after, like, 30 years! :( ) -- note the tick labels don't have the same precision for the integer values and that just looks tacky. It's pretty simple to fix, albeit it shouldn't be necessary if TMW would just clean up their act:
For the upper subplot presuming the axes handle array is hAx1
yt=get(hAx1,'ytick'); % return tick values y-axes; will be cell array
set(hAx1,{'yticklabel'},{cellfun(@(y) num2str(y.','%0.1f'),yt,'uniform',0)})
For the lower since there's only the one axes, same idea except don't have a cell array, only a simple one--
yt=get(hAx2,'ytick'); % subplot(2,1,2) ytick values
set(hAx2,'yticklabel',num2str(yt.','%0.1f'))
NB: I used hAx2 for the lower handle, obviously use whatever you used for the variable name in your code. These details are why it would help to post your actual code...
The upper subplot example I gave before now looks as below after the fixup--isn't that much nicer???? :) Again, why, oh why, TMW can't you do this internally???!!!

Note also that if one wants to eliminate the temporary array yt one can fold the get into the set...
set(hAx1,{'yticklabel'}, ...
{cellfun(@(y) num2str(y.','%0.1f'),get(hAx1,'ytick'),'uniform',0)})
One starts writing (and testing) stuff like this from the inside out, getting the first steps right then encapsulating them into the next higher-level step.
I don't see how you could possibly have gotten the attached figure from the above code again...at a bare minimum there are only two ylabel calls and there are three labels on the figure.
It doesn't seem that you're paying any real attention to the code and help you're being given. Again, you've still got the first subplot call followed by two successive plotyy calls, the first of which again is followed by the command form of the axis square command so that'll have screwed up the aspect ratio yet again, as well as not having saved the second set of line handles. The second plotyy call is identically the same data and will consequently overwrite the first. I don't know what the second figure is; you don't show code that would have generated it at all.
The result of the first series up through axis square is similar to the following plot excepting for the random data here instead of yours.

on which you'll note only one axis ratio changed and also only one line width has been changed since you only saved the one handle array, not both sets. The second plotyy overwrites the first LH axes since the positions are not the same after having changed the aspect ratio but deletes the second with identically the same position values, thereby destroying the first RH axes hAx1(2). To see this,
>> subplot(2,1,1)
>> [hAx1,hL1]=plotyy(0:10,rand(1,11),0:10,10*rand(1,11));
>> axis square
>> gca % which is left as current axis?
ans =
173.0385
>> hAx1 % first plotyy axes handles
hAx1 =
173.0385 175.0385
>> % Now introduce second PLOTYY() call...
>> [hAx2,hL3,hL2]=plotyy(0:10,rand(1,11),0:10,10*rand(1,11));
>> hAx2 % and second plotyy axes handles
hAx2 =
173.0385 175.0386
>>
Note that the first (LH) axis is same handle, RH isn't and in fact the RH axes from the first plotyy call has been destroyed...the second is no longer valid as shown
>> ishandle(hAx1)
ans =
1 0
>>
The sequence as I've outlined before should look something like --
subplot(2,1,1)
[hAx1,hL1,hL2]=plotyy(... % top two-line plot here...
set([hL1 hL2],'linewidth',2) % fixup linewidths
% put rest of annotation, labels, etc, here on hAx1(1), hAx1(2)
....
% lower half (square) plot...
hAx2=subplot(2,1,2) % create lower axes, save handle
hL=plot(... % data for the square axes
set(hL,'linewidth',2)
axis(hAx2,'square')
% rest of labelling, etc., goes here
...
It's really not complicated...
dpb
on 21 Jan 2016
As a note, for the lower plot since you're not using plotyy you can simplify a little further using the ability of plot for named parameter pairs...
% lower half (square) plot...
hAx2=subplot(2,1,2) % create lower axes, save handle
hL=plot(... ,'linewidth',2); % can set the style/width, etc.
axis(hAx2,'square')
% rest of labelling, etc., goes here
...
Hopefully it makes more sense now and that it isn't any deep mystery any longer but a logical sequence of steps.
It wouldn't have been difficult at all if either Walter or I had recognized the first go-'round the issue was entirely owing to axis and the multiple handles and not thrown in the red-herring of which set of axes were plotting into. It's documented behavior in subplot regarding the destruction of axes that are overlaid by the creation of new one(s) that overlay their position while an exact position match makes that existing axes the new target; hence the behavior illustrated above with the two successive plotyy calls of still ending up with only two valid handles out of the four which had been "created".
Categories
Find more on Two y-axis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
