using plot with 2 x and 2 y axis
Mostrar comentarios más antiguos
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
Más respuestas (2)
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
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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

