using plot with 2 x and 2 y axis

Hello, Here is what I would like to do. I have a matrix which is n x 4. I want to utilize plot to create a figure where column 1 & 2 are plotted with the axes on the left and bottom and column 3 & 4 plotted with the axes on the right and top. The x axis values are not identical but would like them both to start at the left axis. I don't have any code examples and I've been able to utilize line to accomplish this, but would like to use plot instead. Thanks..Dave

 Respuesta aceptada

dpb
dpb el 17 de Jul. de 2016
Editada: dpb el 18 de Jul. de 2016
Just use plotyy and fix the initial result up a little...example
x=[[1:10].' [1:10].'-5];
y=[randn(10,2) randn(10,2)];
hAx=plotyy(x(:,1),y(:,1:2),x(:,2),y(:,3:4));
set(hAx(2),'XAxisLoc','top')
xlim(hAx(1),[min(x(:,1) max(x(:,1)])
xlim(hAx(2),[min(x(:,2) max(x(:,2)])
results in
If you're going to want to do this more than just once or twice, make a new function containing the above and then won't have to do much at all except call it. Be sure, of course, in that case to save and return the other handles from plotyy so you'll have easy access to them to make any other modifications wanted besides the bare bones to begin...

18 comentarios

Dave
Dave el 17 de Jul. de 2016
I think this is the direction I want to go, however, when I try the example I get the following error. "Error: File: testplotyy.m Line: 5 Column: 25 Unexpected MATLAB expression."
dpb
dpb el 17 de Jul. de 2016
Show your work...clearly it doesn't do that here altho I've a now somewhat dated release.
Show the exact code you used that caused the problem; it looks like you've probably got a typo in your testplotyy function.
Dave
Dave el 17 de Jul. de 2016
Editada: dpb el 17 de Jul. de 2016
what I did just as a test was cut and pasted the example.
x=[[1:10].' [1:10].'-5];
y=[randn(10,2) randn(10,2)];
hAx=plotyy(x(:,1),y(:,1:2),x(:,2),y(:,3:4));
set(hAx(2),'XAxisLoc','top')
xlim(hAx(1),[min(x(:,1) max(x(:,1)])
xlim(hAx(2),[min(x(:,2) max(x(:,2)])
I'm using version 2016a and got the error.
Dave
Dave el 17 de Jul. de 2016
Words to live by, but I don't think that's the issue. I'm wondering if xlim has changed in later versions. When I paste the a example into a script, it doesn't like the last two lines. It says there is an invalid syntax at 'max', possible missing )]}. I've reviewed the documentation and modifying the lines without success. Thanks for the help
dpb
dpb el 17 de Jul. de 2016
I don't see anything untoward...copy the exact code including all error messages in context from the screen. Can't tell what it didn't like from the above in isolation, sorry.
Dave
Dave el 17 de Jul. de 2016
Editada: dpb el 17 de Jul. de 2016
x=[[1:10].' [1:10].'];
y=[randn(10,2) randn(10,2)];
hAx=plotyy(x(:,1),y(:,1:2),x(:,2),y(:,3:4));
set(hAx(2),'XAxisLoc','top')
xlim(hAx(1),[min(x(:,1)) max(x(:,1))])
xlim(hAx(2),[min(x(:,2)) max(x(:,2))])
I modified the last two lines by adding ")" but if the top X axis still does not have any values.
dpb
dpb el 17 de Jul. de 2016
Editada: dpb el 17 de Jul. de 2016
Yeah, missing closing paren's would cause the syntax error.
I subtracted 5 from the second x value vector (the "-5") so the two axis ranges wouldn't be identical as they are with the change; then forgot if you saw my note when I thought I'd mistyped...that is ok and was intended.
I think maybe the issue is the new HG2 engine; seems like I've seen somewhere that it needs the names of properties to be spelled out in their entirety; I'm used to being able to use the shortest unique name and having it match up for me...
Try
set(hAx(2),'XAxisLocation','top')
Oh, perhaps HG2 plotyy turns the tick labels and/or ticks off now by default so won't accidentally clash.
What do
get(hAx(2),'XTick')
get(hAx(2),'XTickLabel')
get(hAx(2),'XTickMode')
return?
Oh, and
get(hAx(2),'XTickPosition')
too, of course...
Dave
Dave el 17 de Jul. de 2016
get(hAx(2),'XTick') returns "1 2 3 4 5 6 7 8 9 10" get(hAx(2),'XTickLabel') returns "'1' '2' '3' '4' '5' '6' '7' '8' '9' '10'
get(hAx(2),'XTickMode') returns "auto"
get(hAx(2),'XTickPosition') returns "Error using matlab.graphics.axis.Axes/get Invalid or deleted object."
dpb
dpb el 17 de Jul. de 2016
Got to pay closer attention to the property names--
get(hAx(2),'XTickPosition') returns "Error using matlab.graphics.axis.Axes/get Invalid or deleted object."
get(hAx(2),'XAxesPosition')
Ticks don't have a position property. Tick marks and labels are ok, so maybe you didn't actually get the position set...
Oh, BTW, use the 'Code' button to format your code (or just enter two blanks preceding typing is shorthand way...). That'll stop the word wrap and keep stuff on line intended w/o the extra blank lines taking up room. Why TMW won't turn automagic word wrap off for a coding forum just irritates me no end... :(
Dave
Dave el 17 de Jul. de 2016
get(hAx(2),'XAxesPosition') Error using matlab.graphics.axis.Axes/get There is no XAxesPosition property on the Axes class.
This is what I get with get(hAx(2),'XAxesPosition')
dpb
dpb el 17 de Jul. de 2016
Chek speling. It's 'XAxisLocation'
You're not doing any work on your own it seems...sorry I misnamed it, but it shouldn't be hard to look at the doc and see where I made an error instead of waiting...
Dave
Dave el 18 de Jul. de 2016
You are correct, though I am trying to look through the documentation, just not real familiar with Matlab and using multiple axis. the hAx(2) axis is at the top, the values from get(hAx(2),'TickLabel')and get(hAx(2),'XTick') are correct they are just not being displayed on the figure.
Image Analyst
Image Analyst el 18 de Jul. de 2016
Dave, did you see the other answers?
dpb
dpb el 18 de Jul. de 2016
Editada: dpb el 18 de Jul. de 2016
They do the same thing albeit plotyy hides creating the second axis...
Whatever is the issue, it's got to do with what Dave is doing IN CONTEXT with the rest of his system; simply setting the 'XAxisLocation','top' has to put the axis at the top unless HG2 has done something funky with it...I can't test that.
But, it's not that hard directly, either, true...
Hmmm....I wonder if plotyy now does something funky like linkaxes on the x-axes that could be screwing up the display? This is puzzling and since I'm at R2012b can't test here--
Just for grins, see if
linkaxes(hAx,'off')
makes any difference. Wouldn't think so, but...
After that, either just go with the straightforward "make two axes and plot into them yourself" solution or try one last time with a fresh figure and the above example with no typos...it really should be "a piece o' cake", there's nothing at all exotic here...
ADDENDUM One last thought came to me overnight...wonder if plotyy now sets 'XColor' to background color or somesuch so it doesn't show for second axes...does
get(hAx(2),'XColor')
return [0 0 0] (black)? If not, set it...
Dave
Dave el 20 de Jul. de 2016
Sorry, been out of contact for a couple of days. Just to close this out, I've tried the last two suggestions and still no values displayed on the top axis. However, after looking at the data I will be working with, it will suffice to just utilize two y axis and one x axis as that data are the same units and close in values. I'll just check which min and max values and then a short if statement for which to use for the figure. I appreciate the assistance, and I've learned a lot in part of Matlab I have not used before.
dpb
dpb el 20 de Jul. de 2016
"...still no values displayed on the top axis"
If that is so with a clean Matlab session, there's something definitely wrong and that's worth putting in a support request to TMW at www.mathworks.com to get to root cause.
Anybody else see same symptoms with recent release?
Image Analyst
Image Analyst el 20 de Jul. de 2016
No. Like with the screenshot I posted in my Answer, there are definitely tick marks and tick labels on the top axis along the top edge of the graph.
dpb
dpb el 20 de Jul. de 2016
OK, what I expected; it's a system-dependent issue. There was a posting not long ago on "when graphics go funky, do this" and I was going to remember what "this" was, and now I forget...something to do with OpenGL setting, maybe??? Anybody watching recall that?

Iniciar sesión para comentar.

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 17 de Jul. de 2016
Look at this example
% ------------------your 2 functions-----------------------------------------
x1=0:0.5:10;
x2=x1+1
y1=sin(x1);
y2=10*cos(x2);
c1='r' % color of curve 1
c2='g' % color of curve 2
%----------------------------------------------plot y1----------------------
line(x1,y1,'color',c1)
ax1=gca;
set(gcf,'position',[100 100 1300 550])
%----------------------------------------------plot y2--------------------------
pos=double(get(ax1,'position'))
ax2=axes('position',pos, 'XAxisLocation','top','YAxisLocation','right', 'Color','none', 'XColor',c2,'YColor',c2);
line(x2,y2,'color',c2,'parent',ax2);
Image Analyst
Image Analyst el 17 de Jul. de 2016
Look up "Graph with Multiple x-Axes and y-Axes" in the help. Here is their example:
%%Graph with Multiple x-Axes and y-Axes
% This example shows how to create a graph using the bottom and left
% sides of the axes for the first plot, and the top and
% right sides of the axes for the second plot.
%
Create the data to plot.
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
Use the line function to plot y1 versus x1 using a red line. Set the color for the x-axis and y-axis to red.
Note: Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the docid:matlab_ref.f67-432995 function instead, such as set(ax1,'XColor','r').
figure
line(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
Create a second axes in the same location as the first axes by setting the position of the second axes equal to the position of the first axes. Specify the location of the x-axis as the top of the graph and the y-axis as the right side of the graph. Set the axes Color to 'none' so that the first axes is visible underneath the second axes.
Note: Starting in R2014b, you can use dot notation to query properties. If you are using an earlier release, use the docid:matlab_ref.f58-517463 function instead, such as ax1_pos = get(ax1,'Position').
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
Use the line function to plot y2 versus x2 on the second axes. Set the line color to black so that it matches the color of the corresponding x-axis and y-axis.
line(x2,y2,'Parent',ax2,'Color','k')
The graph contains two lines that correspond to different axes. The red line corresponds to the red axes. The black line corresponds to the black axes.
Note the axes colors match the curve colors so you know what curve uses what axes.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Jul. de 2016

Comentada:

dpb
el 20 de Jul. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by