This code gives me a strange plot I cannot figure why. Many thanks for any help

3 Ansichten (letzte 30 Tage)
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 Kommentare
dpb
dpb am 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.
Don
Don am 15 Jan. 2016
Bearbeitet: Don am 15 Jan. 2016
I did attach a figure, but apparently it came unattached. here it is again

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 16 Jan. 2016
Bearbeitet: dpb am 17 Jan. 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 --
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 Kommentare
Don
Don am 17 Jan. 2016
All this turns out to be a lot trickier than I expected. However, the subplot plotyy problem seems to successfully confine the plots to a single square-axis figure. If I can get the RH y-axis T3 and T4)labeled and scaled that would be great. Presently used code is below and figure is attached Many many thanks.
figure(2);
start=floor(TrajX(1));
stop=floor(TrajX(length(TrajX)));
P3_FzInv = flipud(P3_Fz*-1);
z3 = P3_FzInv;
subplot(2,1,1);
hAx1=plotyy(t,z3,t,z2);% zi = P3-Fz as received Z3 = P3_FzInverse
axis square;
subplot(2,1,1)
hAx2=plot(t,z3,t,z2);
axis square;
grid on;
set(hAx2,'LineWidth',2);
xlabel(hAx1(1),'seconds');
ylabel(hAx1(1),'Fz <<--- Power Site --->> P3');
%ylabel(hAx1(2),'T3 <<--- Power Site --->> T4');
title(File2VarTitle);
FigHandle2 = figure(2);
set(FigHandle2, 'Position', [200, 200, 800,800])
dpb
dpb am 18 Jan. 2016
Bearbeitet: dpb am 18 Jan. 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 --
  1. There's only a single axis for the lines to be plotted into, and
  2. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Walter Roberson
Walter Roberson am 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 Kommentare
Walter Roberson
Walter Roberson am 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
Don am 17 Jan. 2016
Ornithorhynchus anatinus sends thanks to Nyctereutes procyonoides viverrinus We're gaining on it!

Melden Sie sich an, um zu kommentieren.


dpb
dpb am 15 Jan. 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"

Don
Don am 15 Jan. 2016
as you can see in the top plot, one of the variables (z2, I think) does not fit within the plot boundaries I can't find a problem with the plotyy code. maybe this is a bug. If so, can you suggest a work-around?
many thanks for help
  1 Kommentar
Don
Don am 15 Jan. 2016
Here's the code. t (=x-vector)added to (start:stop) in plotyy call, RH axes. still have problem with variable z2 (RH axes % code figure(2);
start=floor(TrajX(1));
stop=floor(TrajX(length(TrajX)));
P3_FzInv = flipud(P3_Fz*-1);
z3 = P3_FzInv;
subplot(2,1,1); % top plot, P3Fz v. T4T3
[ax,p1,p2]=plotyy(t(start:stop),z1(start:stop),t(start:stop),z2(start:stop));
xlim([start,stop]);
axis('square');
grid on;
set(p1,'LineWidth',2);
set(p2,'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])
NOTE: "Fig 2 with t for RH axes" shows as "attached" but I cannot get it to appear in this comment window
Problem still persists with z2 not confined to plot box Is this a bug in the plotyy code?

Melden Sie sich an, um zu kommentieren.


Don
Don am 18 Jan. 2016
In the words of Colombo, "one more thing..."
plot looks correct (finally!) I have tried all the combinations I can think of but still cannot get lineWidth set to 2. I would study the principles behind this plotyy design with handles, if I could locate a book that explains the subject. Otherwise, otherwise its trial and error, then ask a (probably) elementary question. Many thanks, as usual, for any assistance
  5 Kommentare
dpb
dpb am 20 Jan. 2016
Bearbeitet: dpb am 20 Jan. 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.
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
dpb am 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".

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Two y-axis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by