Reputation: 107
I have the following code. I'm trying to create an xy plot wth two y axis. However I only get one line down the middle. I want y to be the vertical axis to the right and vel to be the vertical axis to the left. I have several sets of data for different positions and I'm trying to place the first at 0.66 on the x-axis, the second at 1 etc but I cannot get it to work. Help please.
Regards, Jer
clc
clear
%Retrieve data and figure setup
filename = 'G:\Protable Hard Drive\PHD Hard Drive\Experimental Data\Bulkrename Trial\Common data for line graphs\Data for line graphs.xls';
a = xlsread(filename, 'Veldef');
vel = 0:1/37:1;
y = -16/15:1/15:21/15;
%X/D=0.66 TSR5
x = 0.66;
exp = a(1:38,2);
ko = a(1:38,4);
rst = a(1:38,6);
%Plot
h = plot(x,exp,x,ko,x,rst);
Upvotes: 0
Views: 849
Reputation: 645
Starting from Matlab 2016a you can also use new function yyaxis
. Example from documentation:
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)
z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])
Upvotes: 0
Reputation: 12214
One option is plotyy()
x = 1:10;
y = rand(1,10);
plotyy(x, x, x, y)
A more flexible option is to overlay two (or more) axes and specify what data you want plotted on each.
% Sample data
x = 1:10;
y = rand(1,10);
% Create axes & store handles
h.myfig = figure;
h.ax1 = axes('Parent', h.myfig, 'Box', 'off');
h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right');
% Preserve axes formatting
hold(h.ax1, 'on');
hold(h.ax2, 'on');
% Plot data
plot(h.ax1, x, x);
plot(h.ax2, x, y);
The Box
property turns off drawing of the external box around each axis. I'd recommend turning it off for all but one of the axes to eliminate axis tick clutter.
The Position
property sets the axis size and position to the exact same as the first axis. Note that I've used the dot notation introduced in R2014b, if you have an older version just swap h.ax1.Position
with get(h.ax1, 'Position')
.
The Color
and YAxisLocation
calls should be self explanatory.
I used hold
to preserve the axes formatting. If you don't include these and plot your data it will reset the background color and axes locations, requiring you to adjust them back.
Hope this helps!
Upvotes: 1