using plot with 2 x and 2 y axis
59 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
dpb
on 17 Jul 2016
Edited: dpb
on 18 Jul 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 Comments
Image Analyst
on 20 Jul 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
on 20 Jul 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?
More Answers (2)
Azzi Abdelmalek
on 17 Jul 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);
0 Comments
Image Analyst
on 17 Jul 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.
0 Comments
See Also
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!