This code gives me a strange plot I cannot figure why. Many thanks for any help
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 14 de En. de 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.
Respuesta aceptada
dpb
el 16 de En. de 2016
Editada: dpb
el 17 de En. de 2016
" I was unable to get this subplot axis problem fixed"
Don't know why it didn't register with me before, but the problem is
axis('square')
That's changing the axes shape only for the one, last, active axes object and there are two owing to plotyy
The solution is
h1=subplot(2,1,1);
hAx=plotyy(....
axis(hAx,'square')
which will fixup both. The obvious problem is that the one is so short compared to the other; I let the missing t sidetrack (albeit it was a problem in the original post that would lead to confusing plot as well).
My previous example expanded results in --
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172515/image.jpeg)
from
>> subplot(2,1,1)
>> hAx1=plotyy(1:10,rand(1,10),1:10,10*rand(1,10));
>> axis square
>> subplot(2,1,2)
>> hAx2=plotyy(1:10,rand(1,10),1:10,10*rand(1,10));
>> axis(hAx2,'square')
Note don't even need the subplot axes handles (altho it can't hurt to be specific); just must modify both plotyy axes to keep them in synchronicity. linkaxes might help here altho not sure that it will set the 'position' property; didn't test that.
2 comentarios
dpb
el 18 de En. de 2016
Editada: dpb
el 18 de En. de 2016
subplot(2,1,1);
hAx1=plotyy(t,z3,t,z2);
axis square;
subplot(2,1,1)
hAx2=plot(t,z3,t,z2);
axis square;
What are you doing???!!! You create the subplot, then plot into it with plotyy and again do what I just demonstrated to NOT do -- use the command form of axis without the two axes handles. That's what got you in trouble the first time. Then you do it over again.
What do you want in the plot? The above puts two plots in the upper two of the two subplots created, but the second one writes over the first.
Did you mean
subplot(2,1,1);
hAx1=plotyy(t,z3,t,z2);
axis(hAx1,'square')
% rest of stuff for top plot goes here
...
% followed by
hAx2=subplot(2,1,2); % NB: the trailing '2'
hL2=plot(t,z3,t,z2); % NB: plot() returns line handles, not axes
axis square % Can get away here as *plot is only is one axis
grid on;
% finish up on the lower plot here
...
instead, mayhaps?
It's really not that complicated; the only tricky part() is that there are two axes for *plotyy and so you have to do the same thing to each of them. That's what I demonstrated before that when applied the command form of axis to the upper plot that gives you the two with differing aspect ratios whereas the bottom did both by use the function form of axis with the proper axis handles.
Also, as noted in the comment, plot returns (an array of) line handle(s), not axes handles. Hence you really should keep the handle returned by subplot for further manipulations on it should be desired altho you can use the subplot function in its referencing form rather than creating one.
() The only reason this thread turned out so long is that (as sometimes happens) Walter and I both missed the fundamental issue initially and went down a fruitless path. *axis square isn't used a lot so it just didn't register for either of us until later on it finally dawned on me what caused the symptom your first attached plot showed.
After that, it was straightforward application of the functional form for it that accepts the axes handles so operate on both simultaneously to keep them in synch. That's what my revised example demonstrated; the first, top, subplot is deliberately wrong to illustrate the problem whereas the lower is correct for using plotyy and the 'square' aspect ratio. When using plot instead of plotyy then, as documented --
- There's only a single axis for the lines to be plotted into, and
- It (PLOT, that is) returns line handles instead of axes handles.
ADDENDUM
I earlier mentioned linkaxes as perhaps letting you "get away with" less explicit effort in the actual modifications to the x-axes. Just out of curiosity I did the 'spearmint -- as I had surmised it doesn't solve the aspect ratio problem. That is,
subplot(2,1,1)
hAx=plotyy(...
linkaxes(hAx,'x')
axis square
leaves you with the one axes square, the other default as I surmised would likely be so.
Más respuestas (4)
Walter Roberson
el 15 de En. de 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 comentarios
Walter Roberson
el 16 de En. de 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.
dpb
el 15 de En. de 2016
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"
0 comentarios
Don
el 18 de En. de 2016
5 comentarios
dpb
el 20 de En. de 2016
Editada: dpb
el 20 de En. de 2016
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181740/image.jpeg)
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
el 21 de En. de 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".
Ver también
Categorías
Más información sobre Two y-axis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!